@automattic/number-formatters 1.2.4 → 1.2.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/number-formatters",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "Number formatting utilities",
5
5
  "homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/number-formatters/#readme",
6
6
  "bugs": {
@@ -15,17 +15,16 @@
15
15
  "type": "module",
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/types/index.d.ts",
19
- "import": "./dist/esm/index.js",
20
- "require": "./dist/cjs/index.cjs"
18
+ "import": "./dist/index.js",
19
+ "require": "./dist/index.cjs"
21
20
  }
22
21
  },
23
- "main": "./dist/cjs/index.cjs",
24
- "module": "./dist/esm/index.js",
25
- "types": "./dist/types/index.d.ts",
22
+ "main": "./dist/index.cjs",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
26
25
  "scripts": {
27
26
  "build": "pnpm run clean && pnpm run build:ts",
28
- "build:ts": "duel --dirs",
27
+ "build:ts": "tsdown",
29
28
  "clean": "rm -rf dist",
30
29
  "test": "NODE_OPTIONS=--experimental-vm-modules jest",
31
30
  "test-coverage": "pnpm run test --coverage",
@@ -33,18 +32,16 @@
33
32
  },
34
33
  "dependencies": {
35
34
  "@wordpress/date": "^5.19.0",
36
- "debug": "^4.4.0",
37
- "tslib": "^2.5.0"
35
+ "debug": "^4.4.0"
38
36
  },
39
37
  "devDependencies": {
40
38
  "@babel/core": "7.29.7",
41
- "@babel/preset-react": "7.29.7",
42
39
  "@jest/globals": "30.4.1",
43
- "@knighted/duel": "4.0.2",
44
40
  "@types/jest": "30.0.0",
45
41
  "@typescript/native-preview": "7.0.0-dev.20260225.1",
46
42
  "jest": "30.4.2",
47
43
  "jetpack-js-tools": "workspace:*",
44
+ "tsdown": "0.22.2",
48
45
  "typescript": "5.9.3"
49
46
  }
50
47
  }
package/src/constants.ts CHANGED
@@ -1,6 +1,9 @@
1
+ /*
2
+ * This is just to hint to the dependency extraction tool that this package depends on @wordpress/date.
3
+ * The locale is read off the `wp.date` global rather than imported, so there is nothing to bind here.
4
+ * See: https://github.com/Automattic/jetpack/pull/47812#issuecomment-4142452829
5
+ */
6
+ import '@wordpress/date';
7
+
1
8
  export const FALLBACK_LOCALE = 'en';
2
9
  export const FALLBACK_CURRENCY = 'USD';
3
-
4
- // This is just to hint to the dependency extraction tool that this package depends on @wordpress/date
5
- // See: https://github.com/Automattic/jetpack/pull/47812#issuecomment-4142452829
6
- export * from '@wordpress/date';
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FALLBACK_CURRENCY = exports.FALLBACK_LOCALE = void 0;
4
- const tslib_1 = require("tslib");
5
- exports.FALLBACK_LOCALE = 'en';
6
- exports.FALLBACK_CURRENCY = 'USD';
7
- // This is just to hint to the dependency extraction tool that this package depends on @wordpress/date
8
- // See: https://github.com/Automattic/jetpack/pull/47812#issuecomment-4142452829
9
- tslib_1.__exportStar(require("@wordpress/date"), exports);
@@ -1,113 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const constants_ts_1 = require("./constants.cjs");
4
- const index_ts_1 = require("./number-format-currency/index.cjs");
5
- const number_format_ts_1 = require("./number-format.cjs");
6
- /**
7
- * Creates a NumberFormatters instance that provides number and currency formatting functionality with locale awareness
8
- * @return {NumberFormatters} A NumberFormatters instance
9
- */
10
- function createNumberFormatters() {
11
- let localeState;
12
- let geoLocationState;
13
- let currencyOverridesState;
14
- const setLocale = (locale) => {
15
- /**
16
- * The `Intl.NumberFormat` constructor fails only when there is a variant, divided by `_`.
17
- * These suffixes should be removed. Values like `de-at` or `es-mx`
18
- * should all be valid inputs for the constructor.
19
- */
20
- localeState = locale;
21
- };
22
- const setCurrencyOverrides = (overrides) => {
23
- currencyOverridesState = overrides;
24
- };
25
- /**
26
- * Returns the locale defined on the module instance (through `setLocale`)
27
- * or the "fallback locale" if no locale has been set.
28
- *
29
- * The "fallback locale" is defined as:
30
- * - the current WP user locale, if available through `@wordpress/date` settings (assuming this runs in a WordPress context)
31
- * - or the browser locale, if available through `window.navigator.language`
32
- * - or the fallback locale constant (`FALLBACK_LOCALE`)
33
- *
34
- * @return {string} The locale to use for formatting.
35
- */
36
- const getBrowserSafeLocale = () => {
37
- // Accessing the user's locale from `@wordpress/date` package.
38
- // This is a bit hacky but it's better than importing `@wordpress/date` and using its `getSettings` function,
39
- // because it drags moment.js with it even though we don't need it here.
40
- const localeFromUserSettings = typeof window !== 'undefined' ? window.wp?.date?.getSettings?.()?.l10n?.locale : undefined;
41
- const localeFromNavigator = typeof window !== 'undefined' ? window?.navigator?.language : undefined;
42
- return (localeState ??
43
- (localeFromUserSettings || localeFromNavigator) ??
44
- constants_ts_1.FALLBACK_LOCALE).split('_')[0];
45
- };
46
- const setGeoLocation = (geoLocation) => {
47
- geoLocationState = geoLocation;
48
- };
49
- const formatNumber = (number, { decimals = 0, forceLatin = true, numberFormatOptions = {} } = {}) => {
50
- try {
51
- const formatter = (0, number_format_ts_1.numberFormat)({
52
- browserSafeLocale: getBrowserSafeLocale(),
53
- decimals,
54
- forceLatin,
55
- numberFormatOptions,
56
- });
57
- return formatter.format(number);
58
- }
59
- catch {
60
- return String(number);
61
- }
62
- };
63
- const formatNumberCompact = (number, { decimals = 0, forceLatin = true, numberFormatOptions = {} } = {}) => {
64
- try {
65
- const formatter = (0, number_format_ts_1.numberFormatCompact)({
66
- browserSafeLocale: getBrowserSafeLocale(),
67
- decimals,
68
- forceLatin,
69
- numberFormatOptions,
70
- });
71
- return formatter.format(number);
72
- }
73
- catch {
74
- return String(number);
75
- }
76
- };
77
- const formatCurrency = (number, currency, { stripZeros = false, isSmallestUnit = false, signForPositive = false, forceLatin = true } = {}) => {
78
- return (0, index_ts_1.numberFormatCurrency)({
79
- number,
80
- currency,
81
- browserSafeLocale: getBrowserSafeLocale(),
82
- stripZeros,
83
- isSmallestUnit,
84
- signForPositive,
85
- geoLocation: geoLocationState,
86
- forceLatin,
87
- currencyOverrides: currencyOverridesState,
88
- });
89
- };
90
- const getCurrencyObject = (number, currency, { stripZeros = false, isSmallestUnit = false, signForPositive = false, forceLatin = true } = {}) => {
91
- return (0, index_ts_1.getCurrencyObject)({
92
- number,
93
- currency,
94
- browserSafeLocale: getBrowserSafeLocale(),
95
- stripZeros,
96
- isSmallestUnit,
97
- signForPositive,
98
- geoLocation: geoLocationState,
99
- forceLatin,
100
- currencyOverrides: currencyOverridesState,
101
- });
102
- };
103
- return {
104
- setLocale,
105
- setGeoLocation,
106
- setCurrencyOverrides,
107
- formatNumber,
108
- formatNumberCompact,
109
- formatCurrency,
110
- getCurrencyObject,
111
- };
112
- }
113
- exports.default = createNumberFormatters;
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCachedFormatter = getCachedFormatter;
4
- const tslib_1 = require("tslib");
5
- const debug_1 = tslib_1.__importDefault(require("debug"));
6
- const constants_ts_1 = require("./constants.cjs");
7
- const debug = (0, debug_1.default)('number-formatters:get-cached-formatter');
8
- const formatterCache = new Map();
9
- /**
10
- * Get a cached formatter for a given locale and options.
11
- * @param params - The parameters for the formatter.
12
- * @param params.locale - The locale to format the number in.
13
- * @param params.options - Intl.NumberFormatOptions to pass to the formatter.
14
- * @param params.fallbackLocale - The locale to fallback to if the locale is not supported.
15
- * @param params.retries - The number of retries to attempt if the formatter is not created.
16
- * @return {Intl.NumberFormat} A cached formatter for the given locale and options.
17
- */
18
- function getCachedFormatter({ locale, fallbackLocale = constants_ts_1.FALLBACK_LOCALE, options, retries = 1, }) {
19
- const cacheKey = JSON.stringify([locale, options]);
20
- try {
21
- return (formatterCache.get(cacheKey) ??
22
- formatterCache.set(cacheKey, new Intl.NumberFormat(locale, options)).get(cacheKey));
23
- }
24
- catch (error) {
25
- // If the locale is invalid, creating the NumberFormat will throw.
26
- debug(`Intl.NumberFormat was called with a non-existent locale "${locale}"; falling back to ${fallbackLocale}`);
27
- if (retries) {
28
- return getCachedFormatter({
29
- locale: fallbackLocale,
30
- options,
31
- retries: retries - 1,
32
- });
33
- }
34
- throw error;
35
- }
36
- }
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createNumberFormatters = exports.getCurrencyObject = exports.formatCurrency = exports.formatNumberCompact = exports.formatNumber = exports.setCurrencyOverrides = exports.setGeoLocation = exports.setLocale = void 0;
4
- const tslib_1 = require("tslib");
5
- const create_number_formatters_ts_1 = tslib_1.__importDefault(require("./create-number-formatters.cjs"));
6
- exports.createNumberFormatters = create_number_formatters_ts_1.default;
7
- const defaultFormatter = (0, create_number_formatters_ts_1.default)();
8
- exports.setLocale = defaultFormatter.setLocale, exports.setGeoLocation = defaultFormatter.setGeoLocation, exports.setCurrencyOverrides = defaultFormatter.setCurrencyOverrides, exports.formatNumber = defaultFormatter.formatNumber, exports.formatNumberCompact = defaultFormatter.formatNumberCompact, exports.formatCurrency = defaultFormatter.formatCurrency, exports.getCurrencyObject = defaultFormatter.getCurrencyObject;
9
- // We can optionally export the formatters individually if we want to use them in a more granular way.
10
- // export { numberFormat, numberFormatCompact, numberFormatCurrency, getCurrencyObject };