Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lowering first letters without losing underscore in order to be compatible with C# Web Api and bug fix #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
16 changes: 14 additions & 2 deletions converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down