-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
ModelNameConstrainer.ts
52 lines (48 loc) · 1.87 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
import {
NO_NUMBER_START_CHAR,
NO_EMPTY_VALUE,
NO_RESERVED_KEYWORDS
} from '../../../helpers/Constraints';
import { FormatHelpers } from '../../../helpers';
import { isReservedCplusplusKeyword } from '../Constants';
import { CplusplusModelNameConstraint } from '../CplusplusGenerator';
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: FormatHelpers.toSnakeCase,
NO_RESERVED_KEYWORDS: (value: string) => {
return NO_RESERVED_KEYWORDS(value, isReservedCplusplusKeyword);
}
};
/**
* Default constraint logic for Cplusplus, which converts the model name into something that is compatible with Cplusplus
*/
export function defaultModelNameConstraints(
customConstraints?: Partial<ModelNameConstraints>
): CplusplusModelNameConstraint {
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;
};
}