@internationalized/number 3.1.3-nightly.3698 → 3.1.3-nightly.3709

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.
Files changed (2) hide show
  1. package/dist/import.mjs +355 -0
  2. package/package.json +7 -2
@@ -0,0 +1,355 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */ /*
12
+ * Copyright 2020 Adobe. All rights reserved.
13
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
14
+ * you may not use this file except in compliance with the License. You may obtain a copy
15
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
16
+ *
17
+ * Unless required by applicable law or agreed to in writing, software distributed under
18
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
19
+ * OF ANY KIND, either express or implied. See the License for the specific language
20
+ * governing permissions and limitations under the License.
21
+ */ let $488c6ddbf4ef74c2$var$formatterCache = new Map();
22
+ let $488c6ddbf4ef74c2$var$supportsSignDisplay = false;
23
+ try {
24
+ // @ts-ignore
25
+ $488c6ddbf4ef74c2$var$supportsSignDisplay = new Intl.NumberFormat("de-DE", {
26
+ signDisplay: "exceptZero"
27
+ }).resolvedOptions().signDisplay === "exceptZero";
28
+ // eslint-disable-next-line no-empty
29
+ } catch (e) {}
30
+ let $488c6ddbf4ef74c2$var$supportsUnit = false;
31
+ try {
32
+ // @ts-ignore
33
+ $488c6ddbf4ef74c2$var$supportsUnit = new Intl.NumberFormat("de-DE", {
34
+ style: "unit",
35
+ unit: "degree"
36
+ }).resolvedOptions().style === "unit";
37
+ // eslint-disable-next-line no-empty
38
+ } catch (e1) {}
39
+ // Polyfill for units since Safari doesn't support them yet. See https://bugs.webkit.org/show_bug.cgi?id=215438.
40
+ // Currently only polyfilling the unit degree in narrow format for ColorSlider in our supported locales.
41
+ // Values were determined by switching to each locale manually in Chrome.
42
+ const $488c6ddbf4ef74c2$var$UNITS = {
43
+ degree: {
44
+ narrow: {
45
+ default: "\xb0",
46
+ "ja-JP": " 度",
47
+ "zh-TW": "度",
48
+ "sl-SI": " \xb0"
49
+ }
50
+ }
51
+ };
52
+ class $488c6ddbf4ef74c2$export$cc77c4ff7e8673c5 {
53
+ /** Formats a number value as a string, according to the locale and options provided to the constructor. */ format(value) {
54
+ let res = "";
55
+ if (!$488c6ddbf4ef74c2$var$supportsSignDisplay && this.options.signDisplay != null) res = $488c6ddbf4ef74c2$export$711b50b3c525e0f2(this.numberFormatter, this.options.signDisplay, value);
56
+ else res = this.numberFormatter.format(value);
57
+ if (this.options.style === "unit" && !$488c6ddbf4ef74c2$var$supportsUnit) {
58
+ var _UNITS_unit;
59
+ let { unit: unit , unitDisplay: unitDisplay = "short" , locale: locale } = this.resolvedOptions();
60
+ let values = (_UNITS_unit = $488c6ddbf4ef74c2$var$UNITS[unit]) === null || _UNITS_unit === void 0 ? void 0 : _UNITS_unit[unitDisplay];
61
+ res += values[locale] || values.default;
62
+ }
63
+ return res;
64
+ }
65
+ /** Formats a number to an array of parts such as separators, digits, punctuation, and more. */ formatToParts(value) {
66
+ // TODO: implement signDisplay for formatToParts
67
+ // @ts-ignore
68
+ return this.numberFormatter.formatToParts(value);
69
+ }
70
+ /** Formats a number range as a string. */ formatRange(start, end) {
71
+ // @ts-ignore
72
+ if (typeof this.numberFormatter.formatRange === "function") // @ts-ignore
73
+ return this.numberFormatter.formatRange(start, end);
74
+ if (end < start) throw new RangeError("End date must be >= start date");
75
+ // Very basic fallback for old browsers.
76
+ return `${this.format(start)} – ${this.format(end)}`;
77
+ }
78
+ /** Formats a number range as an array of parts. */ formatRangeToParts(start, end) {
79
+ // @ts-ignore
80
+ if (typeof this.numberFormatter.formatRangeToParts === "function") // @ts-ignore
81
+ return this.numberFormatter.formatRangeToParts(start, end);
82
+ if (end < start) throw new RangeError("End date must be >= start date");
83
+ let startParts = this.numberFormatter.formatToParts(start);
84
+ let endParts = this.numberFormatter.formatToParts(end);
85
+ return [
86
+ ...startParts.map((p)=>({
87
+ ...p,
88
+ source: "startRange"
89
+ })),
90
+ {
91
+ type: "literal",
92
+ value: " – ",
93
+ source: "shared"
94
+ },
95
+ ...endParts.map((p)=>({
96
+ ...p,
97
+ source: "endRange"
98
+ }))
99
+ ];
100
+ }
101
+ /** Returns the resolved formatting options based on the values passed to the constructor. */ resolvedOptions() {
102
+ let options = this.numberFormatter.resolvedOptions();
103
+ if (!$488c6ddbf4ef74c2$var$supportsSignDisplay && this.options.signDisplay != null) options = {
104
+ ...options,
105
+ signDisplay: this.options.signDisplay
106
+ };
107
+ if (!$488c6ddbf4ef74c2$var$supportsUnit && this.options.style === "unit") options = {
108
+ ...options,
109
+ style: "unit",
110
+ unit: this.options.unit,
111
+ unitDisplay: this.options.unitDisplay
112
+ };
113
+ return options;
114
+ }
115
+ constructor(locale, options = {}){
116
+ this.numberFormatter = $488c6ddbf4ef74c2$var$getCachedNumberFormatter(locale, options);
117
+ this.options = options;
118
+ }
119
+ }
120
+ function $488c6ddbf4ef74c2$var$getCachedNumberFormatter(locale, options = {}) {
121
+ let { numberingSystem: numberingSystem } = options;
122
+ if (numberingSystem && locale.indexOf("-u-nu-") === -1) locale = `${locale}-u-nu-${numberingSystem}`;
123
+ if (options.style === "unit" && !$488c6ddbf4ef74c2$var$supportsUnit) {
124
+ var _UNITS_unit;
125
+ let { unit: unit , unitDisplay: unitDisplay = "short" } = options;
126
+ if (!unit) throw new Error('unit option must be provided with style: "unit"');
127
+ if (!((_UNITS_unit = $488c6ddbf4ef74c2$var$UNITS[unit]) === null || _UNITS_unit === void 0 ? void 0 : _UNITS_unit[unitDisplay])) throw new Error(`Unsupported unit ${unit} with unitDisplay = ${unitDisplay}`);
128
+ options = {
129
+ ...options,
130
+ style: "decimal"
131
+ };
132
+ }
133
+ let cacheKey = locale + (options ? Object.entries(options).sort((a, b)=>a[0] < b[0] ? -1 : 1).join() : "");
134
+ if ($488c6ddbf4ef74c2$var$formatterCache.has(cacheKey)) return $488c6ddbf4ef74c2$var$formatterCache.get(cacheKey);
135
+ let numberFormatter = new Intl.NumberFormat(locale, options);
136
+ $488c6ddbf4ef74c2$var$formatterCache.set(cacheKey, numberFormatter);
137
+ return numberFormatter;
138
+ }
139
+ function $488c6ddbf4ef74c2$export$711b50b3c525e0f2(numberFormat, signDisplay, num) {
140
+ if (signDisplay === "auto") return numberFormat.format(num);
141
+ else if (signDisplay === "never") return numberFormat.format(Math.abs(num));
142
+ else {
143
+ let needsPositiveSign = false;
144
+ if (signDisplay === "always") needsPositiveSign = num > 0 || Object.is(num, 0);
145
+ else if (signDisplay === "exceptZero") {
146
+ if (Object.is(num, -0) || Object.is(num, 0)) num = Math.abs(num);
147
+ else needsPositiveSign = num > 0;
148
+ }
149
+ if (needsPositiveSign) {
150
+ let negative = numberFormat.format(-num);
151
+ let noSign = numberFormat.format(num);
152
+ // ignore RTL/LTR marker character
153
+ let minus = negative.replace(noSign, "").replace(/\u200e|\u061C/, "");
154
+ if ([
155
+ ...minus
156
+ ].length !== 1) console.warn("@react-aria/i18n polyfill for NumberFormat signDisplay: Unsupported case");
157
+ let positive = negative.replace(noSign, "!!!").replace(minus, "+").replace("!!!", noSign);
158
+ return positive;
159
+ } else return numberFormat.format(num);
160
+ }
161
+ }
162
+
163
+
164
+ /*
165
+ * Copyright 2020 Adobe. All rights reserved.
166
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
167
+ * you may not use this file except in compliance with the License. You may obtain a copy
168
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
169
+ *
170
+ * Unless required by applicable law or agreed to in writing, software distributed under
171
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
172
+ * OF ANY KIND, either express or implied. See the License for the specific language
173
+ * governing permissions and limitations under the License.
174
+ */ const $6c7bd7858deea686$var$CURRENCY_SIGN_REGEX = new RegExp("^.*\\(.*\\).*$");
175
+ const $6c7bd7858deea686$var$NUMBERING_SYSTEMS = [
176
+ "latn",
177
+ "arab",
178
+ "hanidec"
179
+ ];
180
+ class $6c7bd7858deea686$export$cd11ab140839f11d {
181
+ /**
182
+ * Parses the given string to a number. Returns NaN if a valid number could not be parsed.
183
+ */ parse(value) {
184
+ return $6c7bd7858deea686$var$getNumberParserImpl(this.locale, this.options, value).parse(value);
185
+ }
186
+ /**
187
+ * Returns whether the given string could potentially be a valid number. This should be used to
188
+ * validate user input as the user types. If a `minValue` or `maxValue` is provided, the validity
189
+ * of the minus/plus sign characters can be checked.
190
+ */ isValidPartialNumber(value, minValue, maxValue) {
191
+ return $6c7bd7858deea686$var$getNumberParserImpl(this.locale, this.options, value).isValidPartialNumber(value, minValue, maxValue);
192
+ }
193
+ /**
194
+ * Returns a numbering system for which the given string is valid in the current locale.
195
+ * If no numbering system could be detected, the default numbering system for the current
196
+ * locale is returned.
197
+ */ getNumberingSystem(value) {
198
+ return $6c7bd7858deea686$var$getNumberParserImpl(this.locale, this.options, value).options.numberingSystem;
199
+ }
200
+ constructor(locale, options = {}){
201
+ this.locale = locale;
202
+ this.options = options;
203
+ }
204
+ }
205
+ const $6c7bd7858deea686$var$numberParserCache = new Map();
206
+ function $6c7bd7858deea686$var$getNumberParserImpl(locale, options, value) {
207
+ // First try the default numbering system for the provided locale
208
+ let defaultParser = $6c7bd7858deea686$var$getCachedNumberParser(locale, options);
209
+ // If that doesn't match, and the locale doesn't include a hard coded numbering system,
210
+ // try each of the other supported numbering systems until we find one that matches.
211
+ if (!locale.includes("-nu-") && !defaultParser.isValidPartialNumber(value)) {
212
+ for (let numberingSystem of $6c7bd7858deea686$var$NUMBERING_SYSTEMS)if (numberingSystem !== defaultParser.options.numberingSystem) {
213
+ let parser = $6c7bd7858deea686$var$getCachedNumberParser(locale + (locale.includes("-u-") ? "-nu-" : "-u-nu-") + numberingSystem, options);
214
+ if (parser.isValidPartialNumber(value)) return parser;
215
+ }
216
+ }
217
+ return defaultParser;
218
+ }
219
+ function $6c7bd7858deea686$var$getCachedNumberParser(locale, options) {
220
+ let cacheKey = locale + (options ? Object.entries(options).sort((a, b)=>a[0] < b[0] ? -1 : 1).join() : "");
221
+ let parser = $6c7bd7858deea686$var$numberParserCache.get(cacheKey);
222
+ if (!parser) {
223
+ parser = new $6c7bd7858deea686$var$NumberParserImpl(locale, options);
224
+ $6c7bd7858deea686$var$numberParserCache.set(cacheKey, parser);
225
+ }
226
+ return parser;
227
+ }
228
+ // The actual number parser implementation. Instances of this class are cached
229
+ // based on the locale, options, and detected numbering system.
230
+ class $6c7bd7858deea686$var$NumberParserImpl {
231
+ parse(value) {
232
+ // to parse the number, we need to remove anything that isn't actually part of the number, for example we want '-10.40' not '-10.40 USD'
233
+ let fullySanitizedValue = this.sanitize(value);
234
+ // Remove group characters, and replace decimal points and numerals with ASCII values.
235
+ fullySanitizedValue = $6c7bd7858deea686$var$replaceAll(fullySanitizedValue, this.symbols.group, "").replace(this.symbols.decimal, ".").replace(this.symbols.minusSign, "-").replace(this.symbols.numeral, this.symbols.index);
236
+ let newValue = fullySanitizedValue ? +fullySanitizedValue : NaN;
237
+ if (isNaN(newValue)) return NaN;
238
+ // accounting will always be stripped to a positive number, so if it's accounting and has a () around everything, then we need to make it negative again
239
+ if (this.options.currencySign === "accounting" && $6c7bd7858deea686$var$CURRENCY_SIGN_REGEX.test(value)) newValue = -1 * newValue;
240
+ // when reading the number, if it's a percent, then it should be interpreted as being divided by 100
241
+ if (this.options.style === "percent") {
242
+ newValue /= 100;
243
+ var _this_options_maximumFractionDigits;
244
+ // after dividing to get the percent value, javascript may get .0210999999 instead of .0211, so fix the number of fraction digits
245
+ newValue = +newValue.toFixed(((_this_options_maximumFractionDigits = this.options.maximumFractionDigits) !== null && _this_options_maximumFractionDigits !== void 0 ? _this_options_maximumFractionDigits : 0) + 2);
246
+ }
247
+ return newValue;
248
+ }
249
+ sanitize(value) {
250
+ // Remove literals and whitespace, which are allowed anywhere in the string
251
+ value = value.replace(this.symbols.literals, "");
252
+ // Replace the ASCII minus sign with the minus sign used in the current locale
253
+ // so that both are allowed in case the user's keyboard doesn't have the locale's minus sign.
254
+ value = value.replace("-", this.symbols.minusSign);
255
+ // In arab numeral system, their decimal character is 1643, but most keyboards don't type that
256
+ // instead they use the , (44) character or apparently the (1548) character.
257
+ if (this.options.numberingSystem === "arab") {
258
+ value = value.replace(",", this.symbols.decimal);
259
+ value = value.replace(String.fromCharCode(1548), this.symbols.decimal);
260
+ value = $6c7bd7858deea686$var$replaceAll(value, ".", this.symbols.group);
261
+ }
262
+ // fr-FR group character is char code 8239, but that's not a key on the french keyboard,
263
+ // so allow 'period' as a group char and replace it with a space
264
+ if (this.options.locale === "fr-FR") value = $6c7bd7858deea686$var$replaceAll(value, ".", String.fromCharCode(8239));
265
+ return value;
266
+ }
267
+ isValidPartialNumber(value, minValue = -Infinity, maxValue = Infinity) {
268
+ value = this.sanitize(value);
269
+ // Remove minus or plus sign, which must be at the start of the string.
270
+ if (value.startsWith(this.symbols.minusSign) && minValue < 0) value = value.slice(this.symbols.minusSign.length);
271
+ else if (this.symbols.plusSign && value.startsWith(this.symbols.plusSign) && maxValue > 0) value = value.slice(this.symbols.plusSign.length);
272
+ // Numbers cannot start with a group separator
273
+ if (value.startsWith(this.symbols.group)) return false;
274
+ // Remove numerals, groups, and decimals
275
+ value = $6c7bd7858deea686$var$replaceAll(value, this.symbols.group, "").replace(this.symbols.numeral, "").replace(this.symbols.decimal, "");
276
+ // The number is valid if there are no remaining characters
277
+ return value.length === 0;
278
+ }
279
+ constructor(locale, options = {}){
280
+ this.formatter = new Intl.NumberFormat(locale, options);
281
+ this.options = this.formatter.resolvedOptions();
282
+ this.symbols = $6c7bd7858deea686$var$getSymbols(this.formatter, this.options, options);
283
+ }
284
+ }
285
+ const $6c7bd7858deea686$var$nonLiteralParts = new Set([
286
+ "decimal",
287
+ "fraction",
288
+ "integer",
289
+ "minusSign",
290
+ "plusSign",
291
+ "group"
292
+ ]);
293
+ function $6c7bd7858deea686$var$getSymbols(formatter, intlOptions, originalOptions) {
294
+ var _allParts_find, _posAllParts_find, _allParts_find1, _allParts_find2;
295
+ // Note: some locale's don't add a group symbol until there is a ten thousands place
296
+ let allParts = formatter.formatToParts(-10000.111);
297
+ let posAllParts = formatter.formatToParts(10000.111);
298
+ let singularParts = formatter.formatToParts(1);
299
+ var _allParts_find_value;
300
+ let minusSign = (_allParts_find_value = (_allParts_find = allParts.find((p)=>p.type === "minusSign")) === null || _allParts_find === void 0 ? void 0 : _allParts_find.value) !== null && _allParts_find_value !== void 0 ? _allParts_find_value : "-";
301
+ let plusSign = (_posAllParts_find = posAllParts.find((p)=>p.type === "plusSign")) === null || _posAllParts_find === void 0 ? void 0 : _posAllParts_find.value;
302
+ // Safari does not support the signDisplay option, but our number parser polyfills it.
303
+ // If no plus sign was returned, but the original options contained signDisplay, default to the '+' character.
304
+ // @ts-ignore
305
+ if (!plusSign && ((originalOptions === null || originalOptions === void 0 ? void 0 : originalOptions.signDisplay) === "exceptZero" || (originalOptions === null || originalOptions === void 0 ? void 0 : originalOptions.signDisplay) === "always")) plusSign = "+";
306
+ let decimal = (_allParts_find1 = allParts.find((p)=>p.type === "decimal")) === null || _allParts_find1 === void 0 ? void 0 : _allParts_find1.value;
307
+ let group = (_allParts_find2 = allParts.find((p)=>p.type === "group")) === null || _allParts_find2 === void 0 ? void 0 : _allParts_find2.value;
308
+ // this set is also for a regex, it's all literals that might be in the string we want to eventually parse that
309
+ // don't contribute to the numerical value
310
+ let pluralLiterals = allParts.filter((p)=>!$6c7bd7858deea686$var$nonLiteralParts.has(p.type)).map((p)=>$6c7bd7858deea686$var$escapeRegex(p.value));
311
+ let singularLiterals = singularParts.filter((p)=>!$6c7bd7858deea686$var$nonLiteralParts.has(p.type)).map((p)=>$6c7bd7858deea686$var$escapeRegex(p.value));
312
+ let sortedLiterals = [
313
+ ...new Set([
314
+ ...singularLiterals,
315
+ ...pluralLiterals
316
+ ])
317
+ ].sort((a, b)=>b.length - a.length);
318
+ let literals = sortedLiterals.length === 0 ? new RegExp("[\\p{White_Space}]", "gu") : new RegExp(`${sortedLiterals.join("|")}|[\\p{White_Space}]`, "gu");
319
+ // These are for replacing non-latn characters with the latn equivalent
320
+ let numerals = [
321
+ ...new Intl.NumberFormat(intlOptions.locale, {
322
+ useGrouping: false
323
+ }).format(9876543210)
324
+ ].reverse();
325
+ let indexes = new Map(numerals.map((d, i)=>[
326
+ d,
327
+ i
328
+ ]));
329
+ let numeral = new RegExp(`[${numerals.join("")}]`, "g");
330
+ let index = (d)=>String(indexes.get(d));
331
+ return {
332
+ minusSign: minusSign,
333
+ plusSign: plusSign,
334
+ decimal: decimal,
335
+ group: group,
336
+ literals: literals,
337
+ numeral: numeral,
338
+ index: index
339
+ };
340
+ }
341
+ function $6c7bd7858deea686$var$replaceAll(str, find, replace) {
342
+ // @ts-ignore
343
+ if (str.replaceAll) // @ts-ignore
344
+ return str.replaceAll(find, replace);
345
+ return str.split(find).join(replace);
346
+ }
347
+ function $6c7bd7858deea686$var$escapeRegex(string) {
348
+ return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
349
+ }
350
+
351
+
352
+
353
+
354
+ export {$488c6ddbf4ef74c2$export$cc77c4ff7e8673c5 as NumberFormatter, $6c7bd7858deea686$export$cd11ab140839f11d as NumberParser};
355
+ //# sourceMappingURL=module.js.map
package/package.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "@internationalized/number",
3
- "version": "3.1.3-nightly.3698+72ee92f6d",
3
+ "version": "3.1.3-nightly.3709+0f953cac8",
4
4
  "description": "Internationalized number formatting and parsing utilities",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/module.js",
8
+ "exports": {
9
+ "types": "./dist/types.d.ts",
10
+ "import": "./dist/import.mjs",
11
+ "require": "./dist/main.js"
12
+ },
8
13
  "types": "dist/types.d.ts",
9
14
  "source": "src/index.ts",
10
15
  "files": [
@@ -22,5 +27,5 @@
22
27
  "publishConfig": {
23
28
  "access": "public"
24
29
  },
25
- "gitHead": "72ee92f6d497163468a80642ccb96576f822f365"
30
+ "gitHead": "0f953cac84f98cdff29f2348c120ce541ebb6da8"
26
31
  }