Skip to content

Repository files navigation

typescript-imagenpm-imagenpm-download-imagelicense-image

@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.

Installation

npm install @boringnode/pluralize

Usage

Simple Functions

The 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'

Using a Different Locale

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'

Custom Rules with Inflector Class

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'

Creating a Custom Language Ruleset

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'

API

Functions

  • pluralize(word: string): string - Returns the plural form of a word
  • singularize(word: string): string - Returns the singular form of a word

Inflector Class

MethodDescription
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

Builder Exports (@boringnode/pluralize/builder)

For creating custom language rulesets:

ExportDescription
patternHelper function to create a case-insensitive regex
PatternsA collection of patterns for uninflected words
TransformationInterface: { pattern: RegExp, replacement: string }
TransformationsA collection of transformations
SubstitutionInterface: { from: string, to: string }
SubstitutionsA collection of substitutions
RulesetInterface: { regular, uninflected, irregular }

About

A TypeScript library for pluralizing and singularizing words.

Resources

Stars

8 stars

Watchers

4 watching

Forks

Releases

Packages

Contributors

Languages