fast-plural-rules
Advanced tools
Comparing version 0.0.3 to 1.0.0
declare function getPluralFormForCardinalByLocale (locale: string, count: number): number | ||
declare function getPluralFormNameForCardinalByLocale (locale: string, count: number): string | ||
declare function getPluralFormForCardinalByIndex (index: number, count: number): number | ||
declare function getPluralFormNameForCardinalByIndex (index: number, count: number): string | ||
declare function getPluralRuleForCardinalsByLocale (locale: string): Function | ||
declare function getPluralRuleForNamedFormsForCardinalsByLocale (locale: string): Function | ||
declare function getPluralRuleForCardinalsByIndex (index: number): Function | ||
declare function getPluralRuleForNamedFormsForCardinalsByIndex (index: number): Function | ||
export { | ||
getPluralFormForCardinalByLocale, getPluralRuleForCardinalsByLocale, | ||
getPluralFormForCardinalByIndex, getPluralRuleForCardinalsByIndex | ||
getPluralFormNameForCardinalByLocale, getPluralRuleForNamedFormsForCardinalsByLocale, | ||
getPluralFormForCardinalByIndex, getPluralRuleForCardinalsByIndex, | ||
getPluralFormNameForCardinalByIndex, getPluralRuleForNamedFormsForCardinalsByIndex | ||
} | ||
export as namespace fastPluralRules |
@@ -5,73 +5,27 @@ 'use strict'; | ||
var cardinals = { | ||
const cardinals = { | ||
rules: [ | ||
function () { | ||
return 0; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : 1; | ||
}, | ||
function (n) { | ||
return n <= 1 ? 0 : 1; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 !== 0 ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n === 1 || n === 11 ? 0 : n === 2 || n === 12 ? 1 : n > 2 && n < 20 ? 2 : 3; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 2 ? 1 : n > 2 && n < 7 ? 2 : n > 6 && n < 11 ? 3 : 4; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 2 ? 1 : n % 100 >= 3 && n % 100 <= 10 ? 2 : n % 100 >= 11 ? 3 : n !== 0 ? 4 : 5; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 ? 0 : n % 10 === 2 ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : 1; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 && n % 100 !== 71 && n % 100 !== 91 ? 0 : n % 10 === 2 && n % 100 !== 12 && n % 100 !== 72 && n % 100 !== 92 ? 2 : n % 10 === 3 && n % 100 !== 13 && n % 100 !== 73 && n % 100 !== 93 ? 3 : n % 10 === 4 && n % 100 !== 14 && n % 100 !== 74 && n % 100 !== 94 ? 3 : n % 10 === 9 && n % 100 !== 19 && n % 100 !== 79 && n % 100 !== 99 ? 3 : 4; | ||
}, | ||
function (n) { | ||
return n !== 0 ? 0 : 1; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : n === 6 ? 3 : n !== 0 ? 4 : 5; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : 3; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n === 0 ? 0 : n === 1 ? 1 : 2; | ||
}], | ||
() => 0, | ||
n => n === 1 ? 0 : 1, | ||
n => n <= 1 ? 0 : 1, | ||
n => n % 10 === 1 && n % 100 !== 11 ? 1 : n % 10 !== 0 ? 2 : 0, | ||
n => n === 1 || n === 11 ? 0 : n === 2 || n === 12 ? 1 : n > 2 && n < 20 ? 2 : 3, | ||
n => n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2, | ||
n => n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 2 : 1, | ||
n => n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2, | ||
n => n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2, | ||
n => n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2, | ||
n => n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3, | ||
n => n === 1 ? 0 : n === 2 ? 1 : n > 2 && n < 7 ? 2 : n > 6 && n < 11 ? 3 : 4, | ||
n => n === 1 ? 0 : n === 2 ? 1 : n % 100 >= 3 && n % 100 <= 10 ? 2 : n % 100 >= 11 ? 3 : n !== 0 ? 4 : 5, | ||
n => n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3, | ||
n => n % 10 === 1 ? 0 : n % 10 === 2 ? 1 : 2, | ||
n => n % 10 === 1 && n % 100 !== 11 ? 0 : 1, | ||
n => n % 10 === 1 && n % 100 !== 11 && n % 100 !== 71 && n % 100 !== 91 ? 0 : n % 10 === 2 && n % 100 !== 12 && n % 100 !== 72 && n % 100 !== 92 ? 1 : (n % 10 === 3 || n % 10 === 4 || n % 10 === 9) && n % 100 !== 13 && n % 100 !== 73 && n % 100 !== 93 && n % 100 !== 14 && n % 100 !== 74 && n % 100 !== 94 && n % 100 !== 19 && n % 100 !== 79 && n % 100 !== 99 ? 2 : n !== 0 && n % 1000000 === 0 ? 3 : 4, | ||
n => n !== 0 ? 1 : 0, | ||
n => n === 1 ? 1 : n === 2 ? 2 : n === 3 ? 3 : n === 6 ? 4 : n !== 0 ? 5 : 0, | ||
n => n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2, | ||
n => n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : 3, | ||
n => n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2, | ||
n => n === 0 ? 0 : n === 1 ? 1 : 2], | ||
rulesByLocale: { | ||
@@ -222,7 +176,32 @@ ach: 2, | ||
'zh-tw': 2 | ||
} | ||
}, | ||
formsByIndex: [['other'], | ||
['one', 'other'], | ||
['one', 'other'], | ||
['zero', 'one', 'other'], | ||
['one', 'two', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'two', 'few', 'other'], | ||
['one', 'two', 'few', 'many', 'other'], | ||
['one', 'two', 'few', 'many', 'other', 'zero'], | ||
['one', 'few', 'many', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'other'], | ||
['one', 'two', 'few', 'many', 'other'], | ||
['zero', 'other'], | ||
['zero', 'one', 'two', 'few', 'many', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'two', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['zero', 'one', 'other'] | ||
] | ||
}; | ||
var rules = cardinals.rules, | ||
rulesByLocale = cardinals.rulesByLocale; | ||
const rules = cardinals.rules, | ||
rulesByLocale = cardinals.rulesByLocale, | ||
formsByIndex = cardinals.formsByIndex; | ||
function normalizeLocale(locale) { | ||
@@ -232,36 +211,69 @@ return locale.toLowerCase().replace('_', '-'); | ||
function getLanguage(locale) { | ||
var separator = locale.indexOf('-'); | ||
const separator = locale.indexOf('-'); | ||
return separator > 0 ? locale.substr(0, separator) : locale; | ||
} | ||
function getPluralRuleForCardinalsByLocale(locale) { | ||
function getPluralRuleIndexForCardinalsByLocale(locale) { | ||
locale = normalizeLocale(locale); | ||
var index = rulesByLocale[locale]; | ||
let index = rulesByLocale[locale]; | ||
if (index === undefined) { | ||
var language = getLanguage(locale); | ||
const language = getLanguage(locale); | ||
index = rulesByLocale[language]; | ||
} | ||
if (index === undefined) { | ||
throw new Error("Unrecognized locale: \"" + locale + "\"."); | ||
throw new Error(`Unrecognized locale: "${locale}".`); | ||
} | ||
return index; | ||
} | ||
function adaptPluralRuleForFormName(rule, index) { | ||
const forms = formsByIndex[index]; | ||
return function (count) { | ||
return forms[rule(count)]; | ||
}; | ||
} | ||
function getPluralRuleForCardinalsByLocale(locale) { | ||
const index = getPluralRuleIndexForCardinalsByLocale(locale); | ||
return rules[index]; | ||
} | ||
function getPluralRuleForCardinalsByIndex(index) { | ||
var rule = rules[index]; | ||
const rule = rules[index]; | ||
if (rule === undefined) { | ||
throw new Error("Invalid index: \"" + index + "\"."); | ||
throw new Error(`Invalid index: "${index}".`); | ||
} | ||
return rule; | ||
} | ||
function getPluralRuleForNamedFormsForCardinalsByLocale(locale) { | ||
const index = getPluralRuleIndexForCardinalsByLocale(locale); | ||
return adaptPluralRuleForFormName(rules[index], index); | ||
} | ||
function getPluralRuleForNamedFormsForCardinalsByIndex(index) { | ||
const rule = rules[index]; | ||
if (rule === undefined) { | ||
throw new Error(`Invalid index: "${index}".`); | ||
} | ||
return adaptPluralRuleForFormName(rules[index], index); | ||
} | ||
function getPluralFormForCardinalByLocale(locale, count) { | ||
var rule = getPluralRuleForCardinalsByLocale(locale); | ||
const rule = getPluralRuleForCardinalsByLocale(locale); | ||
return rule(count); | ||
} | ||
function getPluralFormForCardinalByIndex(index, count) { | ||
var rule = getPluralRuleForCardinalsByIndex(index); | ||
const rule = getPluralRuleForCardinalsByIndex(index); | ||
return rule(count); | ||
} | ||
function getPluralFormNameForCardinalByLocale(locale, count) { | ||
const rule = getPluralRuleForNamedFormsForCardinalsByLocale(locale); | ||
return rule(count); | ||
} | ||
function getPluralFormNameForCardinalByIndex(index, count) { | ||
const rule = getPluralRuleForNamedFormsForCardinalsByIndex(index); | ||
return rule(count); | ||
} | ||
exports.getPluralFormForCardinalByIndex = getPluralFormForCardinalByIndex; | ||
exports.getPluralFormForCardinalByLocale = getPluralFormForCardinalByLocale; | ||
exports.getPluralFormNameForCardinalByIndex = getPluralFormNameForCardinalByIndex; | ||
exports.getPluralFormNameForCardinalByLocale = getPluralFormNameForCardinalByLocale; | ||
exports.getPluralRuleForCardinalsByIndex = getPluralRuleForCardinalsByIndex; | ||
exports.getPluralRuleForCardinalsByLocale = getPluralRuleForCardinalsByLocale; | ||
exports.getPluralRuleForNamedFormsForCardinalsByIndex = getPluralRuleForNamedFormsForCardinalsByIndex; | ||
exports.getPluralRuleForNamedFormsForCardinalsByLocale = getPluralRuleForNamedFormsForCardinalsByLocale; |
@@ -1,2 +0,329 @@ | ||
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((n=n||self).fastPluralRules={})}(this,function(n){"use strict";var o=[function(){return 0},function(n){return 1===n?0:1},function(n){return n<=1?0:1},function(n){return n%10==1&&n%100!=11?0:n%10!=0?1:2},function(n){return 1===n||11===n?0:2===n||12===n?1:2<n&&n<20?2:3},function(n){return 1===n?0:0===n||0<n%100&&n%100<20?1:2},function(n){return n%10==1&&n%100!=11?0:2<=n%10&&(n%100<10||20<=n%100)?1:2},function(n){return n%10==1&&n%100!=11?0:2<=n%10&&n%10<=4&&(n%100<10||20<=n%100)?1:2},function(n){return 1===n?0:2<=n&&n<=4?1:2},function(n){return 1===n?0:2<=n%10&&n%10<=4&&(n%100<10||20<=n%100)?1:2},function(n){return n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3},function(n){return 1===n?0:2===n?1:2<n&&n<7?2:6<n&&n<11?3:4},function(n){return 1===n?0:2===n?1:3<=n%100&&n%100<=10?2:11<=n%100?3:0!==n?4:5},function(n){return 1===n?0:0===n||1<n%100&&n%100<11?1:10<n%100&&n%100<20?2:3},function(n){return n%10==1?0:n%10==2?1:2},function(n){return n%10==1&&n%100!=11?0:1},function(n){return n%10==1&&n%100!=11&&n%100!=71&&n%100!=91?0:n%10==2&&n%100!=12&&n%100!=72&&n%100!=92?2:n%10==3&&n%100!=13&&n%100!=73&&n%100!=93?3:n%10==4&&n%100!=14&&n%100!=74&&n%100!=94?3:n%10==9&&n%100!=19&&n%100!=79&&n%100!=99?3:4},function(n){return 0!==n?0:1},function(n){return 1===n?0:2===n?1:3===n?2:6===n?3:0!==n?4:5},function(n){return 1===n?0:2<=n%10&&n%10<=4&&(n%100<10||20<=n%100)?1:2},function(n){return 1===n?0:2===n?1:3===n?2:3},function(n){return n%10==1&&n%100!=11?0:2<=n%10&&n%10<=4&&(n%100<10||20<=n%100)?1:2},function(n){return 0===n?0:1===n?1:2}],i={ach:2,af:1,ak:2,am:2,an:1,anp:1,ar:12,arn:2,as:1,ast:1,ay:0,az:1,be:7,bg:1,bn:1,bo:0,br:2,brx:1,bs:7,ca:1,cgg:0,cs:8,csb:19,cy:18,da:1,de:1,doi:1,dz:0,el:1,en:1,eo:1,es:1,"es-ar":1,et:1,eu:1,fa:2,ff:1,fi:1,fil:2,fo:1,fr:2,fur:1,fy:1,ga:11,gd:4,gl:1,gu:1,gun:2,ha:1,he:1,hi:1,hne:1,hr:7,hu:1,hy:1,ia:1,id:0,is:15,it:1,ja:0,jbo:0,jv:17,ka:0,kk:1,kl:1,km:0,kn:1,ko:0,ku:1,kw:20,ky:1,lb:1,ln:2,lo:0,lt:6,lv:3,mai:1,me:21,mfe:2,mg:2,mi:2,mk:15,ml:1,mn:1,mni:1,mnk:22,mr:1,ms:0,mt:13,my:0,nah:1,nap:1,nb:1,ne:1,nl:1,nn:1,no:1,nso:1,oc:2,or:1,pa:1,pap:1,pl:9,pms:1,ps:1,pt:1,"pt-br":2,rm:1,ro:5,ru:7,rw:1,sah:0,sat:1,sco:1,sd:1,se:1,si:1,sk:8,sl:10,so:1,son:1,sq:1,sr:7,su:0,sv:1,sw:1,ta:1,te:1,tg:2,th:0,ti:2,tk:1,tr:2,tt:0,ug:0,uk:7,ur:1,uz:2,vi:0,wa:2,wo:0,yo:1,"zh-cn":0,"zh-tw":2};function t(n){n=n.toLowerCase().replace("_","-");var r,t,e=i[n];if(void 0===e){var u=0<(t=(r=n).indexOf("-"))?r.substr(0,t):r;e=i[u]}if(void 0===e)throw new Error('Unrecognized locale: "'+n+'".');return o[e]}function e(n){var r=o[n];if(void 0===r)throw new Error('Invalid index: "'+n+'".');return r}n.getPluralFormForCardinalByIndex=function(n,r){return e(n)(r)},n.getPluralFormForCardinalByLocale=function(n,r){return t(n)(r)},n.getPluralRuleForCardinalsByIndex=e,n.getPluralRuleForCardinalsByLocale=t,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory(global.fastPluralRules = {})); | ||
}(this, function (exports) { 'use strict'; | ||
var cardinals = { | ||
rules: [ | ||
function () { | ||
return 0; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : 1; | ||
}, | ||
function (n) { | ||
return n <= 1 ? 0 : 1; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 ? 1 : n % 10 !== 0 ? 2 : 0; | ||
}, | ||
function (n) { | ||
return n === 1 || n === 11 ? 0 : n === 2 || n === 12 ? 1 : n > 2 && n < 20 ? 2 : 3; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 2 : 1; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 2 ? 1 : n > 2 && n < 7 ? 2 : n > 6 && n < 11 ? 3 : 4; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 2 ? 1 : n % 100 >= 3 && n % 100 <= 10 ? 2 : n % 100 >= 11 ? 3 : n !== 0 ? 4 : 5; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 ? 0 : n % 10 === 2 ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : 1; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 && n % 100 !== 71 && n % 100 !== 91 ? 0 : n % 10 === 2 && n % 100 !== 12 && n % 100 !== 72 && n % 100 !== 92 ? 1 : (n % 10 === 3 || n % 10 === 4 || n % 10 === 9) && n % 100 !== 13 && n % 100 !== 73 && n % 100 !== 93 && n % 100 !== 14 && n % 100 !== 74 && n % 100 !== 94 && n % 100 !== 19 && n % 100 !== 79 && n % 100 !== 99 ? 2 : n !== 0 && n % 1000000 === 0 ? 3 : 4; | ||
}, | ||
function (n) { | ||
return n !== 0 ? 1 : 0; | ||
}, | ||
function (n) { | ||
return n === 1 ? 1 : n === 2 ? 2 : n === 3 ? 3 : n === 6 ? 4 : n !== 0 ? 5 : 0; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : 3; | ||
}, | ||
function (n) { | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
}, | ||
function (n) { | ||
return n === 0 ? 0 : n === 1 ? 1 : 2; | ||
}], | ||
rulesByLocale: { | ||
ach: 2, | ||
af: 1, | ||
ak: 2, | ||
am: 2, | ||
an: 1, | ||
anp: 1, | ||
ar: 12, | ||
arn: 2, | ||
as: 1, | ||
ast: 1, | ||
ay: 0, | ||
az: 1, | ||
be: 7, | ||
bg: 1, | ||
bn: 1, | ||
bo: 0, | ||
br: 2, | ||
brx: 1, | ||
bs: 7, | ||
ca: 1, | ||
cgg: 0, | ||
cs: 8, | ||
csb: 19, | ||
cy: 18, | ||
da: 1, | ||
de: 1, | ||
doi: 1, | ||
dz: 0, | ||
el: 1, | ||
en: 1, | ||
eo: 1, | ||
es: 1, | ||
'es-ar': 1, | ||
et: 1, | ||
eu: 1, | ||
fa: 2, | ||
ff: 1, | ||
fi: 1, | ||
fil: 2, | ||
fo: 1, | ||
fr: 2, | ||
fur: 1, | ||
fy: 1, | ||
ga: 11, | ||
gd: 4, | ||
gl: 1, | ||
gu: 1, | ||
gun: 2, | ||
ha: 1, | ||
he: 1, | ||
hi: 1, | ||
hne: 1, | ||
hr: 7, | ||
hu: 1, | ||
hy: 1, | ||
ia: 1, | ||
id: 0, | ||
is: 15, | ||
it: 1, | ||
ja: 0, | ||
jbo: 0, | ||
jv: 17, | ||
ka: 0, | ||
kk: 1, | ||
kl: 1, | ||
km: 0, | ||
kn: 1, | ||
ko: 0, | ||
ku: 1, | ||
kw: 20, | ||
ky: 1, | ||
lb: 1, | ||
ln: 2, | ||
lo: 0, | ||
lt: 6, | ||
lv: 3, | ||
mai: 1, | ||
me: 21, | ||
mfe: 2, | ||
mg: 2, | ||
mi: 2, | ||
mk: 15, | ||
ml: 1, | ||
mn: 1, | ||
mni: 1, | ||
mnk: 22, | ||
mr: 1, | ||
ms: 0, | ||
mt: 13, | ||
my: 0, | ||
nah: 1, | ||
nap: 1, | ||
nb: 1, | ||
ne: 1, | ||
nl: 1, | ||
nn: 1, | ||
no: 1, | ||
nso: 1, | ||
oc: 2, | ||
or: 1, | ||
pa: 1, | ||
pap: 1, | ||
pl: 9, | ||
pms: 1, | ||
ps: 1, | ||
pt: 1, | ||
'pt-br': 2, | ||
rm: 1, | ||
ro: 5, | ||
ru: 7, | ||
rw: 1, | ||
sah: 0, | ||
sat: 1, | ||
sco: 1, | ||
sd: 1, | ||
se: 1, | ||
si: 1, | ||
sk: 8, | ||
sl: 10, | ||
so: 1, | ||
son: 1, | ||
sq: 1, | ||
sr: 7, | ||
su: 0, | ||
sv: 1, | ||
sw: 1, | ||
ta: 1, | ||
te: 1, | ||
tg: 2, | ||
th: 0, | ||
ti: 2, | ||
tk: 1, | ||
tr: 2, | ||
tt: 0, | ||
ug: 0, | ||
uk: 7, | ||
ur: 1, | ||
uz: 2, | ||
vi: 0, | ||
wa: 2, | ||
wo: 0, | ||
yo: 1, | ||
'zh-cn': 0, | ||
'zh-tw': 2 | ||
}, | ||
formsByIndex: [['other'], | ||
['one', 'other'], | ||
['one', 'other'], | ||
['zero', 'one', 'other'], | ||
['one', 'two', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'two', 'few', 'other'], | ||
['one', 'two', 'few', 'many', 'other'], | ||
['one', 'two', 'few', 'many', 'other', 'zero'], | ||
['one', 'few', 'many', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'other'], | ||
['one', 'two', 'few', 'many', 'other'], | ||
['zero', 'other'], | ||
['zero', 'one', 'two', 'few', 'many', 'other'], | ||
['one', 'few', 'other'], | ||
['one', 'two', 'few', 'other'], | ||
['one', 'few', 'other'], | ||
['zero', 'one', 'other'] | ||
] | ||
}; | ||
var rules = cardinals.rules, | ||
rulesByLocale = cardinals.rulesByLocale, | ||
formsByIndex = cardinals.formsByIndex; | ||
function normalizeLocale(locale) { | ||
return locale.toLowerCase().replace('_', '-'); | ||
} | ||
function getLanguage(locale) { | ||
var separator = locale.indexOf('-'); | ||
return separator > 0 ? locale.substr(0, separator) : locale; | ||
} | ||
function getPluralRuleIndexForCardinalsByLocale(locale) { | ||
locale = normalizeLocale(locale); | ||
var index = rulesByLocale[locale]; | ||
if (index === undefined) { | ||
var language = getLanguage(locale); | ||
index = rulesByLocale[language]; | ||
} | ||
if (index === undefined) { | ||
throw new Error("Unrecognized locale: \"" + locale + "\"."); | ||
} | ||
return index; | ||
} | ||
function adaptPluralRuleForFormName(rule, index) { | ||
var forms = formsByIndex[index]; | ||
return function (count) { | ||
return forms[rule(count)]; | ||
}; | ||
} | ||
function getPluralRuleForCardinalsByLocale(locale) { | ||
var index = getPluralRuleIndexForCardinalsByLocale(locale); | ||
return rules[index]; | ||
} | ||
function getPluralRuleForCardinalsByIndex(index) { | ||
var rule = rules[index]; | ||
if (rule === undefined) { | ||
throw new Error("Invalid index: \"" + index + "\"."); | ||
} | ||
return rule; | ||
} | ||
function getPluralRuleForNamedFormsForCardinalsByLocale(locale) { | ||
var index = getPluralRuleIndexForCardinalsByLocale(locale); | ||
return adaptPluralRuleForFormName(rules[index], index); | ||
} | ||
function getPluralRuleForNamedFormsForCardinalsByIndex(index) { | ||
var rule = rules[index]; | ||
if (rule === undefined) { | ||
throw new Error("Invalid index: \"" + index + "\"."); | ||
} | ||
return adaptPluralRuleForFormName(rules[index], index); | ||
} | ||
function getPluralFormForCardinalByLocale(locale, count) { | ||
var rule = getPluralRuleForCardinalsByLocale(locale); | ||
return rule(count); | ||
} | ||
function getPluralFormForCardinalByIndex(index, count) { | ||
var rule = getPluralRuleForCardinalsByIndex(index); | ||
return rule(count); | ||
} | ||
function getPluralFormNameForCardinalByLocale(locale, count) { | ||
var rule = getPluralRuleForNamedFormsForCardinalsByLocale(locale); | ||
return rule(count); | ||
} | ||
function getPluralFormNameForCardinalByIndex(index, count) { | ||
var rule = getPluralRuleForNamedFormsForCardinalsByIndex(index); | ||
return rule(count); | ||
} | ||
exports.getPluralFormForCardinalByIndex = getPluralFormForCardinalByIndex; | ||
exports.getPluralFormForCardinalByLocale = getPluralFormForCardinalByLocale; | ||
exports.getPluralFormNameForCardinalByIndex = getPluralFormNameForCardinalByIndex; | ||
exports.getPluralFormNameForCardinalByLocale = getPluralFormNameForCardinalByLocale; | ||
exports.getPluralRuleForCardinalsByIndex = getPluralRuleForCardinalsByIndex; | ||
exports.getPluralRuleForCardinalsByLocale = getPluralRuleForCardinalsByLocale; | ||
exports.getPluralRuleForNamedFormsForCardinalsByIndex = getPluralRuleForNamedFormsForCardinalsByIndex; | ||
exports.getPluralRuleForNamedFormsForCardinalsByLocale = getPluralRuleForNamedFormsForCardinalsByLocale; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "fast-plural-rules", | ||
"version": "0.0.3", | ||
"version": "1.0.0", | ||
"description": "Evaluates locale-specific plural rules to identify the right plural form for a cardinal number, which represents an item count.", | ||
@@ -40,2 +40,3 @@ "author": { | ||
"lint:ts": "tslint -t stylish src/*.ts test/*.ts", | ||
"generate:plural-rule-definitions": "node util/generate-plural-rule-definitions", | ||
"generate:browser-tests": "node util/generate-browser-tests", | ||
@@ -50,3 +51,3 @@ "compile": "rollup -c", | ||
"coverage": "test `node --version | cut -c 2,3` -eq 10 && cat coverage/lcov.info | coveralls", | ||
"test": "run-s compile:tests check generate:browser-tests check:browser" | ||
"test": "run-s compile:tests generate:plural-rule-definitions check generate:browser-tests check:browser" | ||
}, | ||
@@ -60,2 +61,5 @@ "standard": { | ||
"jest": { | ||
"setupFilesAfterEnv": [ | ||
"jest-expect-message" | ||
], | ||
"roots": [ | ||
@@ -65,3 +69,3 @@ "test" | ||
"testMatch": [ | ||
"**/test/*.js" | ||
"**/test/*.test.js" | ||
], | ||
@@ -85,4 +89,4 @@ "coverageDirectory": "coverage", | ||
"devDependencies": { | ||
"@babel/core": "7.4.5", | ||
"@babel/preset-env": "7.4.5", | ||
"@babel/core": "7.5.5", | ||
"@babel/preset-env": "7.5.5", | ||
"babel-core": "7.0.0-bridge.0", | ||
@@ -94,28 +98,29 @@ "babel-jest": "24.8.0", | ||
"connect": "3.7.0", | ||
"coveralls": "3.0.4", | ||
"coveralls": "3.0.5", | ||
"cpy-cli": "2.0.0", | ||
"es6-promisify": "6.0.1", | ||
"eslint-config-standard": "12.0.0", | ||
"eslint-plugin-import": "2.17.3", | ||
"eslint-config-standard": "13.0.1", | ||
"eslint-plugin-import": "2.18.2", | ||
"eslint-plugin-node": "9.1.0", | ||
"eslint-plugin-promise": "4.1.1", | ||
"eslint-plugin-promise": "4.2.1", | ||
"eslint-plugin-standard": "4.0.0", | ||
"fast-glob": "2.2.7", | ||
"fs-extra": "8.0.1", | ||
"fast-glob": "3.0.4", | ||
"fs-extra": "8.1.0", | ||
"jasmine-core": "3.4.0", | ||
"jest": "24.8.0", | ||
"jest-expect-message": "1.0.2", | ||
"npm-run-all": "4.1.5", | ||
"plural-rules": "0.0.2", | ||
"puppeteer": "1.17.0", | ||
"regenerator-runtime": "0.13.2", | ||
"puppeteer": "1.19.0", | ||
"regenerator-runtime": "0.13.3", | ||
"rimraf": "2.6.3", | ||
"rollup": "1.14.4", | ||
"rollup-plugin-babel": "4.3.2", | ||
"rollup": "1.17.0", | ||
"rollup-plugin-babel": "4.3.3", | ||
"rollup-plugin-cleanup": "3.1.1", | ||
"rollup-plugin-uglify": "6.0.2", | ||
"serve-static": "1.14.1", | ||
"standard": "12.0.1", | ||
"tslint": "5.17.0", | ||
"standard": "13.1.0", | ||
"tslint": "5.18.0", | ||
"tslint-config-standard": "8.0.1", | ||
"typescript": "3.5.1" | ||
"typescript": "3.5.3" | ||
}, | ||
@@ -122,0 +127,0 @@ "keywords": [ |
@@ -16,5 +16,7 @@ # Fast Plural Rules | ||
* Tiny and [fast](./docs/speed.md#plural-form-lookup-speed) - 3 kB minified, 1 kB gzipped. Using [plain hand-coded functions](./src/cardinals.js) as [plural rules](./docs/design.md#plural-rules) to pick [plural forms](./docs/design.md#plural-forms) using [language locales](./docs/design.md#locales). | ||
* Reliable and correct - written using the [Translate Project documentation] and the [Mozilla documentation]. | ||
* Standard and documented - written using the [Translate Project documentation] and the [Mozilla documentation]. | ||
* Reliable and correct - contains the [full test suite] [comparing the actual results with the Mozilla specification], run in both Node.js and the browser. | ||
* Universal and modern - supports both plural form index (0-5) and plural form rules (`zero`, `one`, `two`, `few`, `many` and `other`) and includes declarations for [TypeScript]. | ||
If you are looking for a library using the official and regularly updated [CLDR plural rules], only just a little bigger, see [plural-rules]. | ||
If you are looking for a library compiling and executing the declarative [CLDR plural rules], see [plural-rules]. Generated programmatically for better reliability, but a little bigger and slower. | ||
@@ -47,9 +49,52 @@ ### Table of Contents | ||
// English: 0 - singular | ||
// 1 - plural | ||
// Czech: 0 - singular | ||
// 1 - plural for 2-4 items | ||
// 2 - plural for 5+ items | ||
// Returns a localized message for the specified locale and cardinal. | ||
localizeMessage('en', 'fileCount', 3) // Returns "3 files" | ||
localizeMessage('cs', 'fileCount', 3) // Returns "3 soubory" | ||
// Returns a localized message for the specified locale and cardinal. | ||
function localizeMessage (locale, messageKey, cardinal) { | ||
const pluralForm = getPluralFormForCardinalByLocale(locale, cardinal) | ||
const messageFormat = messages[locale][messageKey][pluralForm] | ||
return messageFormat.replace('{0}', cardinal) | ||
} | ||
// A language pack with a testing message. | ||
const messages = { | ||
en: { | ||
fileCount: [ | ||
"{0} file", // 0 - singular | ||
"{0} files" // 1 - plural | ||
], | ||
} | ||
cs: { | ||
fileCount: [ | ||
"{0} soubor", // 0 - singular | ||
"{0} soubory", // 1 - plural for 2-4 items | ||
"{0} souborů" // 2 - plural for 5+ items | ||
] | ||
} | ||
} | ||
``` | ||
There is another [full example using plural form names](./docs/design.md#using-form-names) instead of numeric indexes like this: | ||
```js | ||
// Localized messages organized by locales and message keys. | ||
const messages = { | ||
en: { | ||
fileCount: { | ||
one: '{0} file', // singular | ||
other: '{0} files' // plural | ||
} | ||
}, | ||
cs: { | ||
fileCount: { | ||
one: '{0} soubor', // singular | ||
few: '{0} soubory', // plural for 2-4 items | ||
other: '{0} souborů' // plural for 5 and more items | ||
} | ||
} | ||
} | ||
``` | ||
## Installation and Getting Started | ||
@@ -85,2 +130,4 @@ | ||
* 2019-07-31 v1.0.0 Correct rules for Baltic, Celtic, Semitic, Shuar and Welsh | ||
language families to match the latest Mozilla documentation, support CLDR plural form names, complete the test suite | ||
* 2018-10-28 v0.0.1 Initial release | ||
@@ -97,2 +144,5 @@ | ||
[Yarn]: https://yarnpkg.com/ | ||
[TypeScript]: https://www.typescriptlang.org/ | ||
[full test suite]: https://travis-ci.org/prantlf/fast-plural-rules | ||
[comparing the actual results with the Mozilla specification]: https://github.com/prantlf/fast-plural-rules/blob/master/test/rules.test.js | ||
[Translate Project documentation]: http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html#pluralforms-list | ||
@@ -102,3 +152,3 @@ [Mozilla documentation]: https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_and_Plurals#List_of_Plural_Rules | ||
[relativeTime plugin]: https://github.com/prantlf/dayjs/blob/combined/docs/en/Plugin.md#relativetime | ||
[CLDR plural rules]: http://cldr.unicode.org/index/cldr-spec/plural-rules | ||
[CLDR plural rules]: http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html | ||
[plural-rules]: https://github.com/prantlf/plural-rules |
// See the plural rules at https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_and_Plurals#List_of_Plural_Rules | ||
// See the expressions to identify the plural form at http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html#pluralforms-list | ||
// and at https://hg.mozilla.org/releases/mozilla-release/file/837bbcb850cd58eb07c7f6437078d5229986967c/intl/locale/PluralForm.jsm (resource://gre/modules/PluralForm.jsm) | ||
@@ -17,2 +18,3 @@ /* eslint-disable no-confusing-arrow, no-nested-ternary */ | ||
// vi (Vietnamese), wo (Wolof), zh-CN (Simplified Chinese) | ||
// Forms: other | ||
// everything: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, | ||
@@ -22,2 +24,3 @@ // 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, | ||
() => 0, | ||
// Plural rule #1 (2 forms) | ||
@@ -27,3 +30,3 @@ // Families: Germanic (Danish, Dutch, English, Faroese, Frisian, German, | ||
// Language isolate (Basque), Latin/Greek (Greek), Semitic (Hebrew), | ||
// Romanic (Italian, Portuguese, Spanish, Catalan), Vietnamese, | ||
// Romanic (Italian, Portuguese, Spanish, Catalan), Vietnamese | ||
// Locales: af (Afrikaans), an (Aragonese), anp (Angika), as (Assamese), | ||
@@ -49,2 +52,3 @@ // ast (Asturian), az (Azerbaijani), bg (Bulgarian), bn (Bengali), | ||
// ur (Urdu), yo (Yoruba) | ||
// Forms: one, other | ||
// is 1: 1 | ||
@@ -55,2 +59,3 @@ // everything else: 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | ||
n => n === 1 ? 0 : 1, | ||
// Plural rule #2 (2 forms) | ||
@@ -63,2 +68,3 @@ // Families: Romanic (French, Brazilian Portuguese) | ||
// tr (Turkish), uz (Uzbek), wa (Walloon), zh-TW (Traditional Chinese) | ||
// Forms: one, other | ||
// is 0 or 1: 0, 1 | ||
@@ -69,5 +75,8 @@ // everything else: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, | ||
n => n <= 1 ? 0 : 1, | ||
// Plural rule #3 (3 forms) | ||
// Families: Baltic (Latvian, Latgalian) | ||
// Locales: lv (Latvian) | ||
// Forms: zero, one, other | ||
// ends in 0: 0 | ||
// ends in 1, excluding 11: 1, 21, 31, 41, 51, 61, 71, 81, 91, 101, 121, 131, | ||
@@ -79,7 +88,8 @@ // 141, 151, 161, 171, 181, 191, 201, 221, 231, 241, 251, 261, 271, 281, 291, | ||
// 43, 44, 45, 46, 47, 48, 49, 52, 53, ... | ||
// ends in 0: 0 | ||
n => n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 !== 0 ? 1 : 2, | ||
n => n % 10 === 1 && n % 100 !== 11 ? 1 : n % 10 !== 0 ? 2 : 0, | ||
// Plural rule #4 (4 forms) | ||
// Families: Celtic (Scottish Gaelic) | ||
// Locales: gd (Scottish Gaelic) | ||
// Forms: one, two, few, other | ||
// is 1 or 11: 1, 11 | ||
@@ -92,5 +102,7 @@ // is 2 or 12: 2, 12 | ||
n => n === 1 || n === 11 ? 0 : n === 2 || n === 12 ? 1 : n > 2 && n < 20 ? 2 : 3, | ||
// Plural rule #5 (3 forms) | ||
// Families: Romanic (Romanian) | ||
// Locales: ro (Romanian) | ||
// Forms: one, few, other | ||
// is 1: 1 | ||
@@ -105,5 +117,7 @@ // is 0 or ends in 01-19, excluding 1: 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, | ||
n => n === 1 ? 0 : n === 0 || (n % 100 > 0 && n % 100 < 20) ? 1 : 2, | ||
// Plural rule #6 (3 forms) | ||
// Families: Baltic (Lithuanian) | ||
// Locales: lt (Lithuanian) | ||
// Forms: one, few, other | ||
// ends in 1, excluding 11: 1, 21, 31, 41, 51, 61, 71, 81, 91, 101, 121, 131, | ||
@@ -120,3 +134,4 @@ // 141, 151, 161, 171, 181, 191, 201, 221, 231, 241, 251, 261, 271, 281, 291, | ||
n => n % 10 === 1 && n % 100 !== 11 ? 0 | ||
: n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2, | ||
: n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 2 : 1, | ||
// Plural rule #7 (3 forms) | ||
@@ -126,2 +141,3 @@ // Families: Slavic (Belarusian, Bosnian, Croatian, Serbian, Russian, Ukrainian) | ||
// sr (Serbian), uk (Ukrainian) | ||
// Forms: one, few, other | ||
// ends in 1, excluding 11: 1, 21, 31, 41, 51, 61, 71, 81, 91, 101, 121, 131, | ||
@@ -140,5 +156,7 @@ // 141, 151, 161, 171, 181, 191, 201, 221, 231, 241, 251, 261, 271, 281, 291, | ||
: n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2, | ||
// Plural rule #8 (3 forms) | ||
// Families: Slavic (Slovak, Czech) | ||
// Locales: cs (Czech), sk (Slovak) | ||
// Forms: one, few, other | ||
// is 1: 1 | ||
@@ -150,5 +168,7 @@ // is 2-4: 2, 3, 4 | ||
n => n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2, | ||
// Plural rule #9 (3 forms) | ||
// Families: Slavic (Polish) | ||
// Locales: pl (Polish) | ||
// Forms: one, few, other | ||
// is 1: 1 | ||
@@ -164,5 +184,7 @@ // ends in 2-4, excluding 12-14: 2, 3, 4, 22, 23, 24, 32, 33, 34, 42, 43, 44, | ||
: n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2, | ||
// Plural rule #10 (4 forms) | ||
// Families: Slavic (Slovenian, Sorbian) | ||
// Locales: sl (Slovenian) | ||
// Forms: one, two, few, other | ||
// ends in 01: 1, 101, 201, ... | ||
@@ -175,5 +197,7 @@ // ends in 02: 2, 102, 202, ... | ||
n => n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3, | ||
// Plural rule #11 (5 forms) | ||
// Families: Celtic (Irish Gaelic) | ||
// Locales: ga (Irish) | ||
// Forms: one, two, few, many, other | ||
// is 1: 1 | ||
@@ -187,5 +211,7 @@ // is 2: 2 | ||
n => n === 1 ? 0 : n === 2 ? 1 : n > 2 && n < 7 ? 2 : n > 6 && n < 11 ? 3 : 4, | ||
// Plural rule #12 (6 forms) | ||
// Families: Semitic (Arabic) | ||
// Locales: ar (Arabic) | ||
// Forms: one, two, few, many, other, zero | ||
// is 1: 1 | ||
@@ -203,5 +229,7 @@ // is 2: 2 | ||
: n % 100 >= 11 ? 3 : n !== 0 ? 4 : 5, | ||
// Plural rule #13 (4 forms) | ||
// Families: Semitic (Maltese) | ||
// Locales: mt (Maltese) | ||
// Forms: one, few, many, other | ||
// is 1: 1 | ||
@@ -216,6 +244,8 @@ // is 0 or ends in 01-10, excluding 1: 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 101, | ||
// 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, ... | ||
n => n === 1 ? 0 : n === 0 || (n % 100 > 1 && n % 100 < 11) ? 1 | ||
n => n === 1 ? 0 : n === 0 || (n % 100 > 0 && n % 100 < 11) ? 1 | ||
: n % 100 > 10 && n % 100 < 20 ? 2 : 3, | ||
// Plural rule #14 (3 forms) | ||
// Families: unused | ||
// Families: Unused | ||
// Forms: one, few, other | ||
// ends in 1: 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 121, 131, 141, | ||
@@ -231,5 +261,7 @@ // 151, 161, 171, 181, 191, 201, 211, 221, 231, 241, 251, 261, 271, 281, 291, | ||
n => n % 10 === 1 ? 0 : n % 10 === 2 ? 1 : 2, | ||
// Plural rule #15 (2 forms) | ||
// Families: Icelandic, Macedonian | ||
// Locales: is (Icelandic), mk (Macedonian) | ||
// Forms: one, other | ||
// ends in 1, excluding 11: 1, 21, 31, 41, 51, 61, 71, 81, 91, 101, 121, 131, | ||
@@ -242,4 +274,6 @@ // 141, 151, 161, 171, 181, 191, 201, 221, 231, 241, 251, 261, 271, 281, 291, | ||
n => n % 10 === 1 && n % 100 !== 11 ? 0 : 1, | ||
// Plural rule #16 (5 forms) | ||
// Families: Celtic (Breton) | ||
// Forms: one, two, few, many, other | ||
// ends in 1, excluding 11, 71, 91: 21, 31, 41, 51, 61, 81, 101, 121, 131, | ||
@@ -251,3 +285,3 @@ // 141, 151, 161, 181, 201, 221, 231, 241, 251, 261, 281, ... | ||
// 23, 24, 29, 33, 34, 39, 43, 44, 49, 53, 54, 59, ... | ||
// ends in 1000000: 1000000: 1000000, 2000000, 3000000, 4000000, 5000000, | ||
// ends in 000000: 1000000, 2000000, 3000000, 4000000, 5000000, | ||
// 6000000, 7000000, 8000000, 9000000, 10000000, ... | ||
@@ -257,18 +291,25 @@ // everything else: 0, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, | ||
n => n % 10 === 1 && n % 100 !== 11 && n % 100 !== 71 && n % 100 !== 91 ? 0 | ||
: n % 10 === 2 && n % 100 !== 12 && n % 100 !== 72 && n % 100 !== 92 ? 2 | ||
: n % 10 === 3 && n % 100 !== 13 && n % 100 !== 73 && n % 100 !== 93 ? 3 | ||
: n % 10 === 4 && n % 100 !== 14 && n % 100 !== 74 && n % 100 !== 94 ? 3 | ||
: n % 10 === 9 && n % 100 !== 19 && n % 100 !== 79 && n % 100 !== 99 ? 3 | ||
: 4, | ||
: n % 10 === 2 && n % 100 !== 12 && n % 100 !== 72 && n % 100 !== 92 ? 1 | ||
: (n % 10 === 3 || n % 10 === 4 || n % 10 === 9) && | ||
n % 100 !== 13 && n % 100 !== 73 && n % 100 !== 93 && | ||
n % 100 !== 14 && n % 100 !== 74 && n % 100 !== 94 && | ||
n % 100 !== 19 && n % 100 !== 79 && n % 100 !== 99 ? 2 | ||
: n !== 0 && n % 1000000 === 0 ? 3 | ||
: 4, | ||
// Plural rule #17 (2 forms) | ||
// Families: Ecuador indigenous languages (Shuar) | ||
// Locales: jv (Javanese) | ||
// Forms: zero, other | ||
// is 0: 0 | ||
// everything else: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | ||
// 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, | ||
// 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, ... | ||
// is 0: 0 | ||
n => n !== 0 ? 0 : 1, | ||
n => n !== 0 ? 1 : 0, | ||
// Plural rule #18 (6 forms) | ||
// Families: Welsh | ||
// Locales: cy (Welsh) | ||
// Forms: zero, one, two, few, many, other | ||
// is 0: 0 | ||
// is 1: 1 | ||
@@ -281,6 +322,7 @@ // is 2: 2 | ||
// 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, ... | ||
// is 0: 0 | ||
n => n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : n === 6 ? 3 : n !== 0 ? 4 : 5, | ||
n => n === 1 ? 1 : n === 2 ? 2 : n === 3 ? 3 : n === 6 ? 4 : n !== 0 ? 5 : 0, | ||
// Plural rule #19 (3 forms) | ||
// Locales: csb (Kashubian) | ||
// Forms: one, few, other | ||
// is 1: 1 | ||
@@ -297,4 +339,6 @@ // ends in 2-4, excluding 12-14: 2, 3, 4, 22, 23, 24, 32, 33, 34, 42, 43, 44, | ||
: n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2, | ||
// Plural rule #20 (4 forms) | ||
// Locales: kw (Cornish) | ||
// Forms: one, two, few, other | ||
// is 1: 1 | ||
@@ -307,4 +351,6 @@ // is 2: 2 | ||
n => n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : 3, | ||
// Plural rule #21 (3 forms) | ||
// Locales: me (Montenegro) | ||
// Forms: one, few, other | ||
// ends in 1, excluding 11: 1, 21, 31, 41, 51, 61, 71, 81, 91, 101, 121, 131, | ||
@@ -323,4 +369,6 @@ // 141, 151, 161, 171, 181, 191, 201, 221, 231, 241, 251, 261, 271, 281, 291, | ||
: n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2, | ||
// Plural rule #22 (3 forms) | ||
// Locales: mnk (Mandinka) | ||
// Forms: zero, one, other | ||
// is 0: 0 | ||
@@ -333,2 +381,3 @@ // is 1: 1 | ||
], | ||
rulesByLocale: { | ||
@@ -479,5 +528,31 @@ ach: 2, // Acholi | ||
'zh-tw': 2 // Traditional Chinese | ||
} | ||
}, | ||
formsByIndex: [ | ||
['other'], // 0: Asian | ||
['one', 'other'], // 1: Germanic | ||
['one', 'other'], // 2: Romanic (French) | ||
['zero', 'one', 'other'], // 3: Baltic (Latvian, Latgalian) | ||
['one', 'two', 'few', 'other'], // 4: Celtic (Scottish Gaelic) | ||
['one', 'few', 'other'], // 5: Romanic (Romanian) | ||
['one', 'few', 'other'], // 6: Baltic (Lithuanian) | ||
['one', 'few', 'other'], // 7: Slavic (Russian) | ||
['one', 'few', 'other'], // 8: Slavic (Slovak, Czech) | ||
['one', 'few', 'other'], // 9: Slavic (Polish) | ||
['one', 'two', 'few', 'other'], // 10: Slavic (Slovenian, Sorbian) | ||
['one', 'two', 'few', 'many', 'other'], // 11: Celtic (Irish Gaelic) | ||
['one', 'two', 'few', 'many', 'other', 'zero'], // 12: Semitic (Arabic) | ||
['one', 'few', 'many', 'other'], // 13: Semitic (Maltese) | ||
['one', 'few', 'other'], // 14: Unused | ||
['one', 'other'], // 15: Icelandic, Macedonian | ||
['one', 'two', 'few', 'many', 'other'], // 16: Celtic (Breton) | ||
['zero', 'other'], // 17: Ecuador (Shuar) | ||
['zero', 'one', 'two', 'few', 'many', 'other'], // 18: Welsh | ||
['one', 'few', 'other'], // 19: csb (Kashubian) | ||
['one', 'two', 'few', 'other'], // 20: kw (Cornish) | ||
['one', 'few', 'other'], // 21: me (Montenegro) | ||
['zero', 'one', 'other'] // 22: mnk (Mandinka) | ||
] | ||
} | ||
export default cardinals |
declare function getPluralFormForCardinalByLocale (locale: string, count: number): number | ||
declare function getPluralFormNameForCardinalByLocale (locale: string, count: number): string | ||
declare function getPluralFormForCardinalByIndex (index: number, count: number): number | ||
declare function getPluralFormNameForCardinalByIndex (index: number, count: number): string | ||
declare function getPluralRuleForCardinalsByLocale (locale: string): Function | ||
declare function getPluralRuleForNamedFormsForCardinalsByLocale (locale: string): Function | ||
declare function getPluralRuleForCardinalsByIndex (index: number): Function | ||
declare function getPluralRuleForNamedFormsForCardinalsByIndex (index: number): Function | ||
export { | ||
getPluralFormForCardinalByLocale, getPluralRuleForCardinalsByLocale, | ||
getPluralFormForCardinalByIndex, getPluralRuleForCardinalsByIndex | ||
getPluralFormNameForCardinalByLocale, getPluralRuleForNamedFormsForCardinalsByLocale, | ||
getPluralFormForCardinalByIndex, getPluralRuleForCardinalsByIndex, | ||
getPluralFormNameForCardinalByIndex, getPluralRuleForNamedFormsForCardinalsByIndex | ||
} | ||
export as namespace fastPluralRules |
@@ -5,3 +5,3 @@ 'use strict' | ||
const { rules, rulesByLocale } = cardinals | ||
const { rules, rulesByLocale, formsByIndex } = cardinals | ||
@@ -17,3 +17,3 @@ function normalizeLocale (locale) { | ||
function getPluralRuleForCardinalsByLocale (locale) { | ||
function getPluralRuleIndexForCardinalsByLocale (locale) { | ||
locale = normalizeLocale(locale) | ||
@@ -28,2 +28,14 @@ let index = rulesByLocale[locale] | ||
} | ||
return index | ||
} | ||
function adaptPluralRuleForFormName (rule, index) { | ||
const forms = formsByIndex[index] | ||
return function (count) { | ||
return forms[rule(count)] | ||
} | ||
} | ||
function getPluralRuleForCardinalsByLocale (locale) { | ||
const index = getPluralRuleIndexForCardinalsByLocale(locale) | ||
return rules[index] | ||
@@ -40,2 +52,15 @@ } | ||
function getPluralRuleForNamedFormsForCardinalsByLocale (locale) { | ||
const index = getPluralRuleIndexForCardinalsByLocale(locale) | ||
return adaptPluralRuleForFormName(rules[index], index) | ||
} | ||
function getPluralRuleForNamedFormsForCardinalsByIndex (index) { | ||
const rule = rules[index] | ||
if (rule === undefined) { | ||
throw new Error(`Invalid index: "${index}".`) | ||
} | ||
return adaptPluralRuleForFormName(rules[index], index) | ||
} | ||
function getPluralFormForCardinalByLocale (locale, count) { | ||
@@ -51,5 +76,17 @@ const rule = getPluralRuleForCardinalsByLocale(locale) | ||
function getPluralFormNameForCardinalByLocale (locale, count) { | ||
const rule = getPluralRuleForNamedFormsForCardinalsByLocale(locale) | ||
return rule(count) | ||
} | ||
function getPluralFormNameForCardinalByIndex (index, count) { | ||
const rule = getPluralRuleForNamedFormsForCardinalsByIndex(index) | ||
return rule(count) | ||
} | ||
export { | ||
getPluralRuleForCardinalsByLocale, getPluralFormForCardinalByLocale, | ||
getPluralRuleForCardinalsByIndex, getPluralFormForCardinalByIndex | ||
getPluralRuleForCardinalsByLocale, getPluralRuleForNamedFormsForCardinalsByLocale, | ||
getPluralFormForCardinalByLocale, getPluralFormNameForCardinalByLocale, | ||
getPluralRuleForCardinalsByIndex, getPluralRuleForNamedFormsForCardinalsByIndex, | ||
getPluralFormForCardinalByIndex, getPluralFormNameForCardinalByIndex | ||
} |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
131491
12
1219
0
150
35