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

refactor(schematics):entity schematics should update the state to plural #1596

Merged
merged 11 commits into from
Mar 12, 2019
24 changes: 14 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,13 @@ export function addReducerToStateInterface(
return new NoopChange();
}

let state = stringUtils.camelize(options.name);
if (options.plural) {
state = stringUtils.camelize(options.name) + 's';
}

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 +127,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 +154,13 @@ export function addReducerToActionReducerMap(

let node = actionReducerMap.initializer;

let state = stringUtils.camelize(options.name);
if (options.plural) {
state = stringUtils.camelize(options.name) + 's';
}

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 Pluralize form of a string

```javascript
'innerHTML'.pluralize() // 'InnerHTMLs'
'action_name'.pluralize() // 'actionNames'
'css-class-name'.pluralize() // 'cssClassNames'
'regex'.capitalize() // 'regexes'
'user'.capitalize() // '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
24 changes: 14 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,13 @@ export function addReducerToStateInterface(
return new NoopChange();
}

let state = stringUtils.camelize(options.name);
if (options.plural) {
state = stringUtils.camelize(options.name) + 's';
}

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 +127,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 +154,13 @@ export function addReducerToActionReducerMap(

let node = actionReducerMap.initializer;

let state = stringUtils.camelize(options.name);
if (options.plural) {
state = stringUtils.camelize(options.name) + 's';
}

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 Pluralize form of a string

```javascript
'innerHTML'.pluralize() // 'InnerHTMLs'
'action_name'.pluralize() // 'actionNames'
'css-class-name'.pluralize() // 'cssClassNames'
'regex'.capitalize() // 'regexes'
'user'.capitalize() // '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
24 changes: 14 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,13 @@ export function addReducerToStateInterface(
return new NoopChange();
}

let state = stringUtils.camelize(options.name);
if (options.plural) {
state = stringUtils.camelize(options.name) + 's';
}

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 +127,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 +154,13 @@ export function addReducerToActionReducerMap(

let node = actionReducerMap.initializer;

let state = stringUtils.camelize(options.name);
if (options.plural) {
state = stringUtils.camelize(options.name) + 's';
}

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 Pluralize form of a string

```javascript
'innerHTML'.pluralize() // 'InnerHTMLs'
'action_name'.pluralize() // 'actionNames'
'css-class-name'.pluralize() // 'cssClassNames'
'regex'.capitalize() // 'regexes'
'user'.capitalize() // '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
24 changes: 14 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,13 @@ export function addReducerToStateInterface(
return new NoopChange();
}

let state = stringUtils.camelize(options.name);
Copy link
Member

@brandonroberts brandonroberts Mar 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Let's use the pluralize method instead of tacking on an s.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if (options.plural) {
state = stringUtils.camelize(options.name) + 's';
}

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 +127,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 +154,13 @@ export function addReducerToActionReducerMap(

let node = actionReducerMap.initializer;

let state = stringUtils.camelize(options.name);
if (options.plural) {
state = stringUtils.camelize(options.name) + 's';
}

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 Pluralize form of a string

```javascript
'innerHTML'.pluralize() // 'InnerHTMLs'
'action_name'.pluralize() // 'actionNames'
'css-class-name'.pluralize() // 'cssClassNames'
'regex'.capitalize() // 'regexes'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'regex'.capitalize() // 'regexes'
'regex'.pluralize() // 'regexes'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

'user'.capitalize() // 'users'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'user'.capitalize() // 'users'
'user'.pluralize() // 'users'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

```
*/
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
24 changes: 14 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,13 @@ export function addReducerToStateInterface(
return new NoopChange();
}

let state = stringUtils.camelize(options.name);
if (options.plural) {
state = stringUtils.camelize(options.name) + 's';
}

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 +127,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 +154,13 @@ export function addReducerToActionReducerMap(

let node = actionReducerMap.initializer;

let state = stringUtils.camelize(options.name);
if (options.plural) {
state = stringUtils.camelize(options.name) + 's';
}

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 Pluralize form of a string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Returns the Pluralize form of a string
Returns the plural form of a string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


```javascript
'innerHTML'.pluralize() // 'InnerHTMLs'
'action_name'.pluralize() // 'actionNames'
'css-class-name'.pluralize() // 'cssClassNames'
'regex'.capitalize() // 'regexes'
'user'.capitalize() // '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