Simple, versatile string pluralization
npm install simplurSimple, versatile string pluralization
simplur@4 has no API changes from version 3. The only change is it is now ESM-only. (I.e. CommonJS is no longer supported.) ESM Module FAQ.
```
npm i simplur
`javascript`
import simplur from 'simplur';
simplur is an ES6 template tag that formats pluralization tokens based on the quantities injected into the string.
Pluralization tokens have the form "[singular|plural]" and are resolved
using the first expression found to the left of each token or, if no
left-expression is available, the first expression to the right.
`javascriptI have ${1} kitt[en|ies]
simplur; // ⇨ 'I have 1 kitten'I have ${3} kitt[en|ies]
simplur; // ⇨ 'I have 3 kitties'
simplurThere [is|are] ${1} m[an|en]; // ⇨ 'There is 1 man'There [is|are] ${5} m[an|en]
simplur; // ⇨ 'There are 5 men'`
Multiple tokens and quantities are allowed. These follow the same rules as above.
`javascriptThere [is|are] ${1} fox[|es] and ${4} octop[us|i]
simplur; // ⇨ 'There is 1 fox and 4 octopi'There [is|are] ${4} fox[|es] and ${1} octop[us|i]
simplur; // ⇨ 'There are 4 foxes and 1 octopus'`
Quantity values may be customized using value of the form, [quantity, format function]. For example:
`javascript
function format(qty) {
return qty == 1 ? 'sole' : qty == 2 ? 'twin' : qty;
}
simplurHer ${[1, format]} br[other|ethren] left; // ⇨ 'Her sole brother left'Her ${[2, format]} br[other|ethren] left
simplur; // ⇨ 'Her twin brethren left'Her ${[3, format]} br[other|ethren] left
simplur; // ⇨ 'Her 3 brethren left'`
#### Hiding quantities
Quantites may be hidden by omitting the format function (i.e. just pass value in
an Array), or by returning null or undefined.
Note: _Whitespace immediately following a hidden quantity will be removed._
`javascript${[1]} gen[us|era]
simplur; // ⇨ 'genus'${[2]} gen[us|era]
simplur; // ⇨ 'genera'
function hideSingular(qty) {
return qty == 1 ? null : qty;
}
simplurDelete the ${[1, hideSingular]} cact[us|i]?; // ⇨ 'Delete the cactus?'Delete the ${[2, hideSingular]} cact[us|i]?
simplur; // ⇨ 'Delete the 2 cacti?'``
Custom
---
Markdown generated from README_js.md by 