@formatjs/intl-unified-numberformat
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -6,2 +6,16 @@ # Change Log | ||
## [0.4.1](https://github.com/formatjs/formatjs/compare/@formatjs/intl-unified-numberformat@0.4.0...@formatjs/intl-unified-numberformat@0.4.1) (2019-08-21) | ||
### Bug Fixes | ||
* **@formatjs/intl-unified-numberformat:** add DEFAULT_LOCALE in locale lookup ([1f25453](https://github.com/formatjs/formatjs/commit/1f25453)) | ||
* **@formatjs/intl-unified-numberformat:** dist polyfill with all locales ([f4c494a](https://github.com/formatjs/formatjs/commit/f4c494a)) | ||
* **@formatjs/intl-unified-numberformat:** use locale-lookup from intl-utils ([7f5eb9e](https://github.com/formatjs/formatjs/commit/7f5eb9e)) | ||
* **intl-unified-numberformat:** dist UMD ([f568cdc](https://github.com/formatjs/formatjs/commit/f568cdc)), closes [#159](https://github.com/formatjs/formatjs/issues/159) | ||
# [0.4.0](https://github.com/formatjs/formatjs/compare/@formatjs/intl-unified-numberformat@0.2.0...@formatjs/intl-unified-numberformat@0.4.0) (2019-08-19) | ||
@@ -8,0 +22,0 @@ |
@@ -38,7 +38,6 @@ import { Unit } from './units-constants'; | ||
private patternData?; | ||
constructor(locale?: string, { style, unit, unitDisplay, ...options }?: UnifiedNumberFormatOptions); | ||
constructor(locales: string | string[], { style, unit, unitDisplay, ...options }?: UnifiedNumberFormatOptions); | ||
format(num: number): string; | ||
formatToParts(num: number): Intl.NumberFormatPart[]; | ||
resolvedOptions(): ResolvedUnifiedNumberFormatOptions; | ||
private findUnitData; | ||
static supportedLocalesOf(...args: Parameters<typeof NativeNumberFormat.supportedLocalesOf>): string[]; | ||
@@ -45,0 +44,0 @@ static polyfilled: boolean; |
@@ -17,35 +17,7 @@ var __assign = (this && this.__assign) || function () { | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) | ||
t[p[i]] = s[p[i]]; | ||
return t; | ||
}; | ||
function resolveLocale(locales) { | ||
var localeData = UnifiedNumberFormat.__unitLocaleData__; | ||
var resolvedLocales = (Array.isArray(locales) | ||
? locales | ||
: [locales]).filter(function (s) { return typeof s === 'string'; }); | ||
var i, len, localeParts, data; | ||
var supportedLocales = []; | ||
// Using the set of locales + the default locale, we look for the first one | ||
// which that has been registered. When data does not exist for a locale, we | ||
// traverse its ancestors to find something that's been registered within | ||
// its hierarchy of locales. Since we lack the proper `parentLocale` data | ||
// here, we must take a naive approach to traversal. | ||
for (i = 0, len = resolvedLocales.length; i < len; i += 1) { | ||
localeParts = resolvedLocales[i].toLowerCase().split('-'); | ||
while (localeParts.length) { | ||
data = localeData[localeParts.join('-')]; | ||
if (data) { | ||
// Return the normalized locale string; e.g., we return "en-US", | ||
// instead of "en-us". | ||
supportedLocales.push(data.locale); | ||
break; | ||
} | ||
localeParts.pop(); | ||
} | ||
} | ||
return supportedLocales; | ||
} | ||
import { resolveSupportedLocales } from '@formatjs/intl-utils'; | ||
export function isUnitSupported(unit) { | ||
@@ -64,4 +36,28 @@ try { | ||
var NativeNumberFormat = Intl.NumberFormat; | ||
function findUnitData(locale, unit) { | ||
var data = UnifiedNumberFormat.__unitLocaleData__; | ||
var parentLocale = ''; | ||
locale = locale.toLowerCase(); | ||
if (!data[locale]) { | ||
parentLocale = locale.split('-')[0]; | ||
} | ||
else { | ||
if (data[locale].units[unit]) { | ||
return data[locale].units[unit]; | ||
} | ||
if (data[locale].parentLocale) { | ||
parentLocale = data[locale].parentLocale; | ||
} | ||
else { | ||
throw new RangeError("Cannot find data for " + locale); | ||
} | ||
} | ||
return findUnitData(parentLocale, unit); | ||
} | ||
function intersection(arr1, arr2) { | ||
return arr1.filter(function (s) { return ~arr2.indexOf(s); }); | ||
} | ||
var DEFAULT_LOCALE = new NativeNumberFormat().resolvedOptions().locale; | ||
var UnifiedNumberFormat = /** @class */ (function () { | ||
function UnifiedNumberFormat(locale, _a) { | ||
function UnifiedNumberFormat(locales, _a) { | ||
if (_a === void 0) { _a = {}; } | ||
@@ -77,6 +73,8 @@ var style = _a.style, unit = _a.unit, unitDisplay = _a.unitDisplay, options = __rest(_a, ["style", "unit", "unitDisplay"]); | ||
this.unitDisplay = unitDisplay || 'short'; | ||
this.locale = resolveLocale([locale])[0]; | ||
this.patternData = this.findUnitData(this.locale, this.unit); | ||
var resolvedLocale = resolveSupportedLocales(intersection(NativeNumberFormat.supportedLocalesOf(locales), Intl.PluralRules.supportedLocalesOf(locales)).concat([ | ||
DEFAULT_LOCALE, | ||
]), UnifiedNumberFormat.__unitLocaleData__)[0]; | ||
this.patternData = findUnitData(resolvedLocale, this.unit); | ||
} | ||
this.nf = new NativeNumberFormat(locale, __assign({}, options, { style: style === 'unit' ? 'decimal' : style })); | ||
this.nf = new NativeNumberFormat(locales, __assign({}, options, { style: style === 'unit' ? 'decimal' : style })); | ||
this.locale = this.nf.resolvedOptions().locale; | ||
@@ -105,22 +103,2 @@ } | ||
}; | ||
UnifiedNumberFormat.prototype.findUnitData = function (locale, unit) { | ||
var data = UnifiedNumberFormat.__unitLocaleData__; | ||
var parentLocale = ''; | ||
locale = locale.toLowerCase(); | ||
if (!data[locale]) { | ||
parentLocale = locale.split('-')[0]; | ||
} | ||
else { | ||
if (data[locale].units[unit]) { | ||
return data[locale].units[unit]; | ||
} | ||
if (data[locale].parentLocale) { | ||
parentLocale = data[locale].parentLocale; | ||
} | ||
else { | ||
throw new RangeError("Cannot find data for " + locale); | ||
} | ||
} | ||
return this.findUnitData(parentLocale, unit); | ||
}; | ||
UnifiedNumberFormat.supportedLocalesOf = function () { | ||
@@ -131,3 +109,3 @@ var args = []; | ||
} | ||
return resolveLocale(NativeNumberFormat.supportedLocalesOf.apply(NativeNumberFormat, args)); | ||
return resolveSupportedLocales(args[0], UnifiedNumberFormat.__unitLocaleData__); | ||
}; | ||
@@ -134,0 +112,0 @@ UnifiedNumberFormat.__addUnitLocaleData = function (data) { |
{ | ||
"name": "@formatjs/intl-unified-numberformat", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Ponyfill for intl unified numberformat proposal", | ||
@@ -29,2 +29,5 @@ "keywords": [ | ||
}, | ||
"dependencies": { | ||
"@formatjs/intl-utils": "^0.6.1" | ||
}, | ||
"scripts": { | ||
@@ -34,3 +37,3 @@ "build": "npm run cldr && npm run compile", | ||
"clean": "rimraf dist lib", | ||
"compile": "tsc && tsc -p tsconfig.cjs.json", | ||
"compile": "tsc && tsc -p tsconfig.cjs.json && rollup -c rollup.config.js", | ||
"jest": "cross-env NODE_ICU_DATA=../../node_modules/full-icu TS_NODE_PROJECT=tsconfig.cjs.json cross-env NODE_ENV=test jest", | ||
@@ -42,3 +45,3 @@ "test": "npm run jest" | ||
}, | ||
"gitHead": "8731e3472e76505e0383cfbd9e31326ae877140d" | ||
"gitHead": "3c05983721b36e5870edecc16ecda5b4ef219529" | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
2014103
17
8475
1
+ Added@formatjs/intl-utils@^0.6.1
+ Added@babel/runtime@7.24.7(transitive)
+ Added@formatjs/intl-utils@0.6.1(transitive)
+ Addeddate-fns@2.30.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)