From 93cc416be6f91f726f2cfe9b4c9b7347b3403cbf Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Sat, 3 Sep 2022 20:39:05 +0300 Subject: [PATCH] feat!: Drop intl-pluralrules/pseudo-number-format (#30) --- README.md | 7 ++- package.json | 2 - rollup.config.js | 12 +---- src/plural-rules.mjs | 7 +-- src/pseudo-number-format.mjs | 70 ----------------------------- test/pseudo-numberformat.test.mjs | 75 ------------------------------- 6 files changed, 5 insertions(+), 168 deletions(-) delete mode 100644 src/pseudo-number-format.mjs delete mode 100644 test/pseudo-numberformat.test.mjs diff --git a/README.md b/README.md index 8b7eda7..74a7c67 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ const cat = { en: enCat, fr: frCat } const getCategories = (lc, ord) => cat[lc][ord ? 'ordinal' : 'cardinal'] const range = { en: enRange, fr: frRange } -const getRangeSelector = (lc) => range[lc] +const getRangeSelector = lc => range[lc] const PluralRules = getPluralRules( Intl.NumberFormat, // Not available in IE 10 @@ -88,9 +88,8 @@ All arguments of `getPluralRules(NumberFormat, getSelector, getCategories, getRangeSelector)` are required. -- `NumberFormat` should be `Intl.NumberFormat`, or a minimal implementation - such as the one available at `intl-pluralrules/pseudo-number-format`. It - should at least support the `"en"` locale and all of the min/max digit count +- `NumberFormat` should be `Intl.NumberFormat`, or an implementation which + supports at least the `"en"` locale and all of the min/max digit count options. - `getSelector(lc)` should return a `function(n, ord)` returning the plural category of `n`, using cardinal plural rules (by default), or ordinal rules if diff --git a/package.json b/package.json index 19f5831..762e39d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,6 @@ "files": [ "factory.*", "plural-rules.*", - "pseudo-number-format.*", "polyfill.*" ], "type": "commonjs", @@ -32,7 +31,6 @@ ], "./plural-rules": "./plural-rules.js", "./polyfill": "./polyfill.js", - "./pseudo-number-format": "./pseudo-number-format.js", "./package.json": "./package.json" }, "browser": { diff --git a/rollup.config.js b/rollup.config.js index 1dd845d..0e6e90a 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -22,8 +22,7 @@ export default [ }, { input: 'src/plural-rules.mjs', - context: 'this', - external: ['./factory.mjs', './pseudo-number-format.mjs'], + external: ['./factory.mjs'], output: { file: 'plural-rules.js', format: 'cjs', @@ -46,14 +45,5 @@ export default [ paths: id => id.replace(/^.*\/([^/]+)\.mjs$/, './$1.js') }, plugins: [babel({ babelHelpers: 'bundled' })] - }, - { - input: 'src/pseudo-number-format.mjs', - output: { - file: 'pseudo-number-format.js', - format: 'cjs', - exports: 'default' - }, - plugins: [babel({ babelHelpers: 'bundled' })] } ] diff --git a/src/plural-rules.mjs b/src/plural-rules.mjs index c4f5ed1..448cee6 100644 --- a/src/plural-rules.mjs +++ b/src/plural-rules.mjs @@ -3,7 +3,6 @@ import * as C from 'make-plural/pluralCategories' import * as R from 'make-plural/ranges' import getPluralRules from './factory.mjs' -import PseudoNumberFormat from './pseudo-number-format.mjs' // In a .mjs context, CommonJS imports only expose the default endpoint. We're // using them here because with this many small functions, bundlers produce less @@ -12,10 +11,6 @@ const Plurals = P.default || P const Categories = C.default || C const RangePlurals = R.default || R -const NumberFormat = - /* c8 ignore next */ - (typeof Intl === 'object' && Intl.NumberFormat) || PseudoNumberFormat - // make-plural exports are cast with safe-identifier to be valid JS identifiers const id = lc => (lc === 'pt-PT' ? 'pt_PT' : lc) @@ -25,7 +20,7 @@ const getCategories = (lc, ord) => const getRangeSelector = lc => RangePlurals[id(lc)] const PluralRules = getPluralRules( - NumberFormat, + Intl.NumberFormat, getSelector, getCategories, getRangeSelector diff --git a/src/pseudo-number-format.mjs b/src/pseudo-number-format.mjs deleted file mode 100644 index a32a1fa..0000000 --- a/src/pseudo-number-format.mjs +++ /dev/null @@ -1,70 +0,0 @@ -export default class PseudoNumberFormat { - #minID - #minFD - #maxFD - #minSD - #maxSD - - constructor( - lc, // locale is ignored; always use 'en-US' in format() - { - minimumIntegerDigits: minID, - minimumFractionDigits: minFD, - maximumFractionDigits: maxFD, - minimumSignificantDigits: minSD, - maximumSignificantDigits: maxSD - } = {} - ) { - this.#minID = typeof minID === 'number' ? minID : 1 - this.#minFD = typeof minFD === 'number' ? minFD : 0 - this.#maxFD = typeof maxFD === 'number' ? maxFD : Math.max(this.#minFD, 3) - if (typeof minSD === 'number' || typeof maxSD === 'number') { - this.#minSD = typeof minSD === 'number' ? minSD : 1 - this.#maxSD = typeof maxSD === 'number' ? maxSD : 21 - } - } - - resolvedOptions() { - const opt = { - minimumIntegerDigits: this.#minID, - minimumFractionDigits: this.#minFD, - maximumFractionDigits: this.#maxFD - } - if (typeof this.#minSD === 'number') { - opt.minimumSignificantDigits = this.#minSD - opt.maximumSignificantDigits = this.#maxSD - } - Object.defineProperty(opt, 'locale', { get: getDefaultLocale }) - return opt - } - - format(n) { - if (this.#minSD) { - const raw = String(n) - let prec = 0 - for (let i = 0; i < raw.length; ++i) { - const c = raw[i] - if (c >= '0' && c <= '9') ++prec - } - if (prec < this.#minSD) return n.toPrecision(this.#minSD) - if (prec > this.#maxSD) return n.toPrecision(this.#maxSD) - return raw - } - if (this.#minFD > 0) return n.toFixed(this.#minFD) - if (this.#maxFD === 0) return n.toFixed(0) - return String(n) - } -} - -function getDefaultLocale() { - if ( - typeof Intl !== 'undefined' && - typeof Intl.DateTimeFormat === 'function' - ) { - return new Intl.DateTimeFormat().resolvedOptions().locale - } else if (typeof navigator !== 'undefined') { - return navigator.userLanguage || navigator.language || 'en-US' - } else { - return 'en-US' - } -} diff --git a/test/pseudo-numberformat.test.mjs b/test/pseudo-numberformat.test.mjs deleted file mode 100644 index 05cfaa7..0000000 --- a/test/pseudo-numberformat.test.mjs +++ /dev/null @@ -1,75 +0,0 @@ -import { expect } from 'chai' -import * as Plurals from 'make-plural/plurals' -import * as Categories from 'make-plural/pluralCategories' -import * as RangePlurals from 'make-plural/ranges' - -import getPluralRules from '../src/factory.mjs' -import PseudoNumberFormat from '../src/pseudo-number-format.mjs' - -import { suite } from './test-suite.mjs' - -describe('With PseudoNumberFormat', () => { - const id = lc => (lc === 'pt-PT' ? 'pt_PT' : lc) - const getSelector = lc => Plurals[id(lc)] - const getCategories = (lc, ord) => - Categories[id(lc)][ord ? 'ordinal' : 'cardinal'] - const getRangeSelector = lc => RangePlurals[id(lc)] - const PluralRules = getPluralRules( - PseudoNumberFormat, - getSelector, - getCategories, - getRangeSelector - ) - suite(PluralRules) - - describe('default locale', () => { - it('should use same default locale as other Intl formatters', () => { - const Intl_ = global.Intl - try { - class MockFormat { - resolvedOptions = () => ({ locale: 'fi-FI' }) - } - global.Intl = { DateTimeFormat: MockFormat, NumberFormat: MockFormat } - const p = new PluralRules() - const opt = p.resolvedOptions() - expect(opt.locale).to.match(/^fi\b/) - } finally { - global.Intl = Intl_ - } - }) - it('should use navigator.language as fallback', () => { - const Intl_ = global.Intl - const navigator_ = global.navigator - try { - delete global.Intl - delete global.navigator - global.navigator = { language: 'fi-FI' } - const pr = new PluralRules() - const opt = pr.resolvedOptions() - expect(opt.locale).to.match(/^fi\b/) - } finally { - global.Intl = Intl_ - global.navigator = navigator_ - } - }) - it('should use "en-US" as ultimate fallback', () => { - const Intl_ = global.Intl - const navigator_ = global.navigator - try { - delete global.Intl - delete global.navigator - const pr0 = new PluralRules() - const opt0 = pr0.resolvedOptions() - expect(opt0.locale).to.match(/^en\b/) - - global.navigator = { language: undefined } - const pr1 = new PluralRules() - const opt1 = pr1.resolvedOptions() - expect(opt1.locale).to.match(/^en\b/) - } finally { - global.Intl = Intl_ - global.navigator = navigator_ - } - }) - }) -})