-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathPropertyKeyConstrainer.ts
81 lines (77 loc) · 2.82 KB
/
PropertyKeyConstrainer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { ConstrainedObjectModel, ObjectModel } from '../../../models';
import {
NO_NUMBER_START_CHAR,
NO_DUPLICATE_PROPERTIES,
NO_EMPTY_VALUE,
NO_RESERVED_KEYWORDS
} from '../../../helpers/Constraints';
import { FormatHelpers, PropertyKeyConstraint } from '../../../helpers';
import { isReservedTemplateKeyword } from '../Constants';
import { TemplatePropertyKeyConstraint } from '../TemplateGenerator';
export type PropertyKeyConstraintOptions = {
NO_SPECIAL_CHAR: (value: string) => string;
NO_NUMBER_START_CHAR: (value: string) => string;
NO_DUPLICATE_PROPERTIES: (
constrainedObjectModel: ConstrainedObjectModel,
objectModel: ObjectModel,
propertyName: string,
namingFormatter: (value: string) => string
) => string;
NO_EMPTY_VALUE: (value: string) => string;
NAMING_FORMATTER: (value: string) => string;
NO_RESERVED_KEYWORDS: (value: string) => string;
};
export const DefaultPropertyKeyConstraints: PropertyKeyConstraintOptions = {
NO_SPECIAL_CHAR: (value: string) => {
//Exclude ` ` because it gets formatted by NAMING_FORMATTER
//Exclude '_' because they are allowed
return FormatHelpers.replaceSpecialCharacters(value, {
exclude: [' ', '_'],
separator: '_'
});
},
NO_NUMBER_START_CHAR,
NO_DUPLICATE_PROPERTIES,
NO_EMPTY_VALUE,
NAMING_FORMATTER: FormatHelpers.toCamelCase,
NO_RESERVED_KEYWORDS: (value: string) => {
return NO_RESERVED_KEYWORDS(value, isReservedTemplateKeyword);
}
};
/**
* Default constraint logic for Template, which converts the object property key into something that is compatible with Template
*/
export function defaultPropertyKeyConstraints(
customConstraints?: Partial<PropertyKeyConstraintOptions>
): TemplatePropertyKeyConstraint {
const constraints = {
...DefaultPropertyKeyConstraints,
...customConstraints
};
return ({ objectPropertyModel, constrainedObjectModel, objectModel }) => {
let constrainedPropertyKey = objectPropertyModel.propertyName;
constrainedPropertyKey = constraints.NO_SPECIAL_CHAR(
constrainedPropertyKey
);
constrainedPropertyKey = constraints.NO_NUMBER_START_CHAR(
constrainedPropertyKey
);
constrainedPropertyKey = constraints.NO_EMPTY_VALUE(constrainedPropertyKey);
constrainedPropertyKey = constraints.NO_RESERVED_KEYWORDS(
constrainedPropertyKey
);
//If the property name has been manipulated, lets make sure it don't clash with existing properties
if (constrainedPropertyKey !== objectPropertyModel.propertyName) {
constrainedPropertyKey = constraints.NO_DUPLICATE_PROPERTIES(
constrainedObjectModel,
objectModel,
constrainedPropertyKey,
constraints.NAMING_FORMATTER
);
}
constrainedPropertyKey = constraints.NAMING_FORMATTER(
constrainedPropertyKey
);
return constrainedPropertyKey;
};
}