make-plural
Unicode CLDR pluralization rules as JavaScript functions
Last updated 7 years ago by eemeli .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ npm install make-plural 
SYNC missed versions from official npm registry.

make-plural

make-plural provides JavaScript functions determining the pluralization categories of the approximately 200 languages included in the Unicode CLDR.

The categorization functions are pre-compiled, require no runtime dependencies, and should compress to about 2.5kB. In order to generate an even smaller file from a subset of all possible language (or to drop ordinal plural support), use make-plural-compiler.

Installation & Usage

npm install make-plural

The "main" export umd/plurals.js contains an UMD module that can be included with node's require or AMD's define. In a browser environment, it will populate a global object plurals. Said module contains approximately 200 functions (one per language), each taking as a first parameter the value to be classified (either a number or a string), and as an optional second parameter, a boolean that if true, applies ordinal rather than cardinal rules.

umd/pluralCategories.js has a similar structure to umd/plurals.js, but contains an array of the pluralization categories the cardinal and ordinal rules each language's pluralization function may output.

es6/plurals.js and es6/pluralCategories.js are the ES6 module equivalents of the above.

If your language isn't directly included in the plural rules, try removing any trailing parts that are separated from the stem by - or _. Note also that the capitalization of locale codes is lowercase for the language, but uppercase for the country, so for example the code for Portugese as spoken in Portugal is pt-PT.

var plurals = require('make-plural')
// { af: [Function],
//   ak: [Function],
//   am: [Function],
// snip 193 lines...
//   yo: [Function],
//   zh: [Function],
//   zu: [Function] }

plurals.en(1) // 1st param is the value
// 'one'

plurals.en(2)
// 'other'

plurals.en(2, true) // 2nd param, if true-ish, is for ordinal rules
// 'two'

console.log(plurals.en.toString())
// function (n, ord) {
//   var s = String(n).split('.'), v0 = !s[1], t0 = Number(s[0]) == n,
//       n10 = t0 && s[0].slice(-1), n100 = t0 && s[0].slice(-2);
//   if (ord) return (n10 == 1 && n100 != 11) ? 'one'
//       : (n10 == 2 && n100 != 12) ? 'two'
//       : (n10 == 3 && n100 != 13) ? 'few'
//       : 'other';
//   return (n == 1 && v0) ? 'one' : 'other';
// }

var pluralCategories = require('make-plural/umd/pluralCategories')
// { af: { cardinal: [ 'one', 'other' ], ordinal: [ 'other' ] },
//   ak: { cardinal: [ 'one', 'other' ], ordinal: [ 'other' ] },
//   am: { cardinal: [ 'one', 'other' ], ordinal: [ 'other' ] },
//   ar:
//    { cardinal: [ 'zero', 'one', 'two', 'few', 'many', 'other' ],
//      ordinal: [ 'other' ] },
// snip 255 lines...
//   zh: { cardinal: [ 'other' ], ordinal: [ 'other' ] },
//   zu: { cardinal: [ 'one', 'other' ], ordinal: [ 'other' ] } }

Current Tags

  • 8.2.0                                ...           latest (a month ago)

49 Versions

  • 8.2.0                                ...           a month ago
  • 8.1.0                                ...           a year ago
  • 8.0.0                                ...           a year ago
  • 7.5.0                                ...           a year ago
  • 7.4.0                                ...           3 years ago
  • 7.3.0                                ...           4 years ago
  • 7.2.0                                ...           4 years ago
  • 7.1.0                                ...           5 years ago
  • 7.0.0                                ...           5 years ago
  • 6.2.2                                ...           6 years ago
  • 6.2.1                                ...           7 years ago
  • 6.2.0                                ...           7 years ago
  • 6.1.0                                ...           7 years ago
  • 6.0.1                                ...           7 years ago
  • 6.0.0                                ...           7 years ago
  • 6.0.0-beta.3                                ...           7 years ago
  • 6.0.0-beta.2                                ...           7 years ago
  • 6.0.0-beta.1                                ...           7 years ago
  • 5.0.0                                ...           7 years ago
  • 5.0.0-beta.1                                ...           7 years ago
  • 4.3.0                                ...           8 years ago
  • 4.2.0                                ...           9 years ago
  • 4.1.1                                ...           9 years ago
  • 4.1.0                                ...           9 years ago
  • 4.0.1                                ...           10 years ago
  • 4.0.0                                ...           10 years ago
  • 3.0.6                                ...           10 years ago
  • 3.0.5                                ...           11 years ago
  • 3.0.4                                ...           11 years ago
  • 3.0.3                                ...           11 years ago
  • 3.0.2                                ...           11 years ago
  • 3.0.1                                ...           11 years ago
  • 3.0.0                                ...           12 years ago
  • 3.0.0-rc4                                ...           12 years ago
  • 3.0.0-rc3                                ...           12 years ago
  • 3.0.0-rc2                                ...           12 years ago
  • 3.0.0-rc1                                ...           12 years ago
  • 2.1.3                                ...           12 years ago
  • 2.1.1                                ...           12 years ago
  • 2.1.0                                ...           12 years ago
  • 2.0.0                                ...           12 years ago
  • 1.3.0                                ...           12 years ago
  • 1.2.0                                ...           12 years ago
  • 1.1.4                                ...           12 years ago
  • 1.1.3                                ...           12 years ago
  • 1.1.2                                ...           12 years ago
  • 1.1.1                                ...           12 years ago
  • 1.1.0                                ...           12 years ago
  • 1.0.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 209
Today 0
This Week 5
This Month 170
Last Day 0
Last Week 52
Last Month 1
Dependencies (0)
None
Dev Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE