Skip to content

Commit

Permalink
refactor(options): extract dasherize in util
Browse files Browse the repository at this point in the history
  • Loading branch information
Zlatin Stanimirov committed Mar 2, 2022
1 parent 83e6e16 commit 80cee87
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
17 changes: 5 additions & 12 deletions lib/schematics/schematic.option.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { dasherize } from '../utils/formatting';

export class SchematicOption {
constructor(private name: string, private value: boolean | string) {}

Expand All @@ -11,15 +13,15 @@ export class SchematicOption {
return `--${this.name}="${this.value}"`;
}
} else if (typeof this.value === 'boolean') {
const str = this.dasherize(this.name);
const str = dasherize(this.name);
return this.value ? `--${str}` : `--no-${str}`;
} else {
return `--${this.dasherize(this.name)}=${this.value}`;
return `--${dasherize(this.name)}=${this.value}`;
}
}

private format() {
return this.dasherize(this.value as string)
return dasherize(this.value as string)
.split('')
.reduce((content, char) => {
if (char === '(' || char === ')' || char === '[' || char === ']') {
Expand All @@ -28,13 +30,4 @@ export class SchematicOption {
return `${content}${char}`;
}, '');
}

private dasherize(str: string) {
const STRING_DASHERIZE_REGEXP = /[\s]/g;
const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g;
return str
.replace(STRING_DECAMELIZE_REGEXP, '$1-$2')
.toLowerCase()
.replace(STRING_DASHERIZE_REGEXP, '-');
}
}
8 changes: 8 additions & 0 deletions lib/utils/formatting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function dasherize(str: string) {
const STRING_DASHERIZE_REGEXP = /[\s]/g;
const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g;
return str
.replace(STRING_DECAMELIZE_REGEXP, '$1-$2')
.toLowerCase()
.replace(STRING_DASHERIZE_REGEXP, '-');
}

0 comments on commit 80cee87

Please sign in to comment.