numeric-quantity 2.1.0 → 3.1.0
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/README.md +15 -3
- package/dist/cjs/numeric-quantity.cjs.development.js +349 -250
- package/dist/cjs/numeric-quantity.cjs.development.js.map +1 -1
- package/dist/cjs/numeric-quantity.cjs.production.js +1 -1
- package/dist/cjs/numeric-quantity.cjs.production.js.map +1 -1
- package/dist/numeric-quantity.iife.umd.min.js +2 -0
- package/dist/numeric-quantity.iife.umd.min.js.map +1 -0
- package/dist/numeric-quantity.legacy-esm.js +402 -230
- package/dist/numeric-quantity.legacy-esm.js.map +1 -1
- package/dist/numeric-quantity.mjs +338 -215
- package/dist/numeric-quantity.mjs.map +1 -1
- package/dist/numeric-quantity.production.mjs +1 -1
- package/dist/numeric-quantity.production.mjs.map +1 -1
- package/dist/types/constants.d.ts +10 -1
- package/dist/types/numericQuantity.d.ts +0 -1
- package/dist/types/types.d.ts +6 -0
- package/dist/types-esm/constants.d.mts +10 -1
- package/dist/types-esm/numericQuantity.d.mts +0 -1
- package/dist/types-esm/types.d.mts +6 -0
- package/package.json +16 -13
- package/dist/numeric-quantity.umd.min.js +0 -2
- package/dist/numeric-quantity.umd.min.js.map +0 -1
|
@@ -1,225 +1,348 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
//#region src/constants.ts
|
|
2
|
+
/**
|
|
3
|
+
* Unicode decimal digit block start code points.
|
|
4
|
+
* Each block contains 10 contiguous digits (0-9).
|
|
5
|
+
* This list covers all \p{Nd} (Decimal_Number) blocks through Unicode 17.0.
|
|
6
|
+
* The drift test in index.test.ts validates completeness against the JS engine.
|
|
7
|
+
*/
|
|
8
|
+
const decimalDigitBlockStarts = [
|
|
9
|
+
48,
|
|
10
|
+
1632,
|
|
11
|
+
1776,
|
|
12
|
+
1984,
|
|
13
|
+
2406,
|
|
14
|
+
2534,
|
|
15
|
+
2662,
|
|
16
|
+
2790,
|
|
17
|
+
2918,
|
|
18
|
+
3046,
|
|
19
|
+
3174,
|
|
20
|
+
3302,
|
|
21
|
+
3430,
|
|
22
|
+
3558,
|
|
23
|
+
3664,
|
|
24
|
+
3792,
|
|
25
|
+
3872,
|
|
26
|
+
4160,
|
|
27
|
+
4240,
|
|
28
|
+
6112,
|
|
29
|
+
6160,
|
|
30
|
+
6470,
|
|
31
|
+
6608,
|
|
32
|
+
6784,
|
|
33
|
+
6800,
|
|
34
|
+
6992,
|
|
35
|
+
7088,
|
|
36
|
+
7232,
|
|
37
|
+
7248,
|
|
38
|
+
42528,
|
|
39
|
+
43216,
|
|
40
|
+
43264,
|
|
41
|
+
43472,
|
|
42
|
+
43504,
|
|
43
|
+
43600,
|
|
44
|
+
44016,
|
|
45
|
+
65296,
|
|
46
|
+
66720,
|
|
47
|
+
68912,
|
|
48
|
+
68928,
|
|
49
|
+
69734,
|
|
50
|
+
69872,
|
|
51
|
+
69942,
|
|
52
|
+
70096,
|
|
53
|
+
70384,
|
|
54
|
+
70736,
|
|
55
|
+
70864,
|
|
56
|
+
71248,
|
|
57
|
+
71360,
|
|
58
|
+
71376,
|
|
59
|
+
71386,
|
|
60
|
+
71472,
|
|
61
|
+
71904,
|
|
62
|
+
72016,
|
|
63
|
+
72688,
|
|
64
|
+
72784,
|
|
65
|
+
73040,
|
|
66
|
+
73120,
|
|
67
|
+
73184,
|
|
68
|
+
73552,
|
|
69
|
+
90416,
|
|
70
|
+
92768,
|
|
71
|
+
92864,
|
|
72
|
+
93008,
|
|
73
|
+
93552,
|
|
74
|
+
118e3,
|
|
75
|
+
120782,
|
|
76
|
+
120792,
|
|
77
|
+
120802,
|
|
78
|
+
120812,
|
|
79
|
+
120822,
|
|
80
|
+
123200,
|
|
81
|
+
123632,
|
|
82
|
+
124144,
|
|
83
|
+
124401,
|
|
84
|
+
125264,
|
|
85
|
+
130032
|
|
86
|
+
];
|
|
87
|
+
/**
|
|
88
|
+
* Normalizes non-ASCII decimal digits to ASCII digits.
|
|
89
|
+
* Converts characters from Unicode decimal digit blocks (e.g., Arabic-Indic,
|
|
90
|
+
* Devanagari, Bengali) to their ASCII equivalents (0-9).
|
|
91
|
+
*
|
|
92
|
+
* All current Unicode \p{Nd} blocks are included in decimalDigitBlockStarts.
|
|
93
|
+
*/
|
|
94
|
+
const normalizeDigits = (str) => str.replace(/\p{Nd}/gu, (ch) => {
|
|
95
|
+
const cp = ch.codePointAt(0);
|
|
96
|
+
if (cp <= 57) return ch;
|
|
97
|
+
let lo = 0;
|
|
98
|
+
let hi = decimalDigitBlockStarts.length - 1;
|
|
99
|
+
while (lo < hi) {
|
|
100
|
+
const mid = lo + hi + 1 >>> 1;
|
|
101
|
+
if (decimalDigitBlockStarts[mid] <= cp) lo = mid;
|
|
102
|
+
else hi = mid - 1;
|
|
103
|
+
}
|
|
104
|
+
return String(cp - decimalDigitBlockStarts[lo]);
|
|
105
|
+
});
|
|
106
|
+
/**
|
|
107
|
+
* Map of Unicode fraction code points to their ASCII equivalents.
|
|
108
|
+
*/
|
|
109
|
+
const vulgarFractionToAsciiMap = {
|
|
110
|
+
"¼": "1/4",
|
|
111
|
+
"½": "1/2",
|
|
112
|
+
"¾": "3/4",
|
|
113
|
+
"⅐": "1/7",
|
|
114
|
+
"⅑": "1/9",
|
|
115
|
+
"⅒": "1/10",
|
|
116
|
+
"⅓": "1/3",
|
|
117
|
+
"⅔": "2/3",
|
|
118
|
+
"⅕": "1/5",
|
|
119
|
+
"⅖": "2/5",
|
|
120
|
+
"⅗": "3/5",
|
|
121
|
+
"⅘": "4/5",
|
|
122
|
+
"⅙": "1/6",
|
|
123
|
+
"⅚": "5/6",
|
|
124
|
+
"⅛": "1/8",
|
|
125
|
+
"⅜": "3/8",
|
|
126
|
+
"⅝": "5/8",
|
|
127
|
+
"⅞": "7/8",
|
|
128
|
+
"⅟": "1/"
|
|
22
129
|
};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Captures the individual elements of a numeric string. Commas and underscores are allowed
|
|
132
|
+
* as separators, as long as they appear between digits and are not consecutive.
|
|
133
|
+
*
|
|
134
|
+
* Capture groups:
|
|
135
|
+
*
|
|
136
|
+
* | # | Description | Example(s) |
|
|
137
|
+
* | --- | ------------------------------------------------ | ------------------------------------------------------------------- |
|
|
138
|
+
* | `0` | entire string | `"2 1/3"` from `"2 1/3"` |
|
|
139
|
+
* | `1` | "negative" dash | `"-"` from `"-2 1/3"` |
|
|
140
|
+
* | `2` | whole number or numerator | `"2"` from `"2 1/3"`; `"1"` from `"1/3"` |
|
|
141
|
+
* | `3` | entire fraction, decimal portion, or denominator | `" 1/3"` from `"2 1/3"`; `".33"` from `"2.33"`; `"/3"` from `"1/3"` |
|
|
142
|
+
*
|
|
143
|
+
* _Capture group 2 may include comma/underscore separators._
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
*
|
|
147
|
+
* ```ts
|
|
148
|
+
* numericRegex.exec("1") // [ "1", "1", null, null ]
|
|
149
|
+
* numericRegex.exec("1.23") // [ "1.23", "1", ".23", null ]
|
|
150
|
+
* numericRegex.exec("1 2/3") // [ "1 2/3", "1", " 2/3", " 2" ]
|
|
151
|
+
* numericRegex.exec("2/3") // [ "2/3", "2", "/3", null ]
|
|
152
|
+
* numericRegex.exec("2 / 3") // [ "2 / 3", "2", "/ 3", null ]
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
const numericRegex = /^(?=-?\s*\.\d|-?\s*\d)(-)?\s*((?:\d(?:[,_]\d|\d)*)*)(([eE][+-]?\d(?:[,_]\d|\d)*)?|\.\d(?:[,_]\d|\d)*([eE][+-]?\d(?:[,_]\d|\d)*)?|(\s+\d(?:[,_]\d|\d)*\s*)?\s*\/\s*\d(?:[,_]\d|\d)*)?$/;
|
|
156
|
+
/**
|
|
157
|
+
* Same as {@link numericRegex}, but allows (and ignores) trailing invalid characters.
|
|
158
|
+
*/
|
|
159
|
+
const numericRegexWithTrailingInvalid = /^(?=-?\s*\.\d|-?\s*\d)(-)?\s*((?:\d(?:[,_]\d|\d)*)*)(([eE][+-]?\d(?:[,_]\d|\d)*)?|\.\d(?:[,_]\d|\d)*([eE][+-]?\d(?:[,_]\d|\d)*)?|(\s+\d(?:[,_]\d|\d)*\s*)?\s*\/\s*\d(?:[,_]\d|\d)*)?(?:\s*[^.\d/].*)?/;
|
|
160
|
+
/**
|
|
161
|
+
* Captures any Unicode vulgar fractions.
|
|
162
|
+
*/
|
|
163
|
+
const vulgarFractionsRegex = /([¼½¾⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞⅟}])/g;
|
|
164
|
+
/**
|
|
165
|
+
* Map of Roman numeral sequences to their decimal equivalents.
|
|
166
|
+
*/
|
|
167
|
+
const romanNumeralValues = {
|
|
168
|
+
MMM: 3e3,
|
|
169
|
+
MM: 2e3,
|
|
170
|
+
M: 1e3,
|
|
171
|
+
CM: 900,
|
|
172
|
+
DCCC: 800,
|
|
173
|
+
DCC: 700,
|
|
174
|
+
DC: 600,
|
|
175
|
+
D: 500,
|
|
176
|
+
CD: 400,
|
|
177
|
+
CCC: 300,
|
|
178
|
+
CC: 200,
|
|
179
|
+
C: 100,
|
|
180
|
+
XC: 90,
|
|
181
|
+
LXXX: 80,
|
|
182
|
+
LXX: 70,
|
|
183
|
+
LX: 60,
|
|
184
|
+
L: 50,
|
|
185
|
+
XL: 40,
|
|
186
|
+
XXX: 30,
|
|
187
|
+
XX: 20,
|
|
188
|
+
XII: 12,
|
|
189
|
+
XI: 11,
|
|
190
|
+
X: 10,
|
|
191
|
+
IX: 9,
|
|
192
|
+
VIII: 8,
|
|
193
|
+
VII: 7,
|
|
194
|
+
VI: 6,
|
|
195
|
+
V: 5,
|
|
196
|
+
IV: 4,
|
|
197
|
+
III: 3,
|
|
198
|
+
II: 2,
|
|
199
|
+
I: 1
|
|
65
200
|
};
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
"\u2171": "II",
|
|
103
|
-
// Small Roman Numeral Three (U+2172)
|
|
104
|
-
"\u2172": "III",
|
|
105
|
-
// Small Roman Numeral Four (U+2173)
|
|
106
|
-
"\u2173": "IV",
|
|
107
|
-
// Small Roman Numeral Five (U+2174)
|
|
108
|
-
"\u2174": "V",
|
|
109
|
-
// Small Roman Numeral Six (U+2175)
|
|
110
|
-
"\u2175": "VI",
|
|
111
|
-
// Small Roman Numeral Seven (U+2176)
|
|
112
|
-
"\u2176": "VII",
|
|
113
|
-
// Small Roman Numeral Eight (U+2177)
|
|
114
|
-
"\u2177": "VIII",
|
|
115
|
-
// Small Roman Numeral Nine (U+2178)
|
|
116
|
-
"\u2178": "IX",
|
|
117
|
-
// Small Roman Numeral Ten (U+2179)
|
|
118
|
-
"\u2179": "X",
|
|
119
|
-
// Small Roman Numeral Eleven (U+217A)
|
|
120
|
-
"\u217A": "XI",
|
|
121
|
-
// Small Roman Numeral Twelve (U+217B)
|
|
122
|
-
"\u217B": "XII",
|
|
123
|
-
// Small Roman Numeral Fifty (U+217C)
|
|
124
|
-
"\u217C": "L",
|
|
125
|
-
// Small Roman Numeral One Hundred (U+217D)
|
|
126
|
-
"\u217D": "C",
|
|
127
|
-
// Small Roman Numeral Five Hundred (U+217E)
|
|
128
|
-
"\u217E": "D",
|
|
129
|
-
// Small Roman Numeral One Thousand (U+217F)
|
|
130
|
-
"\u217F": "M"
|
|
201
|
+
/**
|
|
202
|
+
* Map of Unicode Roman numeral code points to their ASCII equivalents.
|
|
203
|
+
*/
|
|
204
|
+
const romanNumeralUnicodeToAsciiMap = {
|
|
205
|
+
Ⅰ: "I",
|
|
206
|
+
Ⅱ: "II",
|
|
207
|
+
Ⅲ: "III",
|
|
208
|
+
Ⅳ: "IV",
|
|
209
|
+
Ⅴ: "V",
|
|
210
|
+
Ⅵ: "VI",
|
|
211
|
+
Ⅶ: "VII",
|
|
212
|
+
Ⅷ: "VIII",
|
|
213
|
+
Ⅸ: "IX",
|
|
214
|
+
Ⅹ: "X",
|
|
215
|
+
Ⅺ: "XI",
|
|
216
|
+
Ⅻ: "XII",
|
|
217
|
+
Ⅼ: "L",
|
|
218
|
+
Ⅽ: "C",
|
|
219
|
+
Ⅾ: "D",
|
|
220
|
+
Ⅿ: "M",
|
|
221
|
+
ⅰ: "I",
|
|
222
|
+
ⅱ: "II",
|
|
223
|
+
ⅲ: "III",
|
|
224
|
+
ⅳ: "IV",
|
|
225
|
+
ⅴ: "V",
|
|
226
|
+
ⅵ: "VI",
|
|
227
|
+
ⅶ: "VII",
|
|
228
|
+
ⅷ: "VIII",
|
|
229
|
+
ⅸ: "IX",
|
|
230
|
+
ⅹ: "X",
|
|
231
|
+
ⅺ: "XI",
|
|
232
|
+
ⅻ: "XII",
|
|
233
|
+
ⅼ: "L",
|
|
234
|
+
ⅽ: "C",
|
|
235
|
+
ⅾ: "D",
|
|
236
|
+
ⅿ: "M"
|
|
131
237
|
};
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
238
|
+
/**
|
|
239
|
+
* Captures all Unicode Roman numeral code points.
|
|
240
|
+
*/
|
|
241
|
+
const romanNumeralUnicodeRegex = /([ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⅺⅻⅼⅽⅾⅿ])/gi;
|
|
242
|
+
/**
|
|
243
|
+
* Captures a valid Roman numeral sequence.
|
|
244
|
+
*
|
|
245
|
+
* Capture groups:
|
|
246
|
+
*
|
|
247
|
+
* | # | Description | Example |
|
|
248
|
+
* | --- | --------------- | ------------------------ |
|
|
249
|
+
* | `0` | Entire string | "MCCXIV" from "MCCXIV" |
|
|
250
|
+
* | `1` | Thousands | "M" from "MCCXIV" |
|
|
251
|
+
* | `2` | Hundreds | "CC" from "MCCXIV" |
|
|
252
|
+
* | `3` | Tens | "X" from "MCCXIV" |
|
|
253
|
+
* | `4` | Ones | "IV" from "MCCXIV" |
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
*
|
|
257
|
+
* ```ts
|
|
258
|
+
* romanNumeralRegex.exec("M") // [ "M", "M", "", "", "" ]
|
|
259
|
+
* romanNumeralRegex.exec("XII") // [ "XII", "", "", "X", "II" ]
|
|
260
|
+
* romanNumeralRegex.exec("MCCXIV") // [ "MCCXIV", "M", "CC", "X", "IV" ]
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
const romanNumeralRegex = /^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;
|
|
264
|
+
/**
|
|
265
|
+
* Default options for {@link numericQuantity}.
|
|
266
|
+
*/
|
|
267
|
+
const defaultOptions = {
|
|
268
|
+
round: 3,
|
|
269
|
+
allowTrailingInvalid: false,
|
|
270
|
+
romanNumerals: false,
|
|
271
|
+
bigIntOnOverflow: false,
|
|
272
|
+
decimalSeparator: "."
|
|
142
273
|
};
|
|
143
274
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
275
|
+
//#endregion
|
|
276
|
+
//#region src/parseRomanNumerals.ts
|
|
277
|
+
/**
|
|
278
|
+
* Converts a string of Roman numerals to a number, like `parseInt`
|
|
279
|
+
* for Roman numerals. Uses modern, strict rules (only 1 to 3999).
|
|
280
|
+
*
|
|
281
|
+
* The string can include ASCII representations of Roman numerals
|
|
282
|
+
* or Unicode Roman numeral code points (`U+2160` through `U+217F`).
|
|
283
|
+
*/
|
|
284
|
+
const parseRomanNumerals = (romanNumerals) => {
|
|
285
|
+
const normalized = `${romanNumerals}`.replace(romanNumeralUnicodeRegex, (_m, rn) => romanNumeralUnicodeToAsciiMap[rn]).toUpperCase();
|
|
286
|
+
const regexResult = romanNumeralRegex.exec(normalized);
|
|
287
|
+
if (!regexResult) return NaN;
|
|
288
|
+
const [, thousands, hundreds, tens, ones] = regexResult;
|
|
289
|
+
return (romanNumeralValues[thousands] ?? 0) + (romanNumeralValues[hundreds] ?? 0) + (romanNumeralValues[tens] ?? 0) + (romanNumeralValues[ones] ?? 0);
|
|
156
290
|
};
|
|
157
291
|
|
|
158
|
-
|
|
159
|
-
|
|
292
|
+
//#endregion
|
|
293
|
+
//#region src/numericQuantity.ts
|
|
294
|
+
const spaceThenSlashRegex = /^\s*\//;
|
|
160
295
|
function numericQuantity(quantity, options = defaultOptions) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
return dash ? finalResult * -1 : finalResult;
|
|
296
|
+
if (typeof quantity === "number" || typeof quantity === "bigint") return quantity;
|
|
297
|
+
let finalResult = NaN;
|
|
298
|
+
const quantityAsString = normalizeDigits(`${quantity}`.replace(vulgarFractionsRegex, (_m, vf) => ` ${vulgarFractionToAsciiMap[vf]}`).replace("⁄", "/").trim());
|
|
299
|
+
if (quantityAsString.length === 0) return NaN;
|
|
300
|
+
const opts = {
|
|
301
|
+
...defaultOptions,
|
|
302
|
+
...options
|
|
303
|
+
};
|
|
304
|
+
let normalizedString = quantityAsString;
|
|
305
|
+
if (opts.decimalSeparator === ",") {
|
|
306
|
+
const commaCount = (quantityAsString.match(/,/g) || []).length;
|
|
307
|
+
if (commaCount === 1) normalizedString = quantityAsString.replaceAll(".", "_").replace(",", ".");
|
|
308
|
+
else if (commaCount > 1) {
|
|
309
|
+
if (!opts.allowTrailingInvalid) return NaN;
|
|
310
|
+
const firstCommaIndex = quantityAsString.indexOf(",");
|
|
311
|
+
const secondCommaIndex = quantityAsString.indexOf(",", firstCommaIndex + 1);
|
|
312
|
+
const beforeSecondComma = quantityAsString.substring(0, secondCommaIndex).replaceAll(".", "_").replace(",", ".");
|
|
313
|
+
const afterSecondComma = quantityAsString.substring(secondCommaIndex + 1);
|
|
314
|
+
normalizedString = opts.allowTrailingInvalid ? beforeSecondComma + "&" + afterSecondComma : beforeSecondComma;
|
|
315
|
+
} else normalizedString = quantityAsString.replaceAll(".", "_");
|
|
316
|
+
}
|
|
317
|
+
const regexResult = (opts.allowTrailingInvalid ? numericRegexWithTrailingInvalid : numericRegex).exec(normalizedString);
|
|
318
|
+
if (!regexResult) return opts.romanNumerals ? parseRomanNumerals(quantityAsString) : NaN;
|
|
319
|
+
const [, dash, ng1temp, ng2temp] = regexResult;
|
|
320
|
+
const numberGroup1 = ng1temp.replaceAll(",", "").replaceAll("_", "");
|
|
321
|
+
const numberGroup2 = ng2temp === null || ng2temp === void 0 ? void 0 : ng2temp.replaceAll(",", "").replaceAll("_", "");
|
|
322
|
+
if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith(".")) finalResult = 0;
|
|
323
|
+
else {
|
|
324
|
+
if (opts.bigIntOnOverflow) {
|
|
325
|
+
const asBigInt = dash ? BigInt(`-${numberGroup1}`) : BigInt(numberGroup1);
|
|
326
|
+
if (asBigInt > BigInt(Number.MAX_SAFE_INTEGER) || asBigInt < BigInt(Number.MIN_SAFE_INTEGER)) return asBigInt;
|
|
327
|
+
}
|
|
328
|
+
finalResult = parseInt(numberGroup1);
|
|
329
|
+
}
|
|
330
|
+
if (!numberGroup2) return dash ? finalResult * -1 : finalResult;
|
|
331
|
+
const roundingFactor = opts.round === false ? NaN : parseFloat(`1e${Math.floor(Math.max(0, opts.round))}`);
|
|
332
|
+
if (numberGroup2.startsWith(".") || numberGroup2.startsWith("e") || numberGroup2.startsWith("E")) {
|
|
333
|
+
const decimalValue = parseFloat(`${finalResult}${numberGroup2}`);
|
|
334
|
+
finalResult = isNaN(roundingFactor) ? decimalValue : Math.round(decimalValue * roundingFactor) / roundingFactor;
|
|
335
|
+
} else if (spaceThenSlashRegex.test(numberGroup2)) {
|
|
336
|
+
const numerator = parseInt(numberGroup1);
|
|
337
|
+
const denominator = parseInt(numberGroup2.replace("/", ""));
|
|
338
|
+
finalResult = isNaN(roundingFactor) ? numerator / denominator : Math.round(numerator * roundingFactor / denominator) / roundingFactor;
|
|
339
|
+
} else {
|
|
340
|
+
const [numerator, denominator] = numberGroup2.split("/").map((v) => parseInt(v));
|
|
341
|
+
finalResult += isNaN(roundingFactor) ? numerator / denominator : Math.round(numerator * roundingFactor / denominator) / roundingFactor;
|
|
342
|
+
}
|
|
343
|
+
return dash ? finalResult * -1 : finalResult;
|
|
211
344
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
numericRegex,
|
|
216
|
-
numericRegexWithTrailingInvalid,
|
|
217
|
-
parseRomanNumerals,
|
|
218
|
-
romanNumeralRegex,
|
|
219
|
-
romanNumeralUnicodeRegex,
|
|
220
|
-
romanNumeralUnicodeToAsciiMap,
|
|
221
|
-
romanNumeralValues,
|
|
222
|
-
vulgarFractionToAsciiMap,
|
|
223
|
-
vulgarFractionsRegex
|
|
224
|
-
};
|
|
345
|
+
|
|
346
|
+
//#endregion
|
|
347
|
+
export { defaultOptions, normalizeDigits, numericQuantity, numericRegex, numericRegexWithTrailingInvalid, parseRomanNumerals, romanNumeralRegex, romanNumeralUnicodeRegex, romanNumeralUnicodeToAsciiMap, romanNumeralValues, vulgarFractionToAsciiMap, vulgarFractionsRegex };
|
|
225
348
|
//# sourceMappingURL=numeric-quantity.mjs.map
|