Pluralizer 🔄
A powerful, multilingual library for pluralizing and singularizing words
📚 Table of Contents
📖 Description
Pluralizer is a sophisticated, zero-dependency TypeScript library designed to handle word pluralization across multiple languages. Unlike simpler solutions, Pluralizer respects language-specific rules, grammatical gender, and irregular forms, ensuring linguistically correct transformations. It provides comprehensive support for English and Russian, with a flexible architecture that allows easy extension to other languages. Whether you're building an internationalized application, creating natural language interfaces, or developing educational tools, Pluralizer offers a robust solution for grammatically correct word transformations that maintains the nuances of each supported language.
🚀 Features
- ✨ Zero dependencies - lightweight and portable
- ✨ Full TypeScript support with comprehensive type definitions
- ✨ Multilingual architecture with initial support for English and Russian
- ✨ Gender-aware pluralization (especially important for Slavic languages)
- ✨ Extensive handling of irregular plural forms in all supported languages
- ✨ Automatic language detection based on character patterns
- ✨ Case preservation for maintaining proper capitalization
- ✨ Comprehensive API for checking if words are singular or plural
- ✨ Clean domain-driven design architecture for easy extensibility
- ✨ Modular design that allows adding new languages without modifying existing code
- ✨ ESM and CommonJS module support for maximum compatibility
- ✨ High test coverage with unit and end-to-end tests
🛠 Installation
npm install @elsikora/pluralizer
yarn add @elsikora/pluralizer
pnpm add @elsikora/pluralizer
bun add @elsikora/pluralizer
javascript
import { pluralizer } from '@elsikora/pluralizer';
javascript
const { pluralizer } = require('@elsikora/pluralizer');
💡 Usage
Basic Usage
The Pluralizer library provides a straightforward API for working with word pluralization in different languages. The simplest way to use it is with the default singleton instance:
import { pluralizer } from '@elsikora/pluralizer';
console.log(pluralizer.pluralize('book'));
console.log(pluralizer.pluralize('child'));
console.log(pluralizer.pluralize('box'));
console.log(pluralizer.pluralize('book', { count: 1 }));
console.log(pluralizer.pluralize('book', { count: 2 }));
Working with Multiple Languages
Pluralizer supports automatic language detection and explicit language specification:
import { pluralizer, EGender } from '@elsikora/pluralizer';
console.log(pluralizer.pluralize('книга', { language: 'ru', gender: EGender.FEMININE }));
console.log(pluralizer.pluralize('стол', { language: 'ru', gender: EGender.MASCULINE }));
console.log(pluralizer.pluralize('окно', { language: 'ru', gender: EGender.NEUTER }));
console.log(pluralizer.pluralize('книга', { gender: EGender.FEMININE }));
console.log(pluralizer.pluralize('book'));
Converting Between Singular and Plural Forms
Pluralizer provides methods to explicitly convert between singular and plural forms:
import { pluralizer, EGender } from '@elsikora/pluralizer';
console.log(pluralizer.toPlural('book'));
console.log(pluralizer.toPlural('child'));
console.log(pluralizer.toPlural('книга', { gender: EGender.FEMININE }));
console.log(pluralizer.toSingular('books'));
console.log(pluralizer.toSingular('children'));
console.log(pluralizer.toSingular('книги', 'ru'));
Checking Word Forms
Use these methods to check if a word is in singular or plural form:
import { pluralizer } from '@elsikora/pluralizer';
console.log(pluralizer.isPlural('books'));
console.log(pluralizer.isPlural('book'));
console.log(pluralizer.isPlural('children'));
console.log(pluralizer.isPlural('книги', 'ru'));
console.log(pluralizer.isSingular('book'));
console.log(pluralizer.isSingular('books'));
console.log(pluralizer.isSingular('стол', 'ru'));
Advanced Usage with Word Entity
For more control, you can create Word entities directly:
import { Word, PluralizerFactory, EGender } from '@elsikora/pluralizer';
const factory = new PluralizerFactory();
const englishPluralizer = factory.createPluralizer('en');
const word = new Word('phenomenon', { language: 'en' });
console.log(englishPluralizer.pluralize(word));
const russianPluralizer = factory.createPluralizer('ru');
const russianWord = new Word('книга', { language: 'ru', gender: EGender.FEMININE });
console.log(russianPluralizer.pluralize(russianWord));
Creating a Custom Instance
If you need a custom configuration, you can create your own Pluralizer instance:
import { Pluralizer, LanguageDetector } from '@elsikora/pluralizer';
const customPluralizer = new Pluralizer();
const languages = customPluralizer.getSupportedLanguages();
console.log(languages);
console.log(customPluralizer.supportsLanguage('en'));
console.log(customPluralizer.supportsLanguage('fr'));
const detector = new LanguageDetector('es');
detector.addLanguagePattern('fr', /[àâäæçéèêëîïôœùûüÿ]/i);
Handling Special Cases
Pluralizer includes special handling for irregular forms and uncountable words:
import { pluralizer } from '@elsikora/pluralizer';
console.log(pluralizer.toPlural('phenomenon'));
console.log(pluralizer.toPlural('criterion'));
console.log(pluralizer.toPlural('analysis'));
console.log(pluralizer.toPlural('cactus'));
console.log(pluralizer.toPlural('человек', { gender: EGender.MASCULINE }));
console.log(pluralizer.toPlural('ребёнок', { gender: EGender.MASCULINE }));
console.log(pluralizer.toPlural('equipment'));
console.log(pluralizer.toPlural('information'));
console.log(pluralizer.toPlural('ножницы', { language: 'ru' }));
Case Preservation
Pluralizer intelligently preserves the original case pattern:
import { pluralizer } from '@elsikora/pluralizer';
console.log(pluralizer.toPlural('BOOK'));
console.log(pluralizer.toPlural('CHILD'));
console.log(pluralizer.toPlural('Book'));
console.log(pluralizer.toPlural('Child'));
console.log(pluralizer.toPlural('CamelCase'));
console.log(pluralizer.toPlural('iPod'));
React/Next.js Integration Example
import { useState } from 'react';
import { pluralizer } from '@elsikora/pluralizer';
function ItemCounter() {
const [count, setCount] = useState(1);
const itemText = pluralizer.pluralize('item', { count });
return (
<div>
<button onClick={() => setCount(c => Math.max(0, c - 1))}>-</button>
<span>{count} {itemText}</span>
<button onClick={() => setCount(c => c + 1)}>+</button>
</div>
);
}
Internationalization (i18n) Integration
import { pluralizer, EGender } from '@elsikora/pluralizer';
function formatMessage(key, count, language = 'en') {
const messages = {
'item.count': {
en: `You have ${count} {item}`,
ru: `У вас ${count} {item}`
}
};
const template = messages[key][language];
const itemWord = language === 'en' ? 'item' : 'предмет';
const gender = language === 'ru' ? EGender.MASCULINE : undefined;
return template.replace('{item}', pluralizer.pluralize(itemWord, { count, language, gender }));
}
console.log(formatMessage('item.count', 1, 'en'));
console.log(formatMessage('item.count', 5, 'en'));
console.log(formatMessage('item.count', 1, 'ru'));
console.log(formatMessage('item.count', 5, 'ru'));
🛣 Roadmap
| ## Future Development | 🚧 In Progress |
| - Add support for Spanish language pluralization rules | 🚧 In Progress |
| - Expand language support to include French, German, Italian, and more | 🚧 In Progress |
| - Create specialized handling for proper nouns and names | 🚧 In Progress |
| - Develop additional detection mechanisms for ambiguous words | 🚧 In Progress |
| - Add context-aware pluralization for languages with complex number agreement | 🚧 In Progress |
| - Implement browser-specific optimizations for web applications | 🚧 In Progress |
| - Create plugins for popular frameworks (React, Vue, Angular) | 🚧 In Progress |
| - Build integrations with common i18n libraries | 🚧 In Progress |
| - Develop a web-based demo and playground | 🚧 In Progress |
| - Support for numerical words ("one" → "ones", "first" → "firsts") | 🚧 In Progress |
| - Implement count-specific forms for languages with complex countable forms | 🚧 In Progress |
| (done) Zero dependencies - lightweight and portable | 🚧 In Progress |
| (done) Full TypeScript support with comprehensive type definitions | 🚧 In Progress |
| (done) Multilingual architecture with initial support for English and Russian | 🚧 In Progress |
❓ FAQ
Frequently Asked Questions
How does Pluralizer handle words that are the same in both singular and plural forms?
Pluralizer maintains a comprehensive list of uncountable words for each supported language. Words like "sheep", "fish", "series" in English or "ножницы", "брюки" in Russian are recognized as special cases and will not be transformed.
Does Pluralizer work with proper nouns and names?
Yes, Pluralizer can handle proper nouns and names. However, because names often follow special pluralization rules or maintain their form, results may vary. We recommend testing with your specific use cases.
How accurate is the automatic language detection?
The language detector uses character pattern recognition that's highly reliable for distinguishing between Latin-based languages and Cyrillic. For languages sharing the same alphabet, explicit language specification is recommended for best results.
Can I add support for a new language?
Yes! Pluralizer is designed with extensibility in mind. You can create a new language implementation by extending the appropriate interfaces and registering it with the PluralizerFactory. The documentation includes examples for implementing new language support.
How does Pluralizer handle case sensitivity?
Pluralizer intelligently preserves the case pattern of the original word. Whether your word is lowercase, UPPERCASE, Title Case, camelCase, or a miXeD case pattern, Pluralizer will maintain that pattern in the transformed word.
Does Pluralizer support non-standard pluralization rules in technical contexts?
Yes, Pluralizer is built to handle specialized terminology. For example, it correctly transforms technical terms like "index" to "indices" and "matrix" to "matrices". If you encounter specialized terms not handled correctly, you can extend the irregular words list.
How does Pluralizer handle hyphenated or compound words?
For compound words, Pluralizer typically pluralizes the last word component. For example, "mother-in-law" becomes "mothers-in-law". However, some compound words follow special rules and are included in the irregular words lists.
Is Pluralizer suitable for large-scale applications?
Yes, Pluralizer is designed for performance and has zero dependencies, making it suitable for large-scale applications. Its modular design allows you to load only the languages you need.
🔒 License
This project is licensed under **MIT License
Copyright (c) 2025 ElsiKora
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.**.