|
| 1 | +export enum CaseTransformer { |
| 2 | + PASCAL_TO_CAMEL, |
| 3 | + PASCAL_TO_SNAKE, |
| 4 | + SNAKE_TO_CAMEL, |
| 5 | +} |
| 6 | + |
| 7 | +export class Transformer { |
| 8 | + private readonly _object: { [key: string]: any; }; |
| 9 | + private caseTransformer: CaseTransformer; |
| 10 | + private useSafeKeys: boolean; |
| 11 | + private LANGUAGE_KEYWORDS = [ |
| 12 | + "abstract", |
| 13 | + "any", |
| 14 | + "as", |
| 15 | + "async", |
| 16 | + "await", |
| 17 | + "bigint", |
| 18 | + "boolean", |
| 19 | + "break", |
| 20 | + "case", |
| 21 | + "catch", |
| 22 | + "class", |
| 23 | + "configurable", |
| 24 | + "const", |
| 25 | + "constructor", |
| 26 | + "continue", |
| 27 | + "debugger", |
| 28 | + "declare", |
| 29 | + "default", |
| 30 | + "delete", |
| 31 | + "do", |
| 32 | + "else", |
| 33 | + "enum", |
| 34 | + "enumerable", |
| 35 | + "export", |
| 36 | + "extends", |
| 37 | + "false", |
| 38 | + "finally", |
| 39 | + "for", |
| 40 | + "from", |
| 41 | + "function", |
| 42 | + "get", |
| 43 | + "if", |
| 44 | + "in", |
| 45 | + "implements", |
| 46 | + "import", |
| 47 | + "instanceof", |
| 48 | + "interface", |
| 49 | + "is", |
| 50 | + "let", |
| 51 | + "module", |
| 52 | + "namespace", |
| 53 | + "never", |
| 54 | + "new", |
| 55 | + "null", |
| 56 | + "number", |
| 57 | + "of", |
| 58 | + "package", |
| 59 | + "private", |
| 60 | + "protected", |
| 61 | + "public", |
| 62 | + "readonly", |
| 63 | + "require", |
| 64 | + "return", |
| 65 | + "set", |
| 66 | + "static", |
| 67 | + "string", |
| 68 | + "super", |
| 69 | + "switch", |
| 70 | + "symbol", |
| 71 | + "this", |
| 72 | + "throw", |
| 73 | + "true", |
| 74 | + "try", |
| 75 | + "type", |
| 76 | + "typeof", |
| 77 | + "undefined", |
| 78 | + "value", |
| 79 | + "var", |
| 80 | + "void", |
| 81 | + "while", |
| 82 | + "with", |
| 83 | + "writable", |
| 84 | + "yield", |
| 85 | + ]; |
| 86 | + |
| 87 | + constructor(object: { [key: string]: any }) { |
| 88 | + this._object = object; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns an instance of `Transformer` for the given object, that let you chain other options/methods. |
| 93 | + * This is intended to always call at then end {@link Transformer#transform} to return the transformed object. |
| 94 | + * |
| 95 | + * @param object The object for which you wish the transform its keys. |
| 96 | + */ |
| 97 | + public static for(object: { [key: string]: any }) { |
| 98 | + return new Transformer(object); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Used to specify how to transform the keys, like PascalCase to camelCase. |
| 103 | + * See enum {@link CaseTransformer} for all available options. |
| 104 | + * |
| 105 | + * @param caseTransformer The transformer to use on the object keys. |
| 106 | + */ |
| 107 | + public transformKeys(caseTransformer: CaseTransformer) { |
| 108 | + this.caseTransformer = caseTransformer; |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Used to generate transform keys with format that the model auto-generation for TypeScript expects. |
| 114 | + * |
| 115 | + * Detail: The model auto-generation will suffix keys with `_` if they are keywords from the language |
| 116 | + * (see https://github.com/cloudsoft/cloudformation-cli-typescript-plugin/blob/master/python/rpdk/typescript/utils.py#L83-L86) |
| 117 | + */ |
| 118 | + public forModelIngestion() { |
| 119 | + this.useSafeKeys = true; |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Transform the given object keys, according to the given {@link CaseTransformer}. |
| 125 | + * |
| 126 | + * This will return `undefined` if given `object` is not set. |
| 127 | + * This will throw an exception is the given {@link CaseTransformer} is invalid. |
| 128 | + */ |
| 129 | + public transform() { |
| 130 | + switch (this.caseTransformer) { |
| 131 | + case CaseTransformer.PASCAL_TO_CAMEL: |
| 132 | + return this.transformObjectKeys(this._object, key => key.substring(0, 1).toLocaleLowerCase() + key.substring(1)); |
| 133 | + case CaseTransformer.PASCAL_TO_SNAKE: |
| 134 | + return this.transformObjectKeys(this._object, key => key.substring(0, 1).toLocaleLowerCase() + key.substring(1).replace(/([A-Z])/g, (input) => `_${input.toLocaleLowerCase()}`)); |
| 135 | + case CaseTransformer.SNAKE_TO_CAMEL: |
| 136 | + return this.transformObjectKeys(this._object, key => key.substring(0, 1).toLocaleLowerCase() + key.substring(1).replace(/_([a-z])/g, (input, p1) => `${p1.toLocaleUpperCase()}`)); |
| 137 | + default: |
| 138 | + throw new Error(`Case transformer "${this.caseTransformer}" not supported`); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private transformObjectKeys(object: { [key: string]: any }, transformer: (key: string) => string) { |
| 143 | + if (!object) { |
| 144 | + return object; |
| 145 | + } |
| 146 | + |
| 147 | + return Object.keys(object).reduce((map, key) => { |
| 148 | + let value = object[key]; |
| 149 | + if (value && value instanceof Object && !(value instanceof Array) && !(value instanceof Set)) { |
| 150 | + value = this.transformObjectKeys(value, transformer); |
| 151 | + } |
| 152 | + if (value && value instanceof Set) { |
| 153 | + value = Array.of(...value); |
| 154 | + } |
| 155 | + if (value && Array.isArray(value)) { |
| 156 | + value = value.map(item => item && item instanceof Object && !(item instanceof Array) && !(item instanceof Set) |
| 157 | + ? this.transformObjectKeys(item, transformer) |
| 158 | + : item); |
| 159 | + } |
| 160 | + let newKey = transformer(key); |
| 161 | + if (this.useSafeKeys) { |
| 162 | + newKey = this.safeKey(newKey); |
| 163 | + } |
| 164 | + map[newKey] = value; |
| 165 | + return map; |
| 166 | + }, {} as { [key: string]: any }) |
| 167 | + } |
| 168 | + |
| 169 | + private safeKey(key: string) { |
| 170 | + return this.LANGUAGE_KEYWORDS.includes(key) |
| 171 | + ? `${key}_` |
| 172 | + : key; |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +// For backwards compatibility |
| 177 | +export function transformObjectCase(model: { [key: string]: any }, caseTransformer: CaseTransformer){ |
| 178 | + return Transformer.for(model) |
| 179 | + .transformKeys(caseTransformer) |
| 180 | + .transform(); |
| 181 | +} |
0 commit comments