Skip to content

Commit

Permalink
feat(schematics): use plural for entity schematics reducer key (#1596)
Browse files Browse the repository at this point in the history
Closes #1412
  • Loading branch information
santoshyadavdev authored and brandonroberts committed Mar 12, 2019
1 parent 8005131 commit 1e49530
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 71 deletions.
22 changes: 12 additions & 10 deletions modules/effects/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function addReducerToState(options: any): Rule {
export function addReducerToStateInterface(
source: ts.SourceFile,
reducersPath: string,
options: { name: string }
options: { name: string; plural: boolean }
): Change {
const stateInterface = source.statements.find(
stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration
Expand All @@ -90,11 +90,12 @@ export function addReducerToStateInterface(
return new NoopChange();
}

const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
stringUtils.camelize(options.name) +
': from' +
stringUtils.classify(options.name) +
'.State;';
state + ': from' + stringUtils.classify(options.name) + '.State;';
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -125,7 +126,7 @@ export function addReducerToStateInterface(
export function addReducerToActionReducerMap(
source: ts.SourceFile,
reducersPath: string,
options: { name: string }
options: { name: string; plural: boolean }
): Change {
let initializer: any;
const actionReducerMap: any = source.statements
Expand All @@ -152,11 +153,12 @@ export function addReducerToActionReducerMap(

let node = actionReducerMap.initializer;

const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
stringUtils.camelize(options.name) +
': from' +
stringUtils.classify(options.name) +
'.reducer,';
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const expr = node as any;
let position;
let toInsert;
Expand Down
19 changes: 19 additions & 0 deletions modules/effects/schematics-core/utility/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.substr(1);
}

/**
Returns the plural form of a string
```javascript
'innerHTML'.pluralize() // 'InnerHTMLs'
'action_name'.pluralize() // 'actionNames'
'css-class-name'.pluralize() // 'cssClassNames'
'regex'.pluralize() // 'regexes'
'user'.pluralize() // 'users'
```
*/
export function pluralize(str: string): string {
return camelize(
[/([^aeiou])y$/, /()fe?$/, /([^aeiou]o|[sxz]|[cs]h)$/].map(
(c, i) => (str = str.replace(c, `$1${'iv'[i] || ''}e`))
) && str + 's'
);
}

export function group(name: string, group: string | undefined) {
return group ? `${group}/${name}` : name;
}
Expand Down
22 changes: 12 additions & 10 deletions modules/entity/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function addReducerToState(options: any): Rule {
export function addReducerToStateInterface(
source: ts.SourceFile,
reducersPath: string,
options: { name: string }
options: { name: string; plural: boolean }
): Change {
const stateInterface = source.statements.find(
stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration
Expand All @@ -90,11 +90,12 @@ export function addReducerToStateInterface(
return new NoopChange();
}

const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
stringUtils.camelize(options.name) +
': from' +
stringUtils.classify(options.name) +
'.State;';
state + ': from' + stringUtils.classify(options.name) + '.State;';
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -125,7 +126,7 @@ export function addReducerToStateInterface(
export function addReducerToActionReducerMap(
source: ts.SourceFile,
reducersPath: string,
options: { name: string }
options: { name: string; plural: boolean }
): Change {
let initializer: any;
const actionReducerMap: any = source.statements
Expand All @@ -152,11 +153,12 @@ export function addReducerToActionReducerMap(

let node = actionReducerMap.initializer;

const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
stringUtils.camelize(options.name) +
': from' +
stringUtils.classify(options.name) +
'.reducer,';
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const expr = node as any;
let position;
let toInsert;
Expand Down
19 changes: 19 additions & 0 deletions modules/entity/schematics-core/utility/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.substr(1);
}

/**
Returns the plural form of a string
```javascript
'innerHTML'.pluralize() // 'InnerHTMLs'
'action_name'.pluralize() // 'actionNames'
'css-class-name'.pluralize() // 'cssClassNames'
'regex'.pluralize() // 'regexes'
'user'.pluralize() // 'users'
```
*/
export function pluralize(str: string): string {
return camelize(
[/([^aeiou])y$/, /()fe?$/, /([^aeiou]o|[sxz]|[cs]h)$/].map(
(c, i) => (str = str.replace(c, `$1${'iv'[i] || ''}e`))
) && str + 's'
);
}

export function group(name: string, group: string | undefined) {
return group ? `${group}/${name}` : name;
}
Expand Down
22 changes: 12 additions & 10 deletions modules/router-store/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function addReducerToState(options: any): Rule {
export function addReducerToStateInterface(
source: ts.SourceFile,
reducersPath: string,
options: { name: string }
options: { name: string; plural: boolean }
): Change {
const stateInterface = source.statements.find(
stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration
Expand All @@ -90,11 +90,12 @@ export function addReducerToStateInterface(
return new NoopChange();
}

const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
stringUtils.camelize(options.name) +
': from' +
stringUtils.classify(options.name) +
'.State;';
state + ': from' + stringUtils.classify(options.name) + '.State;';
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -125,7 +126,7 @@ export function addReducerToStateInterface(
export function addReducerToActionReducerMap(
source: ts.SourceFile,
reducersPath: string,
options: { name: string }
options: { name: string; plural: boolean }
): Change {
let initializer: any;
const actionReducerMap: any = source.statements
Expand All @@ -152,11 +153,12 @@ export function addReducerToActionReducerMap(

let node = actionReducerMap.initializer;

const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
stringUtils.camelize(options.name) +
': from' +
stringUtils.classify(options.name) +
'.reducer,';
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const expr = node as any;
let position;
let toInsert;
Expand Down
19 changes: 19 additions & 0 deletions modules/router-store/schematics-core/utility/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.substr(1);
}

/**
Returns the plural form of a string
```javascript
'innerHTML'.pluralize() // 'InnerHTMLs'
'action_name'.pluralize() // 'actionNames'
'css-class-name'.pluralize() // 'cssClassNames'
'regex'.pluralize() // 'regexes'
'user'.pluralize() // 'users'
```
*/
export function pluralize(str: string): string {
return camelize(
[/([^aeiou])y$/, /()fe?$/, /([^aeiou]o|[sxz]|[cs]h)$/].map(
(c, i) => (str = str.replace(c, `$1${'iv'[i] || ''}e`))
) && str + 's'
);
}

export function group(name: string, group: string | undefined) {
return group ? `${group}/${name}` : name;
}
Expand Down
22 changes: 12 additions & 10 deletions modules/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function addReducerToState(options: any): Rule {
export function addReducerToStateInterface(
source: ts.SourceFile,
reducersPath: string,
options: { name: string }
options: { name: string; plural: boolean }
): Change {
const stateInterface = source.statements.find(
stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration
Expand All @@ -90,11 +90,12 @@ export function addReducerToStateInterface(
return new NoopChange();
}

const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
stringUtils.camelize(options.name) +
': from' +
stringUtils.classify(options.name) +
'.State;';
state + ': from' + stringUtils.classify(options.name) + '.State;';
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -125,7 +126,7 @@ export function addReducerToStateInterface(
export function addReducerToActionReducerMap(
source: ts.SourceFile,
reducersPath: string,
options: { name: string }
options: { name: string; plural: boolean }
): Change {
let initializer: any;
const actionReducerMap: any = source.statements
Expand All @@ -152,11 +153,12 @@ export function addReducerToActionReducerMap(

let node = actionReducerMap.initializer;

const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
stringUtils.camelize(options.name) +
': from' +
stringUtils.classify(options.name) +
'.reducer,';
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const expr = node as any;
let position;
let toInsert;
Expand Down
19 changes: 19 additions & 0 deletions modules/schematics-core/utility/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.substr(1);
}

/**
Returns the plural form of a string
```javascript
'innerHTML'.pluralize() // 'InnerHTMLs'
'action_name'.pluralize() // 'actionNames'
'css-class-name'.pluralize() // 'cssClassNames'
'regex'.pluralize() // 'regexes'
'user'.pluralize() // 'users'
```
*/
export function pluralize(str: string): string {
return camelize(
[/([^aeiou])y$/, /()fe?$/, /([^aeiou]o|[sxz]|[cs]h)$/].map(
(c, i) => (str = str.replace(c, `$1${'iv'[i] || ''}e`))
) && str + 's'
);
}

export function group(name: string, group: string | undefined) {
return group ? `${group}/${name}` : name;
}
Expand Down
22 changes: 12 additions & 10 deletions modules/schematics/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function addReducerToState(options: any): Rule {
export function addReducerToStateInterface(
source: ts.SourceFile,
reducersPath: string,
options: { name: string }
options: { name: string; plural: boolean }
): Change {
const stateInterface = source.statements.find(
stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration
Expand All @@ -90,11 +90,12 @@ export function addReducerToStateInterface(
return new NoopChange();
}

const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
stringUtils.camelize(options.name) +
': from' +
stringUtils.classify(options.name) +
'.State;';
state + ': from' + stringUtils.classify(options.name) + '.State;';
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -125,7 +126,7 @@ export function addReducerToStateInterface(
export function addReducerToActionReducerMap(
source: ts.SourceFile,
reducersPath: string,
options: { name: string }
options: { name: string; plural: boolean }
): Change {
let initializer: any;
const actionReducerMap: any = source.statements
Expand All @@ -152,11 +153,12 @@ export function addReducerToActionReducerMap(

let node = actionReducerMap.initializer;

const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
stringUtils.camelize(options.name) +
': from' +
stringUtils.classify(options.name) +
'.reducer,';
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const expr = node as any;
let position;
let toInsert;
Expand Down
19 changes: 19 additions & 0 deletions modules/schematics/schematics-core/utility/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.substr(1);
}

/**
Returns the plural form of a string
```javascript
'innerHTML'.pluralize() // 'InnerHTMLs'
'action_name'.pluralize() // 'actionNames'
'css-class-name'.pluralize() // 'cssClassNames'
'regex'.pluralize() // 'regexes'
'user'.pluralize() // 'users'
```
*/
export function pluralize(str: string): string {
return camelize(
[/([^aeiou])y$/, /()fe?$/, /([^aeiou]o|[sxz]|[cs]h)$/].map(
(c, i) => (str = str.replace(c, `$1${'iv'[i] || ''}e`))
) && str + 's'
);
}

export function group(name: string, group: string | undefined) {
return group ? `${group}/${name}` : name;
}
Expand Down
Loading

0 comments on commit 1e49530

Please sign in to comment.