Simplur

Simple, versatile string pluralization

Installation

npm i simplur

Use CommonJS or ESM to import

const simplur = require ( ' simplur ' ) ;

import simplur from ' simplur ' ;

Usage

simplur is applied as an ES6 template tag. Tokens of the form " [singular|plural] " are replaced based on the values injected before (or after) them.

Simple case

General, pluralization is based on the preceeding value.

simplur ` I have ${ 1 } kitt[en|ies] ` ; simplur ` I have ${ 3 } kitt[en|ies] ` ;

Look-ahead substitution

If there's no preceeding value, the following value is used.

simplur ` There [is|are] ${ 1 } m[an|en] ` ; simplur ` There [is|are] ${ 5 } m[an|en] ` ;

Multiple tokens

You can have multiple tokens. These follow the same rules as above.

simplur ` There [is|are] ${ 1 } fox[|es] and ${ 4 } octop[us|i] ` ; simplur ` There [is|are] ${ 4 } fox[|es] and ${ 1 } octop[us|i] ` ;

Custom quantity strings

To customize the displayed quantity, provide an array of the form [quantity, text] .

simplur ` You have ${ [ 0 , ' no ' ] } chinchilla[|s] ` ; simplur ` You have ${ [ 1 , ' one lonely ' ] } chinchilla[|s] ` ; simplur ` You have ${ [ 3 , ' all the ' ] } chinchilla[|s] ` ;

You may find this more generally useful when combined with a formatting function, thusly:

function formatQuantity ( v ) { switch ( v ) { case 0 : return ' no ' ; case 1 : return ' one annoying ' ; case 2 : return ' two ' ; default : return v > 3 ? ' too many ' : v ; } } for ( let i = 0 ; i <= 4 ; i ++ ) { console . log ( simplur ` She has ${ [ i , formatQuantity ( i ) ] } br[other|ethren] ` ) ; } ⇒ She has no brethren ⇒ She has one annoying brother ⇒ She has two brethren ⇒ She has 3 brethren ⇒ She has too many brethren

Markdown generated from README.md by