Skip to main content

Pluralizer - Manage Word Forms

The Pluralizer class provides a low‑level API to handle English word pluralization and singularization.
It supports irregular forms, uncountable nouns, and allows you to extend or customize the behavior by adding new rules.

Acknowledgement

This class is heavily inspired by Blake Embrey’s excellent pluralize package.

  • Many of the core regular expressions, irregular word mappings, and uncountable word lists were studied and adapted from the original implementation.
  • Additional improvements, extensions, and refactoring were made to fit the needs of this project, but full credit goes to the author for the foundational work and inspiration.

Many thanks to the author for the original work and inspiration.

When to Use
  • You need multiple independent configurations.
  • You want to add or override rules without affecting the shared pluralizer instance.

✨ Features​

  • βœ… Convert words between singular and plural forms.
  • βœ… Built‑in default rules loaded on construction.
  • βœ… Manage rules dynamically:
    • addPluralRule()
    • addSingularRule()
    • addIrregular()
    • addUncountable()
  • βœ… Automatically handles irregular words (e.g. child β†’ children).
  • βœ… Detects uncountable nouns (e.g. fish).

πŸ“¦ Import​

import { Pluralizer } from 'nhb-toolbox';

πŸš€ Usage​

const myPluralizer = new Pluralizer();

myPluralizer.pluralize('child'); // "children"
myPluralizer.toSingular('geese'); // "goose"
myPluralizer.isPlural('fish'); // true (uncountable)

πŸ”§ Extending Rules​

You can modify your instance without affecting others:

// Add a custom plural rule
myPluralizer.addPluralRule(/(foo)$/i, '$1bars');

// Add a custom singular rule
myPluralizer.addSingularRule(/(bars)$/i, '$1');

// Add an uncountable
myPluralizer.addUncountable('luggage');

// Add an irregular form
myPluralizer.addIrregular('cactus', 'cacti');

πŸ› οΈ API Overview​

constructor()​

Initializes with built‑in rules, irregular forms, and uncountables.


pluralize(word, options?)​

Get the proper singular or plural form based on an optional count.

myPluralizer.pluralize('category', { count: 3 }); // "categories"
myPluralizer.pluralize('child', { count: 1, inclusive: true }); // "1 child"
PluralizeOptions​

Options you can pass to pluralize(word, options?) to control how the result is formatted.

PropertyTypeDescriptionExample
countNumericDetermines whether to use singular or plural form.
If omitted, the method always returns the plural form.
{ count: 1 or '1' } β†’ returns singular
{ count: 5 or '5' } β†’ returns plural
inclusivebooleanWhether to include the count in the returned string.
Works only if count is provided.
{ count: 3, inclusive: true } β†’ "3 categories"
{ count: 3, inclusive: false } β†’ "categories"

Example Usage​
// Count-based pluralization
myPluralizer.pluralize('category', { count: 3 });
// β†’ "categories"

// Inclusive formatting
myPluralizer.pluralize('child', { count: 1, inclusive: true });
// β†’ "1 child"

// No count provided: always plural
myPluralizer.pluralize('analysis');
// β†’ "analyses"
Tips
  • count can be a number (3) or a numeric string ("3").
  • If count is not supplied, pluralize() will always return the plural form without a count prefix.

toPlural(word)​

Convert a word to its plural form.

myPluralizer.toPlural('analysis'); // "analyses"

toSingular(word)​

Convert a word to its singular form.

myPluralizer.toSingular('geese'); // "goose"

isPlural(word)​

Check if a word is plural.

Note

Always returns true for uncountable nouns.

myPluralizer.isPlural('children'); // true

isSingular(word)​

Check if a word is singular.

Note

Always returns true for uncountable nouns.

myPluralizer.isSingular('child'); // true

addPluralRule(rule, replacement)​

Add a new pluralization rule.

myPluralizer.addPluralRule(/(quiz)$/i, '$1zes');

addSingularRule(rule, replacement)​

Add a new singularization rule.

myPluralizer.addSingularRule(/(matr)ices$/i, '$1ix');

addIrregular(single, plural)​

Add an irregular word pair.

myPluralizer.addIrregular('person', 'people');

addUncountable(word)​

Add a word or pattern that should never change.

myPluralizer.addUncountable('information');
myPluralizer.addUncountable(/pok[eΓ©]mon$/i);

See also​


Summary​

Use the Pluralizer class for custom rules and isolated configurations. Use the pluralizer instance for quick, standard use‑cases.