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.
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.
- 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.
Property | Type | Description | Example |
---|---|---|---|
count | Numeric | Determines 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 |
inclusive | boolean | Whether 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"
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.
Always returns true
for uncountable nouns.
myPluralizer.isPlural('children'); // true
isSingular(word)
β
Check if a word is singular.
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β
- pluralizer (default instance) β shared instance of
Pluralizer
. - formatUnitWithPlural β Simple utility for formatting units with pluralization with just
"s"
.
Summaryβ
Use the Pluralizer
class for custom rules and isolated configurations.
Use the pluralizer
instance for quick, standard useβcases.