Skip to content

Commit 1e49530

Browse files
santoshyadavdevbrandonroberts
authored andcommitted
feat(schematics): use plural for entity schematics reducer key (#1596)
Closes #1412
1 parent 8005131 commit 1e49530

File tree

16 files changed

+250
-71
lines changed

16 files changed

+250
-71
lines changed

modules/effects/schematics-core/utility/ngrx-utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function addReducerToState(options: any): Rule {
7979
export function addReducerToStateInterface(
8080
source: ts.SourceFile,
8181
reducersPath: string,
82-
options: { name: string }
82+
options: { name: string; plural: boolean }
8383
): Change {
8484
const stateInterface = source.statements.find(
8585
stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration
@@ -90,11 +90,12 @@ export function addReducerToStateInterface(
9090
return new NoopChange();
9191
}
9292

93+
const state = options.plural
94+
? stringUtils.pluralize(options.name)
95+
: stringUtils.camelize(options.name);
96+
9397
const keyInsert =
94-
stringUtils.camelize(options.name) +
95-
': from' +
96-
stringUtils.classify(options.name) +
97-
'.State;';
98+
state + ': from' + stringUtils.classify(options.name) + '.State;';
9899
const expr = node as any;
99100
let position;
100101
let toInsert;
@@ -125,7 +126,7 @@ export function addReducerToStateInterface(
125126
export function addReducerToActionReducerMap(
126127
source: ts.SourceFile,
127128
reducersPath: string,
128-
options: { name: string }
129+
options: { name: string; plural: boolean }
129130
): Change {
130131
let initializer: any;
131132
const actionReducerMap: any = source.statements
@@ -152,11 +153,12 @@ export function addReducerToActionReducerMap(
152153

153154
let node = actionReducerMap.initializer;
154155

156+
const state = options.plural
157+
? stringUtils.pluralize(options.name)
158+
: stringUtils.camelize(options.name);
159+
155160
const keyInsert =
156-
stringUtils.camelize(options.name) +
157-
': from' +
158-
stringUtils.classify(options.name) +
159-
'.reducer,';
161+
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
160162
const expr = node as any;
161163
let position;
162164
let toInsert;

modules/effects/schematics-core/utility/strings.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ export function capitalize(str: string): string {
110110
return str.charAt(0).toUpperCase() + str.substr(1);
111111
}
112112

113+
/**
114+
Returns the plural form of a string
115+
116+
```javascript
117+
'innerHTML'.pluralize() // 'InnerHTMLs'
118+
'action_name'.pluralize() // 'actionNames'
119+
'css-class-name'.pluralize() // 'cssClassNames'
120+
'regex'.pluralize() // 'regexes'
121+
'user'.pluralize() // 'users'
122+
```
123+
*/
124+
export function pluralize(str: string): string {
125+
return camelize(
126+
[/([^aeiou])y$/, /()fe?$/, /([^aeiou]o|[sxz]|[cs]h)$/].map(
127+
(c, i) => (str = str.replace(c, `$1${'iv'[i] || ''}e`))
128+
) && str + 's'
129+
);
130+
}
131+
113132
export function group(name: string, group: string | undefined) {
114133
return group ? `${group}/${name}` : name;
115134
}

modules/entity/schematics-core/utility/ngrx-utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function addReducerToState(options: any): Rule {
7979
export function addReducerToStateInterface(
8080
source: ts.SourceFile,
8181
reducersPath: string,
82-
options: { name: string }
82+
options: { name: string; plural: boolean }
8383
): Change {
8484
const stateInterface = source.statements.find(
8585
stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration
@@ -90,11 +90,12 @@ export function addReducerToStateInterface(
9090
return new NoopChange();
9191
}
9292

93+
const state = options.plural
94+
? stringUtils.pluralize(options.name)
95+
: stringUtils.camelize(options.name);
96+
9397
const keyInsert =
94-
stringUtils.camelize(options.name) +
95-
': from' +
96-
stringUtils.classify(options.name) +
97-
'.State;';
98+
state + ': from' + stringUtils.classify(options.name) + '.State;';
9899
const expr = node as any;
99100
let position;
100101
let toInsert;
@@ -125,7 +126,7 @@ export function addReducerToStateInterface(
125126
export function addReducerToActionReducerMap(
126127
source: ts.SourceFile,
127128
reducersPath: string,
128-
options: { name: string }
129+
options: { name: string; plural: boolean }
129130
): Change {
130131
let initializer: any;
131132
const actionReducerMap: any = source.statements
@@ -152,11 +153,12 @@ export function addReducerToActionReducerMap(
152153

153154
let node = actionReducerMap.initializer;
154155

156+
const state = options.plural
157+
? stringUtils.pluralize(options.name)
158+
: stringUtils.camelize(options.name);
159+
155160
const keyInsert =
156-
stringUtils.camelize(options.name) +
157-
': from' +
158-
stringUtils.classify(options.name) +
159-
'.reducer,';
161+
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
160162
const expr = node as any;
161163
let position;
162164
let toInsert;

modules/entity/schematics-core/utility/strings.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ export function capitalize(str: string): string {
110110
return str.charAt(0).toUpperCase() + str.substr(1);
111111
}
112112

113+
/**
114+
Returns the plural form of a string
115+
116+
```javascript
117+
'innerHTML'.pluralize() // 'InnerHTMLs'
118+
'action_name'.pluralize() // 'actionNames'
119+
'css-class-name'.pluralize() // 'cssClassNames'
120+
'regex'.pluralize() // 'regexes'
121+
'user'.pluralize() // 'users'
122+
```
123+
*/
124+
export function pluralize(str: string): string {
125+
return camelize(
126+
[/([^aeiou])y$/, /()fe?$/, /([^aeiou]o|[sxz]|[cs]h)$/].map(
127+
(c, i) => (str = str.replace(c, `$1${'iv'[i] || ''}e`))
128+
) && str + 's'
129+
);
130+
}
131+
113132
export function group(name: string, group: string | undefined) {
114133
return group ? `${group}/${name}` : name;
115134
}

modules/router-store/schematics-core/utility/ngrx-utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function addReducerToState(options: any): Rule {
7979
export function addReducerToStateInterface(
8080
source: ts.SourceFile,
8181
reducersPath: string,
82-
options: { name: string }
82+
options: { name: string; plural: boolean }
8383
): Change {
8484
const stateInterface = source.statements.find(
8585
stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration
@@ -90,11 +90,12 @@ export function addReducerToStateInterface(
9090
return new NoopChange();
9191
}
9292

93+
const state = options.plural
94+
? stringUtils.pluralize(options.name)
95+
: stringUtils.camelize(options.name);
96+
9397
const keyInsert =
94-
stringUtils.camelize(options.name) +
95-
': from' +
96-
stringUtils.classify(options.name) +
97-
'.State;';
98+
state + ': from' + stringUtils.classify(options.name) + '.State;';
9899
const expr = node as any;
99100
let position;
100101
let toInsert;
@@ -125,7 +126,7 @@ export function addReducerToStateInterface(
125126
export function addReducerToActionReducerMap(
126127
source: ts.SourceFile,
127128
reducersPath: string,
128-
options: { name: string }
129+
options: { name: string; plural: boolean }
129130
): Change {
130131
let initializer: any;
131132
const actionReducerMap: any = source.statements
@@ -152,11 +153,12 @@ export function addReducerToActionReducerMap(
152153

153154
let node = actionReducerMap.initializer;
154155

156+
const state = options.plural
157+
? stringUtils.pluralize(options.name)
158+
: stringUtils.camelize(options.name);
159+
155160
const keyInsert =
156-
stringUtils.camelize(options.name) +
157-
': from' +
158-
stringUtils.classify(options.name) +
159-
'.reducer,';
161+
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
160162
const expr = node as any;
161163
let position;
162164
let toInsert;

modules/router-store/schematics-core/utility/strings.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ export function capitalize(str: string): string {
110110
return str.charAt(0).toUpperCase() + str.substr(1);
111111
}
112112

113+
/**
114+
Returns the plural form of a string
115+
116+
```javascript
117+
'innerHTML'.pluralize() // 'InnerHTMLs'
118+
'action_name'.pluralize() // 'actionNames'
119+
'css-class-name'.pluralize() // 'cssClassNames'
120+
'regex'.pluralize() // 'regexes'
121+
'user'.pluralize() // 'users'
122+
```
123+
*/
124+
export function pluralize(str: string): string {
125+
return camelize(
126+
[/([^aeiou])y$/, /()fe?$/, /([^aeiou]o|[sxz]|[cs]h)$/].map(
127+
(c, i) => (str = str.replace(c, `$1${'iv'[i] || ''}e`))
128+
) && str + 's'
129+
);
130+
}
131+
113132
export function group(name: string, group: string | undefined) {
114133
return group ? `${group}/${name}` : name;
115134
}

modules/schematics-core/utility/ngrx-utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function addReducerToState(options: any): Rule {
7979
export function addReducerToStateInterface(
8080
source: ts.SourceFile,
8181
reducersPath: string,
82-
options: { name: string }
82+
options: { name: string; plural: boolean }
8383
): Change {
8484
const stateInterface = source.statements.find(
8585
stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration
@@ -90,11 +90,12 @@ export function addReducerToStateInterface(
9090
return new NoopChange();
9191
}
9292

93+
const state = options.plural
94+
? stringUtils.pluralize(options.name)
95+
: stringUtils.camelize(options.name);
96+
9397
const keyInsert =
94-
stringUtils.camelize(options.name) +
95-
': from' +
96-
stringUtils.classify(options.name) +
97-
'.State;';
98+
state + ': from' + stringUtils.classify(options.name) + '.State;';
9899
const expr = node as any;
99100
let position;
100101
let toInsert;
@@ -125,7 +126,7 @@ export function addReducerToStateInterface(
125126
export function addReducerToActionReducerMap(
126127
source: ts.SourceFile,
127128
reducersPath: string,
128-
options: { name: string }
129+
options: { name: string; plural: boolean }
129130
): Change {
130131
let initializer: any;
131132
const actionReducerMap: any = source.statements
@@ -152,11 +153,12 @@ export function addReducerToActionReducerMap(
152153

153154
let node = actionReducerMap.initializer;
154155

156+
const state = options.plural
157+
? stringUtils.pluralize(options.name)
158+
: stringUtils.camelize(options.name);
159+
155160
const keyInsert =
156-
stringUtils.camelize(options.name) +
157-
': from' +
158-
stringUtils.classify(options.name) +
159-
'.reducer,';
161+
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
160162
const expr = node as any;
161163
let position;
162164
let toInsert;

modules/schematics-core/utility/strings.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ export function capitalize(str: string): string {
110110
return str.charAt(0).toUpperCase() + str.substr(1);
111111
}
112112

113+
/**
114+
Returns the plural form of a string
115+
116+
```javascript
117+
'innerHTML'.pluralize() // 'InnerHTMLs'
118+
'action_name'.pluralize() // 'actionNames'
119+
'css-class-name'.pluralize() // 'cssClassNames'
120+
'regex'.pluralize() // 'regexes'
121+
'user'.pluralize() // 'users'
122+
```
123+
*/
124+
export function pluralize(str: string): string {
125+
return camelize(
126+
[/([^aeiou])y$/, /()fe?$/, /([^aeiou]o|[sxz]|[cs]h)$/].map(
127+
(c, i) => (str = str.replace(c, `$1${'iv'[i] || ''}e`))
128+
) && str + 's'
129+
);
130+
}
131+
113132
export function group(name: string, group: string | undefined) {
114133
return group ? `${group}/${name}` : name;
115134
}

modules/schematics/schematics-core/utility/ngrx-utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function addReducerToState(options: any): Rule {
7979
export function addReducerToStateInterface(
8080
source: ts.SourceFile,
8181
reducersPath: string,
82-
options: { name: string }
82+
options: { name: string; plural: boolean }
8383
): Change {
8484
const stateInterface = source.statements.find(
8585
stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration
@@ -90,11 +90,12 @@ export function addReducerToStateInterface(
9090
return new NoopChange();
9191
}
9292

93+
const state = options.plural
94+
? stringUtils.pluralize(options.name)
95+
: stringUtils.camelize(options.name);
96+
9397
const keyInsert =
94-
stringUtils.camelize(options.name) +
95-
': from' +
96-
stringUtils.classify(options.name) +
97-
'.State;';
98+
state + ': from' + stringUtils.classify(options.name) + '.State;';
9899
const expr = node as any;
99100
let position;
100101
let toInsert;
@@ -125,7 +126,7 @@ export function addReducerToStateInterface(
125126
export function addReducerToActionReducerMap(
126127
source: ts.SourceFile,
127128
reducersPath: string,
128-
options: { name: string }
129+
options: { name: string; plural: boolean }
129130
): Change {
130131
let initializer: any;
131132
const actionReducerMap: any = source.statements
@@ -152,11 +153,12 @@ export function addReducerToActionReducerMap(
152153

153154
let node = actionReducerMap.initializer;
154155

156+
const state = options.plural
157+
? stringUtils.pluralize(options.name)
158+
: stringUtils.camelize(options.name);
159+
155160
const keyInsert =
156-
stringUtils.camelize(options.name) +
157-
': from' +
158-
stringUtils.classify(options.name) +
159-
'.reducer,';
161+
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
160162
const expr = node as any;
161163
let position;
162164
let toInsert;

modules/schematics/schematics-core/utility/strings.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ export function capitalize(str: string): string {
110110
return str.charAt(0).toUpperCase() + str.substr(1);
111111
}
112112

113+
/**
114+
Returns the plural form of a string
115+
116+
```javascript
117+
'innerHTML'.pluralize() // 'InnerHTMLs'
118+
'action_name'.pluralize() // 'actionNames'
119+
'css-class-name'.pluralize() // 'cssClassNames'
120+
'regex'.pluralize() // 'regexes'
121+
'user'.pluralize() // 'users'
122+
```
123+
*/
124+
export function pluralize(str: string): string {
125+
return camelize(
126+
[/([^aeiou])y$/, /()fe?$/, /([^aeiou]o|[sxz]|[cs]h)$/].map(
127+
(c, i) => (str = str.replace(c, `$1${'iv'[i] || ''}e`))
128+
) && str + 's'
129+
);
130+
}
131+
113132
export function group(name: string, group: string | undefined) {
114133
return group ? `${group}/${name}` : name;
115134
}

0 commit comments

Comments
 (0)