-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathModelNameConstrainer.ts
54 lines (50 loc) · 1.94 KB
/
ModelNameConstrainer.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
import {
NO_NUMBER_START_CHAR,
NO_EMPTY_VALUE,
NO_RESERVED_KEYWORDS
} from '../../../helpers/Constraints';
import { FormatHelpers, ModelNameConstraint } from '../../../helpers';
import { isReservedTemplateKeyword } from '../Constants';
import { TemplateModelNameConstraint } from '../TemplateGenerator';
export type ModelNameConstraints = {
NO_SPECIAL_CHAR: (value: string) => string;
NO_NUMBER_START_CHAR: (value: string) => string;
NO_EMPTY_VALUE: (value: string) => string;
NAMING_FORMATTER: (value: string) => string;
NO_RESERVED_KEYWORDS: (value: string) => string;
};
export const DefaultModelNameConstraints: ModelNameConstraints = {
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_EMPTY_VALUE,
NAMING_FORMATTER: (value: string) => {
return FormatHelpers.toPascalCase(value);
},
NO_RESERVED_KEYWORDS: (value: string) => {
return NO_RESERVED_KEYWORDS(value, isReservedTemplateKeyword);
}
};
/**
* Default constraint logic for Template, which converts the model name into something that is compatible with Template
*/
export function defaultModelNameConstraints(
customConstraints?: Partial<ModelNameConstraints>
): TemplateModelNameConstraint {
const constraints = { ...DefaultModelNameConstraints, ...customConstraints };
return ({ modelName }) => {
let constrainedValue = modelName;
constrainedValue = constraints.NO_SPECIAL_CHAR(constrainedValue);
constrainedValue = constraints.NO_NUMBER_START_CHAR(constrainedValue);
constrainedValue = constraints.NO_EMPTY_VALUE(constrainedValue);
constrainedValue = constraints.NO_RESERVED_KEYWORDS(constrainedValue);
constrainedValue = constraints.NAMING_FORMATTER(constrainedValue);
return constrainedValue;
};
}