@boringnode/pluralize is a TypeScript port of the pluralization features from Doctrine Inflector.
Built-in support for:
- English (
en,english) - French (
fr,french)
Note
Changes to inflection rules (adding, removing, or modifying rules) will not be considered as breaking changes.
npm install @boringnode/pluralizeThe easiest way to use the library is with the simple functions.
import{pluralize,singularize}from'@boringnode/pluralize'pluralize('word')// 'words'pluralize('person')// 'people'pluralize('child')// 'children'singularize('words')// 'word'singularize('people')// 'person'singularize('children')// 'child'You can create an Inflector instance with a different locale.
import{Inflector}from'@boringnode/pluralize'constinflector=newInflector('fr')inflector.pluralize('cheval')// 'chevaux'inflector.pluralize('bijou')// 'bijoux'inflector.singularize('chevaux')// 'cheval'You can create an Inflector instance to add custom rules.
import{Inflector}from'@boringnode/pluralize'constinflector=newInflector().addIrregular('gex','gexes').addUninflected('pokemon').addPluralRule(/(.*)gon$/i,'$1gons').addSingularRule(/(.*)gons$/i,'$1gon')inflector.pluralize('gex')// 'gexes'inflector.pluralize('pokemon')// 'pokemon'inflector.singularize('dragons')// 'dragon'You can create and register custom language rulesets for other languages.
import{Inflector,typeLanguageRuleset}from'@boringnode/pluralize'import{pattern,Patterns,Substitutions,Transformations,}from'@boringnode/pluralize/builder'constSpanishRuleset: LanguageRuleset={getSingularRuleset: ()=>({regular: newTransformations([{pattern: pattern('es$'),replacement: ''},{pattern: pattern('s$'),replacement: ''},]),uninflected: newPatterns([pattern('lunes'),pattern('martes')]),irregular: newSubstitutions([{from: 'hombres',to: 'hombre'}]),}),getPluralRuleset: ()=>({regular: newTransformations([{pattern: pattern('[aeiou]$'),replacement: '$&s'},{pattern: pattern('$'),replacement: 'es'},]),uninflected: newPatterns([pattern('lunes'),pattern('martes')]),irregular: newSubstitutions([{from: 'hombre',to: 'hombres'}]),}),}// Register the rulesetInflector.register('es',SpanishRuleset)// Use itconstinflector=newInflector('es')inflector.pluralize('gato')// 'gatos'pluralize(word: string): string- Returns the plural form of a wordsingularize(word: string): string- Returns the singular form of a word
| Method | Description |
|---|---|
new Inflector(locale?: string) | Creates a new inflector instance (defaults to 'en') |
Inflector.register(locale, ruleset) | Registers a custom language ruleset |
pluralize(word) | Returns the plural form of a word |
singularize(word) | Returns the singular form of a word |
addIrregular(singular, plural) | Adds an irregular word mapping |
addUninflected(word) | Adds a word that doesn't change between singular and plural |
addPluralRule(pattern, replacement) | Adds a custom pluralization rule |
addSingularRule(pattern, replacement) | Adds a custom singularization rule |
For creating custom language rulesets:
| Export | Description |
|---|---|
pattern | Helper function to create a case-insensitive regex |
Patterns | A collection of patterns for uninflected words |
Transformation | Interface: { pattern: RegExp, replacement: string } |
Transformations | A collection of transformations |
Substitution | Interface: { from: string, to: string } |
Substitutions | A collection of substitutions |
Ruleset | Interface: { regular, uninflected, irregular } |