cldr-compact-number
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -0,1 +1,87 @@ | ||
var enLocaleData = { | ||
en: { | ||
locale: "en", | ||
numbers: { | ||
decimal: { | ||
long: [[1000, { | ||
one: ["0 thousand", 1], | ||
other: ["0 thousand", 1] | ||
}], [10000, { | ||
one: ["00 thousand", 2], | ||
other: ["00 thousand", 2] | ||
}], [100000, { | ||
one: ["000 thousand", 3], | ||
other: ["000 thousand", 3] | ||
}], [1000000, { | ||
one: ["0 million", 1], | ||
other: ["0 million", 1] | ||
}], [10000000, { | ||
one: ["00 million", 2], | ||
other: ["00 million", 2] | ||
}], [100000000, { | ||
one: ["000 million", 3], | ||
other: ["000 million", 3] | ||
}], [1000000000, { | ||
one: ["0 billion", 1], | ||
other: ["0 billion", 1] | ||
}], [10000000000, { | ||
one: ["00 billion", 2], | ||
other: ["00 billion", 2] | ||
}], [100000000000, { | ||
one: ["000 billion", 3], | ||
other: ["000 billion", 3] | ||
}], [1000000000000, { | ||
one: ["0 trillion", 1], | ||
other: ["0 trillion", 1] | ||
}], [10000000000000, { | ||
one: ["00 trillion", 2], | ||
other: ["00 trillion", 2] | ||
}], [100000000000000, { | ||
one: ["000 trillion", 3], | ||
other: ["000 trillion", 3] | ||
}]], | ||
short: [[1000, { | ||
one: ["0K", 1], | ||
other: ["0K", 1] | ||
}], [10000, { | ||
one: ["00K", 2], | ||
other: ["00K", 2] | ||
}], [100000, { | ||
one: ["000K", 3], | ||
other: ["000K", 3] | ||
}], [1000000, { | ||
one: ["0M", 1], | ||
other: ["0M", 1] | ||
}], [10000000, { | ||
one: ["00M", 2], | ||
other: ["00M", 2] | ||
}], [100000000, { | ||
one: ["000M", 3], | ||
other: ["000M", 3] | ||
}], [1000000000, { | ||
one: ["0B", 1], | ||
other: ["0B", 1] | ||
}], [10000000000, { | ||
one: ["00B", 2], | ||
other: ["00B", 2] | ||
}], [100000000000, { | ||
one: ["000B", 3], | ||
other: ["000B", 3] | ||
}], [1000000000000, { | ||
one: ["0T", 1], | ||
other: ["0T", 1] | ||
}], [10000000000000, { | ||
one: ["00T", 2], | ||
other: ["00T", 2] | ||
}], [100000000000000, { | ||
one: ["000T", 3], | ||
other: ["000T", 3] | ||
}]] | ||
} | ||
} | ||
} | ||
}; | ||
enLocaleData = JSON.parse(JSON.stringify(enLocaleData)); | ||
var enLocaleData$1 = enLocaleData; | ||
function replaceNumber(normalized, format) { | ||
@@ -32,3 +118,4 @@ // 1.734 -> 1K | ||
function findLocaleData(localeData, locale) { | ||
var topLevelData = localeData[locale]; | ||
locale = Array.isArray(locale) ? locale[0] : locale; | ||
var topLevelData = localeData[locale] || localeData[normalizeLocale(locale)]; | ||
if (!topLevelData) { | ||
@@ -93,2 +180,13 @@ return; | ||
var __assign = (undefined && undefined.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function compactFormat(value, locale, localeData, options) { | ||
@@ -100,4 +198,4 @@ if (options === void 0) { options = {}; } | ||
} | ||
localeData = __assign(__assign({}, enLocaleData$1), localeData); | ||
// figure out which numbers hash based on the locale | ||
locale = normalizeLocale(locale); // en_GB -> en-GB | ||
var data = findLocaleData(localeData, locale); | ||
@@ -153,3 +251,4 @@ if (!data) { | ||
} | ||
var normalized = normalizeNumber(extractIntPart(num, range, numberOfDigits), arbitraryPrecision, sign, locale, options); | ||
var normalizedLocale = normalizeLocale(locale); | ||
var normalized = normalizeNumber(extractIntPart(num, range, numberOfDigits), arbitraryPrecision, sign, normalizedLocale, options); | ||
return polishString(replaceNumber(normalized, formatter)); | ||
@@ -156,0 +255,0 @@ } |
@@ -5,4 +5,90 @@ (function (global, factory) { | ||
(global = global || self, factory(global.cldrCompactNumber = {})); | ||
}(this, function (exports) { 'use strict'; | ||
}(this, (function (exports) { 'use strict'; | ||
var enLocaleData = { | ||
en: { | ||
locale: "en", | ||
numbers: { | ||
decimal: { | ||
long: [[1000, { | ||
one: ["0 thousand", 1], | ||
other: ["0 thousand", 1] | ||
}], [10000, { | ||
one: ["00 thousand", 2], | ||
other: ["00 thousand", 2] | ||
}], [100000, { | ||
one: ["000 thousand", 3], | ||
other: ["000 thousand", 3] | ||
}], [1000000, { | ||
one: ["0 million", 1], | ||
other: ["0 million", 1] | ||
}], [10000000, { | ||
one: ["00 million", 2], | ||
other: ["00 million", 2] | ||
}], [100000000, { | ||
one: ["000 million", 3], | ||
other: ["000 million", 3] | ||
}], [1000000000, { | ||
one: ["0 billion", 1], | ||
other: ["0 billion", 1] | ||
}], [10000000000, { | ||
one: ["00 billion", 2], | ||
other: ["00 billion", 2] | ||
}], [100000000000, { | ||
one: ["000 billion", 3], | ||
other: ["000 billion", 3] | ||
}], [1000000000000, { | ||
one: ["0 trillion", 1], | ||
other: ["0 trillion", 1] | ||
}], [10000000000000, { | ||
one: ["00 trillion", 2], | ||
other: ["00 trillion", 2] | ||
}], [100000000000000, { | ||
one: ["000 trillion", 3], | ||
other: ["000 trillion", 3] | ||
}]], | ||
short: [[1000, { | ||
one: ["0K", 1], | ||
other: ["0K", 1] | ||
}], [10000, { | ||
one: ["00K", 2], | ||
other: ["00K", 2] | ||
}], [100000, { | ||
one: ["000K", 3], | ||
other: ["000K", 3] | ||
}], [1000000, { | ||
one: ["0M", 1], | ||
other: ["0M", 1] | ||
}], [10000000, { | ||
one: ["00M", 2], | ||
other: ["00M", 2] | ||
}], [100000000, { | ||
one: ["000M", 3], | ||
other: ["000M", 3] | ||
}], [1000000000, { | ||
one: ["0B", 1], | ||
other: ["0B", 1] | ||
}], [10000000000, { | ||
one: ["00B", 2], | ||
other: ["00B", 2] | ||
}], [100000000000, { | ||
one: ["000B", 3], | ||
other: ["000B", 3] | ||
}], [1000000000000, { | ||
one: ["0T", 1], | ||
other: ["0T", 1] | ||
}], [10000000000000, { | ||
one: ["00T", 2], | ||
other: ["00T", 2] | ||
}], [100000000000000, { | ||
one: ["000T", 3], | ||
other: ["000T", 3] | ||
}]] | ||
} | ||
} | ||
} | ||
}; | ||
enLocaleData = JSON.parse(JSON.stringify(enLocaleData)); | ||
var enLocaleData$1 = enLocaleData; | ||
function replaceNumber(normalized, format) { | ||
@@ -39,3 +125,4 @@ // 1.734 -> 1K | ||
function findLocaleData(localeData, locale) { | ||
var topLevelData = localeData[locale]; | ||
locale = Array.isArray(locale) ? locale[0] : locale; | ||
var topLevelData = localeData[locale] || localeData[normalizeLocale(locale)]; | ||
if (!topLevelData) { | ||
@@ -100,2 +187,13 @@ return; | ||
var __assign = (undefined && undefined.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function compactFormat(value, locale, localeData, options) { | ||
@@ -107,4 +205,4 @@ if (options === void 0) { options = {}; } | ||
} | ||
localeData = __assign(__assign({}, enLocaleData$1), localeData); | ||
// figure out which numbers hash based on the locale | ||
locale = normalizeLocale(locale); // en_GB -> en-GB | ||
var data = findLocaleData(localeData, locale); | ||
@@ -160,11 +258,12 @@ if (!data) { | ||
} | ||
var normalized = normalizeNumber(extractIntPart(num, range, numberOfDigits), arbitraryPrecision, sign, locale, options); | ||
var normalizedLocale = normalizeLocale(locale); | ||
var normalized = normalizeNumber(extractIntPart(num, range, numberOfDigits), arbitraryPrecision, sign, normalizedLocale, options); | ||
return polishString(replaceNumber(normalized, formatter)); | ||
} | ||
exports.compactFormat = compactFormat; | ||
exports.default = compactFormat; | ||
exports.compactFormat = compactFormat; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
}))); |
{ | ||
"name": "cldr-compact-number", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Compact number formatting logic", | ||
@@ -22,3 +22,3 @@ "main": "dist/cldr-compact-number.umd.js", | ||
"transform": { | ||
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js" | ||
".(ts|tsx)": "ts-jest" | ||
}, | ||
@@ -48,19 +48,19 @@ "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$", | ||
"devDependencies": { | ||
"@types/jest": "^23.3.9", | ||
"@types/jest": "^24.0.23", | ||
"@types/node": "^10.12.2", | ||
"jest": "^23.6.0", | ||
"lodash.camelcase": "^4.3.0", | ||
"prettier": "^1.14.3", | ||
"rimraf": "^2.6.2", | ||
"rollup": "^1.1.0", | ||
"prettier": "^1.19.1", | ||
"rimraf": "^3.0.0", | ||
"rollup": "^1.27.2", | ||
"rollup-plugin-node-resolve": "^4.0.0", | ||
"rollup-plugin-sourcemaps": "^0.4.2", | ||
"rollup-plugin-typescript2": "^0.18.1", | ||
"ts-jest": "^23.10.4", | ||
"tslint": "^5.11.0", | ||
"tslint-config-prettier": "^1.15.0", | ||
"ts-jest": "^24.1.0", | ||
"tslint": "^5.20.1", | ||
"tslint-config-prettier": "^1.18.0", | ||
"tslint-eslint-rules": "^5.4.0", | ||
"tslint-plugin-prettier": "^2.0.1", | ||
"typescript": "^3.1.5" | ||
"typescript": "^3.7.2" | ||
} | ||
} |
@@ -33,2 +33,6 @@ cldr-compact-number | ||
We default this library with `en` `localeData`. What is `localeData`? | ||
`localeData` for most cases comes through a build tool. You define what languages you want upfront so as to avoid bloating your application bundle and the build tool parses CLDR data and formats it. See `priv/` for examples of what shape the data should be in if you want to manually construct this data. | ||
```js | ||
@@ -35,0 +39,0 @@ import compactFormat from 'cldr-compact-number'; |
import compactFormat from '../src'; | ||
import { compactFormat as format } from '../src/format'; | ||
import { en, es, fi } from './locale-data'; | ||
import { en, es, es_MX, fi } from './locale-data'; | ||
@@ -19,2 +19,10 @@ // due to regex replacement | ||
it('works with negative values', () => { | ||
const localeData = {}; | ||
let result = compactFormat(-1234, 'en-gb', localeData); | ||
expect(result).toBe(-1234); | ||
result = compactFormat(-1234, 'es-MX', localeData); | ||
expect(result).toBe(-1234); | ||
}); | ||
it('returns value if locale is an array (ember-intl)', () => { | ||
@@ -71,13 +79,29 @@ const localeData = {}; | ||
}); | ||
expect(normalizeWhitespace(result)).toBe('1,2 mil'); | ||
expect(normalizeWhitespace(result)).toBe('1.2 mil'); | ||
result = format(11234, 'es', localeData, { | ||
significantDigits: 1 | ||
}); | ||
expect(normalizeWhitespace(result)).toBe('11,2 mil'); | ||
expect(normalizeWhitespace(result)).toBe('11.2 mil'); | ||
result = format(91934, 'es', localeData, { | ||
significantDigits: 2 | ||
}); | ||
expect(normalizeWhitespace(result)).toBe('91,93 mil'); | ||
expect(normalizeWhitespace(result)).toBe('91.93 mil'); | ||
}); | ||
it('returns with significantDigits and negative number', () => { | ||
const localeData = es; | ||
let result = format(-1234, 'es', localeData, { | ||
significantDigits: 1 | ||
}); | ||
expect(normalizeWhitespace(result)).toBe('-1.2 mil'); | ||
result = format(-11234, 'es', localeData, { | ||
significantDigits: 1 | ||
}); | ||
expect(normalizeWhitespace(result)).toBe('-11.2 mil'); | ||
result = format(-91934, 'es', localeData, { | ||
significantDigits: 2 | ||
}); | ||
expect(normalizeWhitespace(result)).toBe('-91.93 mil'); | ||
}); | ||
it('returns with financial format', () => { | ||
@@ -103,4 +127,26 @@ const localeData = en; | ||
expect(normalizeWhitespace(result)).toBe('1,2 t.'); | ||
expect(normalizeWhitespace(result)).toBe('1.2 t.'); | ||
}); | ||
it('defaults with english', () => { | ||
const result = format(1234, 'en'); | ||
expect(normalizeWhitespace(result)).toBe('1K'); | ||
}); | ||
it('works with es_MX', () => { | ||
const result = format(1234, 'es-MX', es_MX); | ||
expect(normalizeWhitespace(result)).toBe('1 k'); | ||
}); | ||
it('works with multiple locales', () => { | ||
const localeData = { ...es, ...fi }; | ||
let result = format(1234, 'es', localeData); | ||
expect(normalizeWhitespace(result)).toBe('1 mil'); | ||
result = format(1234, 'en', localeData); | ||
expect(normalizeWhitespace(result)).toBe('1K'); | ||
result = format(1234, 'fi', localeData); | ||
expect(normalizeWhitespace(result)).toBe('1 t.'); | ||
}); | ||
}); |
@@ -17,2 +17,17 @@ export const es = { | ||
export const es_MX = { | ||
'es-MX': { | ||
"locale": "es-MX", | ||
"parentLocale": "es-419", | ||
"numbers": { | ||
"decimal": { | ||
"long": [ | ||
[1000, { "one": ["0 mil", 1], "other": ["0 mil", 1] }], [10000, { "one": ["00 mil", 2], "other": ["00 mil", 2] }], [100000, { "one": ["000 mil", 3], "other": ["000 mil", 3] }], [1000000, { "one": ["0 millón", 1], "other": ["0 millones", 1] }], [10000000, { "one": ["00 millones", 2], "other": ["00 millones", 2] }], [100000000, { "one": ["000 millones", 3], "other": ["000 millones", 3] }], [1000000000, { "one": ["0 mil millones", 1], "other": ["0 mil millones", 1] }], [10000000000, { "one": ["00 mil millones", 2], "other": ["00 mil millones", 2] }], [100000000000, { "one": ["000 mil millones", 3], "other": ["000 mil millones", 3] }], [1000000000000, { "one": ["0 billón", 1], "other": ["0 billones", 1] }], [10000000000000, { "one": ["00 billones", 2], "other": ["00 billones", 2] }], [100000000000000, { "one": ["000 billones", 3], "other": ["000 billones", 3] }] | ||
], | ||
"short": [[1000, { "one": ["0 k", 1], "other": ["0 k", 1] }], [10000, { "one": ["00 k", 2], "other": ["00 k", 2] }], [100000, { "one": ["000 k", 3], "other": ["000 k", 3] }], [1000000, { "one": ["0 M", 1], "other": ["0 M", 1] }], [10000000, { "one": ["00 M", 2], "other": ["00 M", 2] }], [100000000, { "one": ["000 M", 3], "other": ["000 M", 3] }], [1000000000, { "one": ["0000 M", 4], "other": ["0000 M", 4] }], [10000000000, { "one": ["00 mil M", 2], "other": ["00 mil M", 2] }], [100000000000, { "one": ["000 mil M", 3], "other": ["000 mil M", 3] }], [1000000000000, { "one": ["0 B", 1], "other": ["0 B", 1] }], [10000000000000, { "one": ["00 B", 2], "other": ["00 B", 2] }], [100000000000000, { "one": ["000 B", 3], "other": ["000 B", 3] }]] | ||
} | ||
} | ||
} | ||
}; | ||
export const en = { | ||
@@ -19,0 +34,0 @@ en: { |
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
60020
1242
124
13
1