Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript pluralize an english string

In PHP, I use Kuwamoto's class to pluralize nouns in my strings. I didn't find something as good as this script in javascript except for some plugins. So, it would be great to have a javascript function based on Kuwamoto's class.

http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/

like image 417
pmrotule Avatar asked Nov 28 '14 18:11

pmrotule


People also ask

How do you pluralize foreign words?

Rules for foreign plurals of Latin-derived wordsFor words ending in -um, -um is changed to -a: addendum to addenda. For words ending in -us, -us is changed to -i: fungus to fungi. For words ending in -ex, -ex is changed to -ices: index to indices.

How do you pluralize a key?

noun, plural keys.


1 Answers

Simple version (ES6):

const maybePluralize = (count, noun, suffix = 's') =>   `${count} ${noun}${count !== 1 ? suffix : ''}`; 

Usage:

maybePluralize(0, 'turtle'); // 0 turtles maybePluralize(1, 'turtle'); // 1 turtle maybePluralize(2, 'turtle'); // 2 turtles maybePluralize(3, 'fox', 'es'); // 3 foxes 

This obviously doesn't support all english edge-cases, but it's suitable for most purposes

like image 180
Kabir Sarin Avatar answered Sep 20 '22 09:09

Kabir Sarin