diff --git a/README.md b/README.md index 4d90798..f34720c 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ $ npm install --save csharp-models-to-typescript "namespace": "Api", "output": "./api.d.ts", "camelCase": false, + "lowerFirstLetters": false, "camelCaseEnums": false, "camelCaseOptions": { "pascalCase": false, @@ -59,6 +60,17 @@ $ npm install --save csharp-models-to-typescript 3. Run the npm script `generate-types` and the output file specified in your config should be created and populated with your models. +## Configure Config File + +**camelCase** makes the attributes camel case, using **camelCaseOptions**. npm camelcase package is used for this purpose. +**output** is the location for output +**namespace** is the namespace for the generated files +**include** are the locations for the input files +**exclude** are files that are excluded +**lowerFirstLetters** lowers the first letters of attributes. ID => id MYLocation => myLocation + + + ## License diff --git a/converter.js b/converter.js index 126af06..1c01fdd 100644 --- a/converter.js +++ b/converter.js @@ -125,7 +125,7 @@ const createConverter = config => { const convertProperty = property => { const optional = property.Type.endsWith('?'); - const identifier = convertIdentifier(optional ? `${property.Identifier.split(' ')[0]}?` : property.Identifier.split(' ')[0]); + const identifier = convertIdentifier(optional ? `${property.Identifier.split(/[ =]+/)[0]}?` : property.Identifier.split(/[ =]+/)[0]); const type = parseType(property.Type); @@ -175,7 +175,19 @@ const createConverter = config => { return array ? `${type}[]` : type; }; - const convertIdentifier = identifier => config.camelCase ? camelcase(identifier, config.camelCaseOptions) : identifier; + const convertIdentifier = identifier => { + identifier = config.lowerFirstLetters ? lowerFirstLetters(identifier) : identifier; + return config.camelCase ? camelcase(identifier, config.camelCaseOptions) : identifier; + } + const lowerFirstLetters = str => { + let i = 0; + while (i < str.length && str[i].toLowerCase() !== str[i] && str[i] !== "_") { + i++; + } + if(i>1 && str.length !==i) + i = i-1; // if it has only one upper letter, it will be lowered, but if it has more than one upper letter, the last one may be start of another string + return str.slice(0, i).toLowerCase() + str.slice(i); + } const convertType = type => type in typeTranslations ? typeTranslations[type] : type; return convert; diff --git a/index.js b/index.js index 13fc549..f67d7ec 100755 --- a/index.js +++ b/index.js @@ -33,6 +33,7 @@ const output = config.output || 'types.d.ts'; const converter = createConverter({ customTypeTranslations: config.customTypeTranslations || {}, namespace: config.namespace, + lowerFirstLetters: config.lowerFirstLetters, camelCase: config.camelCase || false, camelCaseOptions: config.camelCaseOptions || {}, camelCaseEnums: config.camelCaseEnums || false,