1 comment

[ 252 ms ] story [ 67.4 ms ] thread
There are also libraries to handle this, like https://github.com/blakeembrey/pluralize that does only this, or my own https://github.com/byoung2/grandiloquent that can pluralize, in addition to a lot of other things. Here is an example from Grandiloquent:

  let quantity = 5;
  let sentence = grandiloquent
    .sentence(`${ quantity } item added to cart`);
  
  if(quantity !== 1) {
    sentence.transform('$subject', 'toPlural');
  }
   

  console.log(sentence.toString()); //5 items added to cart.