Install pluralize
masterYou can install pluralize using npm, yarn, or bower depending on your project's package manager.
npm install pluralize --save
yarn add pluralize
bower install pluralize --saverepository·master·Indexed 25 days ago
https://github.com/plurals/pluralizeA utility library for singularizing and pluralizing words based on predefined rules and customizable patterns. Version 8.0.0 provides functions to transform words based on count, force plural or singular forms, and check word states. It supports extending behavior via addPluralRule, addSingularRule, addIrregularRule, and addUncountableRule.
You can install pluralize using npm, yarn, or bower depending on your project's package manager.
npm install pluralize --save
yarn add pluralize
bower install pluralize --saveDepending on your environment, use the following methods to import the library:
Use require to import the module.
Use require within a define block.
<script> tagInclude the pluralize.js file directly in your HTML.
// Node
var pluralize = require('pluralize')
// AMD
define(function (require, exports, module) {
var pluralize = require('pluralize')
})Use isPlural and isSingular to inspect the current form of a word without transforming it.
pluralize.isPlural('test') //=> false
pluralize.isSingular('test') //=> trueYou can customize how pluralize handles specific words or patterns using the following methods:
Use addPluralRule(regex, replacement) to define a new pluralization pattern using a regular expression.
Use addSingularRule(regex, replacement) to define how a word should be singularized.
Use addIrregularRule(singular, plural) to map a specific singular word to a specific plural word (e.g., mapping 'irregular' to 'regular').
Use addUncountableRule(word) to mark a word as uncountable, meaning it will always return the same form regardless of count.
// New plural rule
pluralize.addPluralRule(/gex$/i, 'gexii')
pluralize.plural('regex') //=> "regexii"
// New singular rule
pluralize.addSingularRule(/singles$/i, 'singular')
pluralize.singular('singles') //=> "singular"
// New irregular rule
pluralize.addIrregularRule('irregular', 'regular')
pluralize.plural('irregular') //=> "regular"
// Uncountable rule
pluralize.addUncountableRule('paper')
pluralize.plural('paper') //=> "paper"The core pluralize function converts a word to its plural or singular form based on a provided count. It also supports prefixing the result with the count if requested.
Arguments:
word (string): The word to transform.count (number): The quantity of the word (determines if singular or plural form is used).inclusive (boolean): If true, prefixes the result with the number (e.g., 3 ducks).pluralize('test') //=> "tests"
pluralize('test', 0) //=> "tests"
pluralize('test', 1) //=> "test"
pluralize('test', 5) //=> "tests"
pluralize('test', 1, true) //=> "1 test"
pluralize('test', 5, true) //=> "5 tests"
pluralize('蘋果', 2, true) //=> "2 蘋果"addIrregularRule(single, plural) to define specific word pairs that do not follow standard regex rules (e.g., 'person' -> 'people').pluralize.isPlural(word) or pluralize.isSingular(word) to check the current state of a word.pluralize.singular(word) to force a word into its singular form, regardless of count.You can extend the library's behavior by adding custom rules using addPluralRule(rule, replacement).
rule: A RegExp or a string (which will be converted to a RegExp with ^ and $).replacement: A string that can use $0, $1, etc., to refer to captured groups in the rule.addUncountableRule(word) to define words that do not change between singular and plural forms. This accepts a string or a RegExp.addSingularRule(rule, replacement) to define how specific patterns should be converted back to singular forms.pluralize.plural(word) to force a word into its plural form, regardless of count.