-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from epaew/feature/refactor_utils_pluralize
Refactor utils/pluralize.ts
- Loading branch information
Showing
7 changed files
with
93 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import pluralize from 'pluralize'; | ||
|
||
import { Dictionaries, PluralizeRule } from './types'; | ||
export { Dictionaries, PluralizeRule }; | ||
|
||
export class Pluralize { | ||
readonly #pluralize: typeof pluralize; | ||
|
||
constructor(dictionaries?: Dictionaries) { | ||
this.#pluralize = pluralize; | ||
dictionaries && this.setDictionaries(dictionaries); | ||
} | ||
|
||
correct(name: string, rule: PluralizeRule): string { | ||
const corrector = { | ||
singular: this.#pluralize.singular, | ||
plural: this.#pluralize.plural, | ||
}; | ||
return corrector[rule](name); | ||
} | ||
|
||
isValidName(name: string, rule: PluralizeRule): boolean { | ||
const validator = { | ||
singular: this.#pluralize.isSingular, | ||
plural: this.#pluralize.isPlural, | ||
}; | ||
return validator[rule](name); | ||
} | ||
|
||
private setDictionaries({ irregular, plural, singular, uncountable }: Dictionaries) { | ||
irregular && | ||
irregular.forEach(([singular, plural]) => this.#pluralize.addIrregularRule(singular, plural)); | ||
plural && | ||
plural.forEach(([plural, singular]) => | ||
this.#pluralize.addPluralRule(new RegExp(plural), singular), | ||
); | ||
singular && | ||
singular.forEach(([singular, plural]) => | ||
this.#pluralize.addSingularRule(new RegExp(singular), plural), | ||
); | ||
uncountable && uncountable.forEach(this.#pluralize.addUncountableRule); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type Dictionaries = { | ||
irregular?: [string, string][]; | ||
plural?: [string, string][]; | ||
singular?: [string, string][]; | ||
uncountable?: string[]; | ||
}; | ||
export type PluralizeRule = 'singular' | 'plural'; |