From 9dca9891cb8e2c1c0b85032d661d5f88b49c7464 Mon Sep 17 00:00:00 2001 From: Sahin Kasap <39086573+sahin52@users.noreply.github.com> Date: Wed, 22 Feb 2023 14:35:46 +0300 Subject: [PATCH 1/3] A very important bug fix - some attributes contained "=" sign, + lowerFirstLetter option is added to enable lowering first letter without using camel case to preserve underscore in attributes + README update --- README.md | 12 ++++++++++++ converter.js | 13 +++++++++++-- index.js | 1 + 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d90798..a0f3e4e 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, + "lowerFirstLetter": 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 +**lowerFirstLetter** lowers the first letter of attributes, if they are starting with an upper letter and continues with a lower one. + + + ## License diff --git a/converter.js b/converter.js index 126af06..37cf9cd 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,16 @@ const createConverter = config => { return array ? `${type}[]` : type; }; - const convertIdentifier = identifier => config.camelCase ? camelcase(identifier, config.camelCaseOptions) : identifier; + const convertIdentifier = identifier => { + identifier = config.lowerFirstLetter ? lowerFirstLetter(identifier) : identifier; + return config.camelCase ? camelcase(identifier, config.camelCaseOptions) : identifier; + } + const lowerFirstLetter = str => { + if(str.length===1) return str.toLowerCase(); + if(str[0].toUpperCase()===str[0] && str[1].toLowerCase() ===str[1]) + return str.slice(0, 1).toLowerCase() + str.slice(1); + return str; + } const convertType = type => type in typeTranslations ? typeTranslations[type] : type; return convert; diff --git a/index.js b/index.js index 13fc549..7b65a1d 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, + lowerFirstLetter: config.lowerFirstLetter, camelCase: config.camelCase || false, camelCaseOptions: config.camelCaseOptions || {}, camelCaseEnums: config.camelCaseEnums || false, From 350708999a9fa6a73f601744bb2ad30073bb10f9 Mon Sep 17 00:00:00 2001 From: Sahin Kasap <39086573+sahin52@users.noreply.github.com> Date: Wed, 22 Feb 2023 15:31:35 +0300 Subject: [PATCH 2/3] lowering first letters of words are implemented, in order to be the same with C# web api backend --- README.md | 4 ++-- converter.js | 15 +++++++++------ index.js | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a0f3e4e..f34720c 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ $ npm install --save csharp-models-to-typescript "namespace": "Api", "output": "./api.d.ts", "camelCase": false, - "lowerFirstLetter": false, + "lowerFirstLetters": false, "camelCaseEnums": false, "camelCaseOptions": { "pascalCase": false, @@ -67,7 +67,7 @@ $ npm install --save csharp-models-to-typescript **namespace** is the namespace for the generated files **include** are the locations for the input files **exclude** are files that are excluded -**lowerFirstLetter** lowers the first letter of attributes, if they are starting with an upper letter and continues with a lower one. +**lowerFirstLetters** lowers the first letters of attributes. ID => id MYLocation => myLocation diff --git a/converter.js b/converter.js index 37cf9cd..942a484 100644 --- a/converter.js +++ b/converter.js @@ -176,14 +176,17 @@ const createConverter = config => { }; const convertIdentifier = identifier => { - identifier = config.lowerFirstLetter ? lowerFirstLetter(identifier) : identifier; + identifier = config.lowerFirstLetters ? lowerFirstLetters(identifier) : identifier; return config.camelCase ? camelcase(identifier, config.camelCaseOptions) : identifier; } - const lowerFirstLetter = str => { - if(str.length===1) return str.toLowerCase(); - if(str[0].toUpperCase()===str[0] && str[1].toLowerCase() ===str[1]) - return str.slice(0, 1).toLowerCase() + str.slice(1); - return str; + const lowerFirstLetters = str => { + let i = 0; + while (i < str.length && str[i].toUpperCase() === str[i] && str[i] !== "_") { + i++; + } + if(i>1) + 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; diff --git a/index.js b/index.js index 7b65a1d..f67d7ec 100755 --- a/index.js +++ b/index.js @@ -33,7 +33,7 @@ const output = config.output || 'types.d.ts'; const converter = createConverter({ customTypeTranslations: config.customTypeTranslations || {}, namespace: config.namespace, - lowerFirstLetter: config.lowerFirstLetter, + lowerFirstLetters: config.lowerFirstLetters, camelCase: config.camelCase || false, camelCaseOptions: config.camelCaseOptions || {}, camelCaseEnums: config.camelCaseEnums || false, From 1c209cc848f4928a792940d74bef6518a8d13a91 Mon Sep 17 00:00:00 2001 From: Sahin Kasap <39086573+sahin52@users.noreply.github.com> Date: Wed, 22 Feb 2023 17:02:09 +0300 Subject: [PATCH 3/3] small bug fix - ID was converted as iD but should be id, and BB4BBaa was returned like bb4bbaa but should be bB4BBaa --- converter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/converter.js b/converter.js index 942a484..1c01fdd 100644 --- a/converter.js +++ b/converter.js @@ -181,10 +181,10 @@ const createConverter = config => { } const lowerFirstLetters = str => { let i = 0; - while (i < str.length && str[i].toUpperCase() === str[i] && str[i] !== "_") { + while (i < str.length && str[i].toLowerCase() !== str[i] && str[i] !== "_") { i++; } - if(i>1) + 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); }