@automattic/number-formatters 1.0.0-alpha.1 → 1.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/dist/constants.js +5 -2
- package/dist/create-number-formatters.d.ts +1 -1
- package/dist/create-number-formatters.js +13 -11
- package/dist/get-cached-formatter.js +10 -4
- package/dist/index.d.ts +3 -3
- package/dist/index.js +25 -5
- package/dist/number-format-currency/currencies.d.ts +1 -1
- package/dist/number-format-currency/currencies.js +4 -1
- package/dist/number-format-currency/index.d.ts +1 -1
- package/dist/number-format-currency/index.js +17 -10
- package/dist/number-format.d.ts +1 -1
- package/dist/number-format.js +7 -3
- package/dist/types.js +2 -1
- package/package.json +1 -2
- package/src/create-number-formatters.ts +4 -4
- package/src/get-cached-formatter.ts +1 -1
- package/src/index.ts +2 -2
- package/src/number-format-currency/currencies.ts +1 -1
- package/src/number-format-currency/index.ts +4 -4
- package/src/number-format.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
6
6
|
|
|
7
|
+
## [1.0.0-alpha.2] - 2025-04-10
|
|
8
|
+
### Changed
|
|
9
|
+
- Adjust relative imports in TypeScript sources to use correct extensions after enabling TypeScript's new `rewriteRelativeImportExtensions` option. The built JS should be unaffected, so this is not a breaking change. [#42990]
|
|
10
|
+
- Convert build to common-js [#42982]
|
|
11
|
+
|
|
12
|
+
## [1.0.0-alpha.1] - 2025-04-10
|
|
13
|
+
### Changed
|
|
14
|
+
- Internal updates.
|
|
15
|
+
|
|
7
16
|
## [1.0.0-alpha.1] - 2025-04-07
|
|
8
17
|
### Added
|
|
9
18
|
- initial release [#42639]
|
|
@@ -20,4 +29,5 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
|
20
29
|
- Initial release
|
|
21
30
|
- Basic number formatting functionality
|
|
22
31
|
|
|
32
|
+
[1.0.0-alpha.2]: https://github.com/Automattic/number-formatters/compare/1.0.0-alpha.1...1.0.0-alpha.2
|
|
23
33
|
[1.0.0-alpha.1]: https://github.com/Automattic/number-formatters/compare/0.1.0...1.0.0-alpha.1
|
package/dist/constants.js
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FALLBACK_CURRENCY = exports.FALLBACK_LOCALE = void 0;
|
|
4
|
+
exports.FALLBACK_LOCALE = 'en';
|
|
5
|
+
exports.FALLBACK_CURRENCY = 'USD';
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const date_1 = require("@wordpress/date");
|
|
4
|
+
const constants_ts_1 = require("./constants.js");
|
|
5
|
+
const index_ts_1 = require("./number-format-currency/index.js");
|
|
6
|
+
const number_format_ts_1 = require("./number-format.js");
|
|
5
7
|
/**
|
|
6
8
|
* Creates a NumberFormatters instance that provides number and currency formatting functionality with locale awareness
|
|
7
9
|
* @return {NumberFormatters} A NumberFormatters instance
|
|
@@ -29,17 +31,17 @@ function createNumberFormatters() {
|
|
|
29
31
|
* @return {string} The locale to use for formatting.
|
|
30
32
|
*/
|
|
31
33
|
const getBrowserSafeLocale = () => {
|
|
32
|
-
const { l10n: { locale: localeFromUserSettings }, } = getSettings();
|
|
34
|
+
const { l10n: { locale: localeFromUserSettings }, } = (0, date_1.getSettings)();
|
|
33
35
|
return (localeState ??
|
|
34
36
|
(localeFromUserSettings || global?.window?.navigator?.language) ??
|
|
35
|
-
FALLBACK_LOCALE).split('_')[0];
|
|
37
|
+
constants_ts_1.FALLBACK_LOCALE).split('_')[0];
|
|
36
38
|
};
|
|
37
39
|
const setGeoLocation = (geoLocation) => {
|
|
38
40
|
geoLocationState = geoLocation;
|
|
39
41
|
};
|
|
40
42
|
const formatNumber = (number, { decimals = 0, forceLatin = true, numberFormatOptions = {} } = {}) => {
|
|
41
43
|
try {
|
|
42
|
-
const formatter = numberFormat({
|
|
44
|
+
const formatter = (0, number_format_ts_1.numberFormat)({
|
|
43
45
|
browserSafeLocale: getBrowserSafeLocale(),
|
|
44
46
|
decimals,
|
|
45
47
|
forceLatin,
|
|
@@ -53,7 +55,7 @@ function createNumberFormatters() {
|
|
|
53
55
|
};
|
|
54
56
|
const formatNumberCompact = (number, { decimals = 0, forceLatin = true, numberFormatOptions = {} } = {}) => {
|
|
55
57
|
try {
|
|
56
|
-
const formatter = numberFormatCompact({
|
|
58
|
+
const formatter = (0, number_format_ts_1.numberFormatCompact)({
|
|
57
59
|
browserSafeLocale: getBrowserSafeLocale(),
|
|
58
60
|
decimals,
|
|
59
61
|
forceLatin,
|
|
@@ -66,7 +68,7 @@ function createNumberFormatters() {
|
|
|
66
68
|
}
|
|
67
69
|
};
|
|
68
70
|
const formatCurrency = (number, currency, { stripZeros = false, isSmallestUnit = false, signForPositive = false, forceLatin = true } = {}) => {
|
|
69
|
-
return numberFormatCurrency({
|
|
71
|
+
return (0, index_ts_1.numberFormatCurrency)({
|
|
70
72
|
number,
|
|
71
73
|
currency,
|
|
72
74
|
browserSafeLocale: getBrowserSafeLocale(),
|
|
@@ -78,7 +80,7 @@ function createNumberFormatters() {
|
|
|
78
80
|
});
|
|
79
81
|
};
|
|
80
82
|
const getCurrencyObject = (number, currency, { stripZeros = false, isSmallestUnit = false, signForPositive = false, forceLatin = true } = {}) => {
|
|
81
|
-
return
|
|
83
|
+
return (0, index_ts_1.getCurrencyObject)({
|
|
82
84
|
number,
|
|
83
85
|
currency,
|
|
84
86
|
browserSafeLocale: getBrowserSafeLocale(),
|
|
@@ -98,4 +100,4 @@ function createNumberFormatters() {
|
|
|
98
100
|
getCurrencyObject,
|
|
99
101
|
};
|
|
100
102
|
}
|
|
101
|
-
|
|
103
|
+
exports.default = createNumberFormatters;
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getCachedFormatter = getCachedFormatter;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const constants_ts_1 = require("./constants.js");
|
|
9
|
+
const debug = (0, debug_1.default)('number-formatters:get-cached-formatter');
|
|
4
10
|
const formatterCache = new Map();
|
|
5
11
|
/**
|
|
6
12
|
* Get a cached formatter for a given locale and options.
|
|
@@ -11,7 +17,7 @@ const formatterCache = new Map();
|
|
|
11
17
|
* @param params.retries - The number of retries to attempt if the formatter is not created.
|
|
12
18
|
* @return {Intl.NumberFormat} A cached formatter for the given locale and options.
|
|
13
19
|
*/
|
|
14
|
-
|
|
20
|
+
function getCachedFormatter({ locale, fallbackLocale = constants_ts_1.FALLBACK_LOCALE, options, retries = 1, }) {
|
|
15
21
|
const cacheKey = JSON.stringify([locale, options]);
|
|
16
22
|
try {
|
|
17
23
|
return (formatterCache.get(cacheKey) ??
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import createNumberFormatters from './create-number-formatters.
|
|
2
|
-
export declare const setLocale: (locale: string) => void, setGeoLocation: (geoLocation: string) => void, formatNumber: import("./types.
|
|
1
|
+
import createNumberFormatters from './create-number-formatters.ts';
|
|
2
|
+
export declare const setLocale: (locale: string) => void, setGeoLocation: (geoLocation: string) => void, formatNumber: import("./types.ts").FormatNumber, formatNumberCompact: import("./types.ts").FormatNumber, formatCurrency: import("./types.ts").FormatCurrency, getCurrencyObject: import("./types.ts").GetCurrencyObject;
|
|
3
3
|
export { createNumberFormatters };
|
|
4
|
-
export * from './types.
|
|
4
|
+
export * from './types.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.createNumberFormatters = exports.getCurrencyObject = exports.formatCurrency = exports.formatNumberCompact = exports.formatNumber = exports.setGeoLocation = exports.setLocale = void 0;
|
|
21
|
+
const create_number_formatters_ts_1 = __importDefault(require("./create-number-formatters.js"));
|
|
22
|
+
exports.createNumberFormatters = create_number_formatters_ts_1.default;
|
|
23
|
+
const defaultFormatter = (0, create_number_formatters_ts_1.default)();
|
|
24
|
+
exports.setLocale = defaultFormatter.setLocale, exports.setGeoLocation = defaultFormatter.setGeoLocation, exports.formatNumber = defaultFormatter.formatNumber, exports.formatNumberCompact = defaultFormatter.formatNumberCompact, exports.formatCurrency = defaultFormatter.formatCurrency, exports.getCurrencyObject = defaultFormatter.getCurrencyObject;
|
|
25
|
+
__exportStar(require("./types.js"), exports);
|
|
6
26
|
// We can optionally export the formatters individually if we want to use them in a more granular way.
|
|
7
27
|
// export { numberFormat, numberFormatCompact, numberFormatCurrency, getCurrencyObject };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { CurrencyOverride } from '../types.
|
|
1
|
+
import type { CurrencyOverride } from '../types.ts';
|
|
2
2
|
export declare const defaultCurrencyOverrides: Record<string, CurrencyOverride>;
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getCurrencyObject = exports.numberFormatCurrency = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const constants_ts_1 = require("../constants.js");
|
|
9
|
+
const get_cached_formatter_ts_1 = require("../get-cached-formatter.js");
|
|
10
|
+
const currencies_ts_1 = require("./currencies.js");
|
|
11
|
+
const debug = (0, debug_1.default)('number-formatters:number-format-currency');
|
|
6
12
|
/**
|
|
7
13
|
* Retrieves the currency override for a given currency.
|
|
8
14
|
* If the currency is USD and the user is not in the US, it will return `US$`.
|
|
@@ -14,7 +20,7 @@ function getCurrencyOverride(currency, geoLocation) {
|
|
|
14
20
|
if (currency === 'USD' && geoLocation && geoLocation !== '' && geoLocation !== 'US') {
|
|
15
21
|
return { symbol: 'US$' };
|
|
16
22
|
}
|
|
17
|
-
return defaultCurrencyOverrides[currency];
|
|
23
|
+
return currencies_ts_1.defaultCurrencyOverrides[currency];
|
|
18
24
|
}
|
|
19
25
|
/**
|
|
20
26
|
* Returns a valid currency code based on a shortlist of currency codes.
|
|
@@ -25,8 +31,8 @@ function getCurrencyOverride(currency, geoLocation) {
|
|
|
25
31
|
*/
|
|
26
32
|
function getValidCurrency(currency, geoLocation) {
|
|
27
33
|
if (!getCurrencyOverride(currency, geoLocation)) {
|
|
28
|
-
debug(`getValidCurrency was called with a non-existent currency "${currency}"; falling back to ${FALLBACK_CURRENCY}`);
|
|
29
|
-
return FALLBACK_CURRENCY;
|
|
34
|
+
debug(`getValidCurrency was called with a non-existent currency "${currency}"; falling back to ${constants_ts_1.FALLBACK_CURRENCY}`);
|
|
35
|
+
return constants_ts_1.FALLBACK_CURRENCY;
|
|
30
36
|
}
|
|
31
37
|
return currency;
|
|
32
38
|
}
|
|
@@ -75,7 +81,7 @@ function getCurrencyFormatter({ number, currency, browserSafeLocale, forceLatin
|
|
|
75
81
|
}),
|
|
76
82
|
...(signForPositive && { signDisplay: 'exceptZero' }),
|
|
77
83
|
};
|
|
78
|
-
return getCachedFormatter({
|
|
84
|
+
return (0, get_cached_formatter_ts_1.getCachedFormatter)({
|
|
79
85
|
locale,
|
|
80
86
|
options: numberFormatOptions,
|
|
81
87
|
});
|
|
@@ -204,6 +210,7 @@ const numberFormatCurrency = ({ number, browserSafeLocale, currency, stripZeros,
|
|
|
204
210
|
}
|
|
205
211
|
}, '');
|
|
206
212
|
};
|
|
213
|
+
exports.numberFormatCurrency = numberFormatCurrency;
|
|
207
214
|
/**
|
|
208
215
|
* Returns a formatted price object which can be used to manually render a
|
|
209
216
|
* formatted currency (eg: if you wanted to render the currency symbol in a
|
|
@@ -314,4 +321,4 @@ const getCurrencyObject = ({ number, browserSafeLocale, currency, stripZeros, is
|
|
|
314
321
|
hasNonZeroFraction,
|
|
315
322
|
};
|
|
316
323
|
};
|
|
317
|
-
|
|
324
|
+
exports.getCurrencyObject = getCurrencyObject;
|
package/dist/number-format.d.ts
CHANGED
package/dist/number-format.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.numberFormatCompact = exports.numberFormat = void 0;
|
|
4
|
+
const get_cached_formatter_ts_1 = require("./get-cached-formatter.js");
|
|
2
5
|
/**
|
|
3
6
|
* Formats numbers using locale settings and/or passed options.
|
|
4
7
|
* @param params - The parameters for the number formatter.
|
|
@@ -33,8 +36,9 @@ const numberFormat = ({ browserSafeLocale, decimals = 0, forceLatin = true, numb
|
|
|
33
36
|
maximumFractionDigits: decimals, // maximumFractionDigits default is the greater between minimumFractionDigits and 3
|
|
34
37
|
...numberFormatOptions,
|
|
35
38
|
};
|
|
36
|
-
return getCachedFormatter({ locale, options });
|
|
39
|
+
return (0, get_cached_formatter_ts_1.getCachedFormatter)({ locale, options });
|
|
37
40
|
};
|
|
41
|
+
exports.numberFormat = numberFormat;
|
|
38
42
|
/**
|
|
39
43
|
* Convenience method for formatting numbers in a compact notation e.g. 1K, 1M, etc.
|
|
40
44
|
* Basically sets `notation: 'compact'` and `maximumFractionDigits: 1` in the options.
|
|
@@ -52,4 +56,4 @@ const numberFormatCompact = ({ numberFormatOptions = {}, ...params }) => numberF
|
|
|
52
56
|
...numberFormatOptions,
|
|
53
57
|
},
|
|
54
58
|
});
|
|
55
|
-
|
|
59
|
+
exports.numberFormatCompact = numberFormatCompact;
|
package/dist/types.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automattic/number-formatters",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
4
|
"description": "Number formatting utilities",
|
|
5
5
|
"author": "Automattic",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
"jest-environment-jsdom": "29.7.0",
|
|
28
28
|
"typescript": "5.8.2"
|
|
29
29
|
},
|
|
30
|
-
"type": "module",
|
|
31
30
|
"exports": {
|
|
32
31
|
".": {
|
|
33
32
|
"types": "./dist/index.d.ts",
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { getSettings } from '@wordpress/date';
|
|
2
|
-
import { FALLBACK_LOCALE } from './constants.
|
|
2
|
+
import { FALLBACK_LOCALE } from './constants.ts';
|
|
3
3
|
import {
|
|
4
4
|
numberFormatCurrency,
|
|
5
5
|
getCurrencyObject as getCurrencyObjectFromCurrencyFormatter,
|
|
6
|
-
} from './number-format-currency/index.
|
|
7
|
-
import { numberFormat, numberFormatCompact } from './number-format.
|
|
8
|
-
import type { CurrencyObject, FormatCurrency, FormatNumber, GetCurrencyObject } from './types.
|
|
6
|
+
} from './number-format-currency/index.ts';
|
|
7
|
+
import { numberFormat, numberFormatCompact } from './number-format.ts';
|
|
8
|
+
import type { CurrencyObject, FormatCurrency, FormatNumber, GetCurrencyObject } from './types.ts';
|
|
9
9
|
|
|
10
10
|
// Since global is used inside createNumberFormatters, we need to declare it for TS
|
|
11
11
|
declare const global: typeof globalThis;
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import createNumberFormatters from './create-number-formatters.
|
|
1
|
+
import createNumberFormatters from './create-number-formatters.ts';
|
|
2
2
|
|
|
3
3
|
const defaultFormatter = createNumberFormatters();
|
|
4
4
|
|
|
@@ -13,7 +13,7 @@ export const {
|
|
|
13
13
|
|
|
14
14
|
export { createNumberFormatters };
|
|
15
15
|
|
|
16
|
-
export * from './types.
|
|
16
|
+
export * from './types.ts';
|
|
17
17
|
|
|
18
18
|
// We can optionally export the formatters individually if we want to use them in a more granular way.
|
|
19
19
|
// export { numberFormat, numberFormatCompact, numberFormatCurrency, getCurrencyObject };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import debugFactory from 'debug';
|
|
2
|
-
import { FALLBACK_CURRENCY } from '../constants.
|
|
3
|
-
import { getCachedFormatter } from '../get-cached-formatter.
|
|
4
|
-
import { defaultCurrencyOverrides } from './currencies.
|
|
5
|
-
import type { CurrencyOverride, CurrencyObject, NumberFormatCurrencyParams } from '../types.
|
|
2
|
+
import { FALLBACK_CURRENCY } from '../constants.ts';
|
|
3
|
+
import { getCachedFormatter } from '../get-cached-formatter.ts';
|
|
4
|
+
import { defaultCurrencyOverrides } from './currencies.ts';
|
|
5
|
+
import type { CurrencyOverride, CurrencyObject, NumberFormatCurrencyParams } from '../types.ts';
|
|
6
6
|
|
|
7
7
|
const debug = debugFactory( 'number-formatters:number-format-currency' );
|
|
8
8
|
|
package/src/number-format.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getCachedFormatter } from './get-cached-formatter.
|
|
2
|
-
import type { NumberFormatParams } from './types.
|
|
1
|
+
import { getCachedFormatter } from './get-cached-formatter.ts';
|
|
2
|
+
import type { NumberFormatParams } from './types.ts';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Formats numbers using locale settings and/or passed options.
|