intl-pluralrules
Advanced tools
@@ -94,3 +94,3 @@ 'use strict'; | ||
function getPluralRules(NumberFormat, getSelector, getCategories) { | ||
function getPluralRules(NumberFormat, getSelector, getCategories, getRangeSelector) { | ||
var findLocale = function findLocale(locale) { | ||
@@ -124,2 +124,3 @@ do { | ||
this._select = getSelector(this._locale); | ||
this._range = getRangeSelector(this._locale); | ||
this._type = getType(opt.type); | ||
@@ -137,3 +138,4 @@ this._nf = new NumberFormat('en', opt); // make-plural expects latin digits with . decimal separator | ||
minimumSignificantDigits = _this$_nf$resolvedOpt.minimumSignificantDigits, | ||
maximumSignificantDigits = _this$_nf$resolvedOpt.maximumSignificantDigits; | ||
maximumSignificantDigits = _this$_nf$resolvedOpt.maximumSignificantDigits, | ||
roundingPriority = _this$_nf$resolvedOpt.roundingPriority; | ||
@@ -146,2 +148,3 @@ var opt = { | ||
pluralCategories: getCategories(this._locale, this._type === 'ordinal'), | ||
roundingPriority: roundingPriority || 'auto', | ||
type: this._type | ||
@@ -168,2 +171,11 @@ }; | ||
} | ||
}, { | ||
key: "selectRange", | ||
value: function selectRange(start, end) { | ||
if (!(this instanceof PluralRules)) throw new TypeError("selectRange() called on incompatible ".concat(this)); | ||
if (typeof start !== 'number') start = Number(start); | ||
if (typeof end !== 'number') end = Number(end); | ||
if (!isFinite(start) || !isFinite(end) || start > end) throw new RangeError('start and end must be finite, with end >= start'); | ||
return this._range(this.select(start), this.select(end)); | ||
} | ||
}], [{ | ||
@@ -170,0 +182,0 @@ key: "supportedLocalesOf", |
{ | ||
"name": "intl-pluralrules", | ||
"version": "1.3.1", | ||
"version": "2.0.0-0", | ||
"description": "Intl.PluralRules polyfill", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -8,2 +8,17 @@ 'use strict'; | ||
function _mergeNamespaces(n, m) { | ||
m.forEach(function (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default' && !(k in n)) { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
}); | ||
return Object.freeze(n); | ||
} | ||
var getPluralRules__default = /*#__PURE__*/_interopDefaultLegacy(getPluralRules); | ||
@@ -734,5 +749,6 @@ var PseudoNumberFormat__default = /*#__PURE__*/_interopDefaultLegacy(PseudoNumberFormat); | ||
var P = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), plurals$1.exports, { | ||
var P = /*#__PURE__*/Object.freeze(/*#__PURE__*/_mergeNamespaces({ | ||
__proto__: null, | ||
'default': plurals | ||
})); | ||
}, [plurals$1.exports])); | ||
@@ -1139,6 +1155,143 @@ var pluralCategories$1 = {exports: {}}; | ||
var C = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), pluralCategories$1.exports, { | ||
var C = /*#__PURE__*/Object.freeze(/*#__PURE__*/_mergeNamespaces({ | ||
__proto__: null, | ||
'default': pluralCategories | ||
})); | ||
}, [pluralCategories$1.exports])); | ||
var ranges$1 = {exports: {}}; | ||
(function (module, exports) { | ||
var a = function a(start, end) { | ||
return "other"; | ||
}; | ||
var b = function b(start, end) { | ||
return start === "other" && end === "one" ? "one" : "other"; | ||
}; | ||
var c = function c(start, end) { | ||
return end || "other"; | ||
}; | ||
(function (root, pluralRanges) { | ||
Object.defineProperty(pluralRanges, '__esModule', { | ||
value: true | ||
}); | ||
module.exports = pluralRanges; | ||
})(commonjsGlobal, { | ||
af: a, | ||
ak: b, | ||
am: c, | ||
an: a, | ||
ar: function ar(start, end) { | ||
return end === "few" ? "few" : end === "many" ? "many" : start === "zero" && end === "one" ? "zero" : start === "zero" && end === "two" ? "zero" : "other"; | ||
}, | ||
as: c, | ||
az: c, | ||
be: c, | ||
bg: a, | ||
bn: c, | ||
bs: c, | ||
ca: a, | ||
cs: c, | ||
cy: c, | ||
da: c, | ||
de: c, | ||
el: c, | ||
en: a, | ||
es: a, | ||
et: a, | ||
eu: a, | ||
fa: b, | ||
fi: a, | ||
fil: c, | ||
fr: c, | ||
ga: c, | ||
gl: c, | ||
gsw: c, | ||
gu: c, | ||
he: function he(start, end) { | ||
return start === "many" && end === "many" ? "many" : start === "many" && end === "other" ? "many" : start === "one" && end === "many" ? "many" : start === "other" && end === "many" ? "many" : "other"; | ||
}, | ||
hi: c, | ||
hr: c, | ||
hu: c, | ||
hy: c, | ||
ia: a, | ||
id: a, | ||
io: a, | ||
is: c, | ||
it: c, | ||
ja: a, | ||
ka: function ka(start, end) { | ||
return start || "other"; | ||
}, | ||
kk: c, | ||
km: a, | ||
kn: c, | ||
ko: a, | ||
ky: c, | ||
lij: c, | ||
lo: a, | ||
lt: c, | ||
lv: function lv(start, end) { | ||
return end === "one" ? "one" : "other"; | ||
}, | ||
mk: a, | ||
ml: c, | ||
mn: c, | ||
mr: c, | ||
ms: a, | ||
my: a, | ||
nb: a, | ||
ne: c, | ||
nl: c, | ||
no: a, | ||
or: b, | ||
pa: c, | ||
pcm: a, | ||
pl: c, | ||
ps: c, | ||
pt: c, | ||
ro: function ro(start, end) { | ||
return end === "few" ? "few" : end === "one" ? "few" : "other"; | ||
}, | ||
ru: c, | ||
sc: c, | ||
scn: c, | ||
sd: b, | ||
si: function si(start, end) { | ||
return start === "one" && end === "one" ? "one" : "other"; | ||
}, | ||
sk: c, | ||
sl: function sl(start, end) { | ||
return end === "few" ? "few" : end === "one" ? "few" : end === "two" ? "two" : "other"; | ||
}, | ||
sq: c, | ||
sr: c, | ||
sv: a, | ||
sw: c, | ||
ta: c, | ||
te: c, | ||
th: a, | ||
tk: c, | ||
tr: c, | ||
ug: c, | ||
uk: c, | ||
ur: a, | ||
uz: c, | ||
vi: a, | ||
yue: a, | ||
zh: a, | ||
zu: c | ||
}); | ||
})(ranges$1); | ||
var ranges = /*@__PURE__*/getDefaultExportFromCjs(ranges$1.exports); | ||
var R = /*#__PURE__*/Object.freeze(/*#__PURE__*/_mergeNamespaces({ | ||
__proto__: null, | ||
'default': ranges | ||
}, [ranges$1.exports])); | ||
// using them here because with this many small functions, bundlers produce less | ||
@@ -1149,5 +1302,6 @@ // cruft than for ES module exports. | ||
var Categories = pluralCategories || C; | ||
var RangePlurals = ranges || R; | ||
/* istanbul ignore next */ | ||
var NumberFormat = (typeof Intl === "undefined" ? "undefined" : _typeof(Intl)) === 'object' && Intl.NumberFormat || PseudoNumberFormat__default['default']; // make-plural exports are cast with safe-identifier to be valid JS identifiers | ||
var NumberFormat = (typeof Intl === "undefined" ? "undefined" : _typeof(Intl)) === 'object' && Intl.NumberFormat || PseudoNumberFormat__default["default"]; // make-plural exports are cast with safe-identifier to be valid JS identifiers | ||
@@ -1166,4 +1320,8 @@ var id = function id(lc) { | ||
var PluralRules = getPluralRules__default['default'](NumberFormat, getSelector, getCategories); | ||
var getRangeSelector = function getRangeSelector(lc) { | ||
return RangePlurals[id(lc)]; | ||
}; | ||
var PluralRules = getPluralRules__default["default"](NumberFormat, getSelector, getCategories, getRangeSelector); | ||
module.exports = PluralRules; |
@@ -12,18 +12,18 @@ 'use strict'; | ||
global.Intl = { | ||
PluralRules: PluralRules__default['default'] | ||
PluralRules: PluralRules__default["default"] | ||
}; | ||
} else if (typeof window !== 'undefined') { | ||
window.Intl = { | ||
PluralRules: PluralRules__default['default'] | ||
PluralRules: PluralRules__default["default"] | ||
}; | ||
} else { | ||
this.Intl = { | ||
PluralRules: PluralRules__default['default'] | ||
PluralRules: PluralRules__default["default"] | ||
}; | ||
} | ||
PluralRules__default['default'].polyfill = true; | ||
} else if (!Intl.PluralRules) { | ||
Intl.PluralRules = PluralRules__default['default']; | ||
PluralRules__default['default'].polyfill = true; | ||
PluralRules__default["default"].polyfill = true; | ||
} else if (!Intl.PluralRules || !Intl.PluralRules.prototype.selectRange) { | ||
Intl.PluralRules = PluralRules__default["default"]; | ||
PluralRules__default["default"].polyfill = true; | ||
} else { | ||
@@ -34,5 +34,5 @@ var test = ['en', 'es', 'ru', 'zh']; | ||
if (supported.length < test.length) { | ||
Intl.PluralRules = PluralRules__default['default']; | ||
PluralRules__default['default'].polyfill = true; | ||
Intl.PluralRules = PluralRules__default["default"]; | ||
PluralRules__default["default"].polyfill = true; | ||
} | ||
} |
# intl-pluralrules | ||
A spec-compliant implementation & polyfill for [Intl.PluralRules]. Particularly | ||
useful if you need proper support for [`minimumFractionDigits`], which are only | ||
supported in Chrome 77 & later. | ||
A spec-compliant implementation & polyfill for [Intl.PluralRules], | ||
including the `selectRange(start, end)` method introduced in [Intl.NumberFormat v3]. | ||
Also useful if you need proper support for [`minimumFractionDigits`], | ||
which are only supported in Chrome 77 & later. | ||
For a polyfill without `selectRange()`, please use `intl-pluralrules@1`. | ||
[intl.pluralrules]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules | ||
[intl.numberformat v3]: https://github.com/tc39/proposal-intl-numberformat-v3/ | ||
[`minimumfractiondigits`]: https://bugs.chromium.org/p/v8/issues/detail?id=8866 | ||
@@ -25,5 +29,6 @@ | ||
If `Intl.PluralRules` already exists and supports | ||
[multiple locales](https://nodejs.org/api/intl.html), the polyfill will not be | ||
loaded. Full support for `minimumFractionDigits` is not checked. | ||
If `Intl.PluralRules` already exists, | ||
includes a `selectRange()` method, | ||
and supports [multiple locales](https://nodejs.org/api/intl.html), | ||
the polyfill will not be loaded. | ||
@@ -41,2 +46,4 @@ ## Ponyfill | ||
new PluralRules('en', { minimumSignificantDigits: 3 }).select(1) // 'other' | ||
new PluralRules('en').selectRange(0, 1) // 'other' | ||
new PluralRules('fr').selectRange(0, 1) // 'one' | ||
``` | ||
@@ -55,3 +62,3 @@ | ||
Thanks to tree-shaking, this example that only supports English and French | ||
minifies & gzips to 1619 bytes. Do note that this size difference is only | ||
minifies & gzips to 1472 bytes. Do note that this size difference is only | ||
apparent with minified production builds. | ||
@@ -63,2 +70,3 @@ | ||
import { en as enCat, fr as frCat } from 'make-plural/pluralCategories' | ||
import { en as enRange, fr as frRange } from 'make-plural/ranges' | ||
@@ -71,6 +79,10 @@ const sel = { en, fr } | ||
const range = { en: enRange, fr: frRange } | ||
const getRangeSelector = (lc) => range[lc] | ||
const PluralRules = getPluralRules( | ||
Intl.NumberFormat, // Not available in IE 10 | ||
getSelector, | ||
getCategories | ||
getCategories, | ||
getRangeSelector | ||
) | ||
@@ -80,4 +92,5 @@ export default PluralRules | ||
All arguments of `getPluralRules(NumberFormat, getSelector, getCategories)` are | ||
required. | ||
All arguments of | ||
`getPluralRules(NumberFormat, getSelector, getCategories, getRangeSelector)` | ||
are required. | ||
@@ -97,1 +110,4 @@ - `NumberFormat` should be `Intl.NumberFormat`, or a minimal implementation | ||
returns a function. | ||
- `getRangeSelector(lc)` should return a `function(start, end)` returning the | ||
plural category of the range. `start` and `end` are the plural categories of | ||
the corresponding values. |
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
59527
9.72%1711
11.25%107
17.58%1
Infinity%