numeric-quantity 2.0.1 → 3.0.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 +247 -246
- 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 +301 -225
- package/dist/numeric-quantity.legacy-esm.js.map +1 -1
- package/dist/numeric-quantity.mjs +237 -210
- 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 +79 -0
- package/dist/types/dev.d.ts +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/numericQuantity.d.ts +12 -0
- package/dist/types/parseRomanNumerals.d.ts +8 -0
- package/dist/types/types.d.ts +49 -0
- package/dist/types-esm/constants.d.mts +79 -0
- package/dist/types-esm/dev.d.mts +1 -0
- package/dist/types-esm/index.d.mts +4 -0
- package/dist/types-esm/numericQuantity.d.mts +12 -0
- package/dist/types-esm/parseRomanNumerals.d.mts +8 -0
- package/dist/types-esm/types.d.mts +49 -0
- package/package.json +23 -18
- package/dist/cjs/numeric-quantity.cjs.development.d.ts +0 -222
- package/dist/cjs/numeric-quantity.cjs.production.d.ts +0 -222
- package/dist/numeric-quantity.d.mts +0 -222
- package/dist/numeric-quantity.legacy-esm.d.mts +0 -222
- package/dist/numeric-quantity.production.d.mts +0 -222
- package/dist/numeric-quantity.umd.min.js +0 -2
- package/dist/numeric-quantity.umd.min.js.map +0 -1
|
@@ -1,233 +1,309 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
//#region src/constants.ts
|
|
2
|
+
/**
|
|
3
|
+
* Map of Unicode fraction code points to their ASCII equivalents.
|
|
4
|
+
*/
|
|
5
|
+
const vulgarFractionToAsciiMap = {
|
|
6
|
+
"¼": "1/4",
|
|
7
|
+
"½": "1/2",
|
|
8
|
+
"¾": "3/4",
|
|
9
|
+
"⅐": "1/7",
|
|
10
|
+
"⅑": "1/9",
|
|
11
|
+
"⅒": "1/10",
|
|
12
|
+
"⅓": "1/3",
|
|
13
|
+
"⅔": "2/3",
|
|
14
|
+
"⅕": "1/5",
|
|
15
|
+
"⅖": "2/5",
|
|
16
|
+
"⅗": "3/5",
|
|
17
|
+
"⅘": "4/5",
|
|
18
|
+
"⅙": "1/6",
|
|
19
|
+
"⅚": "5/6",
|
|
20
|
+
"⅛": "1/8",
|
|
21
|
+
"⅜": "3/8",
|
|
22
|
+
"⅝": "5/8",
|
|
23
|
+
"⅞": "7/8",
|
|
24
|
+
"⅟": "1/"
|
|
16
25
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Captures the individual elements of a numeric string. Commas and underscores are allowed
|
|
28
|
+
* as separators, as long as they appear between digits and are not consecutive.
|
|
29
|
+
*
|
|
30
|
+
* Capture groups:
|
|
31
|
+
*
|
|
32
|
+
* | # | Description | Example(s) |
|
|
33
|
+
* | --- | ------------------------------------------------ | ------------------------------------------------------------------- |
|
|
34
|
+
* | `0` | entire string | `"2 1/3"` from `"2 1/3"` |
|
|
35
|
+
* | `1` | "negative" dash | `"-"` from `"-2 1/3"` |
|
|
36
|
+
* | `2` | whole number or numerator | `"2"` from `"2 1/3"`; `"1"` from `"1/3"` |
|
|
37
|
+
* | `3` | entire fraction, decimal portion, or denominator | `" 1/3"` from `"2 1/3"`; `".33"` from `"2.33"`; `"/3"` from `"1/3"` |
|
|
38
|
+
*
|
|
39
|
+
* _Capture group 2 may include comma/underscore separators._
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* numericRegex.exec("1") // [ "1", "1", null, null ]
|
|
45
|
+
* numericRegex.exec("1.23") // [ "1.23", "1", ".23", null ]
|
|
46
|
+
* numericRegex.exec("1 2/3") // [ "1 2/3", "1", " 2/3", " 2" ]
|
|
47
|
+
* numericRegex.exec("2/3") // [ "2/3", "2", "/3", null ]
|
|
48
|
+
* numericRegex.exec("2 / 3") // [ "2 / 3", "2", "/ 3", null ]
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
const numericRegex = new RegExp("^(?=-?\\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)*)?$", "");
|
|
52
|
+
/**
|
|
53
|
+
* Same as {@link numericRegex}, but allows (and ignores) trailing invalid characters.
|
|
54
|
+
*/
|
|
55
|
+
const numericRegexWithTrailingInvalid = new RegExp("^(?=-?\\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/].*)?", "");
|
|
56
|
+
/**
|
|
57
|
+
* Captures any Unicode vulgar fractions.
|
|
58
|
+
*/
|
|
59
|
+
const vulgarFractionsRegex = new RegExp("([¼½¾⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞⅟}])", "g");
|
|
60
|
+
/**
|
|
61
|
+
* Map of Roman numeral sequences to their decimal equivalents.
|
|
62
|
+
*/
|
|
63
|
+
const romanNumeralValues = {
|
|
64
|
+
MMM: 3e3,
|
|
65
|
+
MM: 2e3,
|
|
66
|
+
M: 1e3,
|
|
67
|
+
CM: 900,
|
|
68
|
+
DCCC: 800,
|
|
69
|
+
DCC: 700,
|
|
70
|
+
DC: 600,
|
|
71
|
+
D: 500,
|
|
72
|
+
CD: 400,
|
|
73
|
+
CCC: 300,
|
|
74
|
+
CC: 200,
|
|
75
|
+
C: 100,
|
|
76
|
+
XC: 90,
|
|
77
|
+
LXXX: 80,
|
|
78
|
+
LXX: 70,
|
|
79
|
+
LX: 60,
|
|
80
|
+
L: 50,
|
|
81
|
+
XL: 40,
|
|
82
|
+
XXX: 30,
|
|
83
|
+
XX: 20,
|
|
84
|
+
XII: 12,
|
|
85
|
+
XI: 11,
|
|
86
|
+
X: 10,
|
|
87
|
+
IX: 9,
|
|
88
|
+
VIII: 8,
|
|
89
|
+
VII: 7,
|
|
90
|
+
VI: 6,
|
|
91
|
+
V: 5,
|
|
92
|
+
IV: 4,
|
|
93
|
+
III: 3,
|
|
94
|
+
II: 2,
|
|
95
|
+
I: 1
|
|
82
96
|
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
"\u2171": "II",
|
|
120
|
-
// Small Roman Numeral Three (U+2172)
|
|
121
|
-
"\u2172": "III",
|
|
122
|
-
// Small Roman Numeral Four (U+2173)
|
|
123
|
-
"\u2173": "IV",
|
|
124
|
-
// Small Roman Numeral Five (U+2174)
|
|
125
|
-
"\u2174": "V",
|
|
126
|
-
// Small Roman Numeral Six (U+2175)
|
|
127
|
-
"\u2175": "VI",
|
|
128
|
-
// Small Roman Numeral Seven (U+2176)
|
|
129
|
-
"\u2176": "VII",
|
|
130
|
-
// Small Roman Numeral Eight (U+2177)
|
|
131
|
-
"\u2177": "VIII",
|
|
132
|
-
// Small Roman Numeral Nine (U+2178)
|
|
133
|
-
"\u2178": "IX",
|
|
134
|
-
// Small Roman Numeral Ten (U+2179)
|
|
135
|
-
"\u2179": "X",
|
|
136
|
-
// Small Roman Numeral Eleven (U+217A)
|
|
137
|
-
"\u217A": "XI",
|
|
138
|
-
// Small Roman Numeral Twelve (U+217B)
|
|
139
|
-
"\u217B": "XII",
|
|
140
|
-
// Small Roman Numeral Fifty (U+217C)
|
|
141
|
-
"\u217C": "L",
|
|
142
|
-
// Small Roman Numeral One Hundred (U+217D)
|
|
143
|
-
"\u217D": "C",
|
|
144
|
-
// Small Roman Numeral Five Hundred (U+217E)
|
|
145
|
-
"\u217E": "D",
|
|
146
|
-
// Small Roman Numeral One Thousand (U+217F)
|
|
147
|
-
"\u217F": "M"
|
|
97
|
+
/**
|
|
98
|
+
* Map of Unicode Roman numeral code points to their ASCII equivalents.
|
|
99
|
+
*/
|
|
100
|
+
const romanNumeralUnicodeToAsciiMap = {
|
|
101
|
+
Ⅰ: "I",
|
|
102
|
+
Ⅱ: "II",
|
|
103
|
+
Ⅲ: "III",
|
|
104
|
+
Ⅳ: "IV",
|
|
105
|
+
Ⅴ: "V",
|
|
106
|
+
Ⅵ: "VI",
|
|
107
|
+
Ⅶ: "VII",
|
|
108
|
+
Ⅷ: "VIII",
|
|
109
|
+
Ⅸ: "IX",
|
|
110
|
+
Ⅹ: "X",
|
|
111
|
+
Ⅺ: "XI",
|
|
112
|
+
Ⅻ: "XII",
|
|
113
|
+
Ⅼ: "L",
|
|
114
|
+
Ⅽ: "C",
|
|
115
|
+
Ⅾ: "D",
|
|
116
|
+
Ⅿ: "M",
|
|
117
|
+
ⅰ: "I",
|
|
118
|
+
ⅱ: "II",
|
|
119
|
+
ⅲ: "III",
|
|
120
|
+
ⅳ: "IV",
|
|
121
|
+
ⅴ: "V",
|
|
122
|
+
ⅵ: "VI",
|
|
123
|
+
ⅶ: "VII",
|
|
124
|
+
ⅷ: "VIII",
|
|
125
|
+
ⅸ: "IX",
|
|
126
|
+
ⅹ: "X",
|
|
127
|
+
ⅺ: "XI",
|
|
128
|
+
ⅻ: "XII",
|
|
129
|
+
ⅼ: "L",
|
|
130
|
+
ⅽ: "C",
|
|
131
|
+
ⅾ: "D",
|
|
132
|
+
ⅿ: "M"
|
|
148
133
|
};
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Captures all Unicode Roman numeral code points.
|
|
136
|
+
*/
|
|
137
|
+
const romanNumeralUnicodeRegex = new RegExp("([ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⅺⅻⅼⅽⅾⅿ])", "gi");
|
|
138
|
+
/**
|
|
139
|
+
* Captures a valid Roman numeral sequence.
|
|
140
|
+
*
|
|
141
|
+
* Capture groups:
|
|
142
|
+
*
|
|
143
|
+
* | # | Description | Example |
|
|
144
|
+
* | --- | --------------- | ------------------------ |
|
|
145
|
+
* | `0` | Entire string | "MCCXIV" from "MCCXIV" |
|
|
146
|
+
* | `1` | Thousands | "M" from "MCCXIV" |
|
|
147
|
+
* | `2` | Hundreds | "CC" from "MCCXIV" |
|
|
148
|
+
* | `3` | Tens | "X" from "MCCXIV" |
|
|
149
|
+
* | `4` | Ones | "IV" from "MCCXIV" |
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
*
|
|
153
|
+
* ```ts
|
|
154
|
+
* romanNumeralRegex.exec("M") // [ "M", "M", "", "", "" ]
|
|
155
|
+
* romanNumeralRegex.exec("XII") // [ "XII", "", "", "X", "II" ]
|
|
156
|
+
* romanNumeralRegex.exec("MCCXIV") // [ "MCCXIV", "M", "CC", "X", "IV" ]
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
const romanNumeralRegex = new RegExp("^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$", "i");
|
|
160
|
+
/**
|
|
161
|
+
* Default options for {@link numericQuantity}.
|
|
162
|
+
*/
|
|
163
|
+
const defaultOptions = {
|
|
164
|
+
round: 3,
|
|
165
|
+
allowTrailingInvalid: false,
|
|
166
|
+
romanNumerals: false,
|
|
167
|
+
bigIntOnOverflow: false,
|
|
168
|
+
decimalSeparator: "."
|
|
158
169
|
};
|
|
159
170
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/parseRomanNumerals.ts
|
|
173
|
+
/**
|
|
174
|
+
* Converts a string of Roman numerals to a number, like `parseInt`
|
|
175
|
+
* for Roman numerals. Uses modern, strict rules (only 1 to 3999).
|
|
176
|
+
*
|
|
177
|
+
* The string can include ASCII representations of Roman numerals
|
|
178
|
+
* or Unicode Roman numeral code points (`U+2160` through `U+217F`).
|
|
179
|
+
*/
|
|
180
|
+
const parseRomanNumerals = (romanNumerals) => {
|
|
181
|
+
var _romanNumeralValues, _romanNumeralValues2, _romanNumeralValues3, _romanNumeralValues4;
|
|
182
|
+
const normalized = `${romanNumerals}`.replace(romanNumeralUnicodeRegex, (_m, rn) => romanNumeralUnicodeToAsciiMap[rn]).toUpperCase();
|
|
183
|
+
const regexResult = romanNumeralRegex.exec(normalized);
|
|
184
|
+
if (!regexResult) return NaN;
|
|
185
|
+
const [, thousands, hundreds, tens, ones] = regexResult;
|
|
186
|
+
return ((_romanNumeralValues = romanNumeralValues[thousands]) !== null && _romanNumeralValues !== void 0 ? _romanNumeralValues : 0) + ((_romanNumeralValues2 = romanNumeralValues[hundreds]) !== null && _romanNumeralValues2 !== void 0 ? _romanNumeralValues2 : 0) + ((_romanNumeralValues3 = romanNumeralValues[tens]) !== null && _romanNumeralValues3 !== void 0 ? _romanNumeralValues3 : 0) + ((_romanNumeralValues4 = romanNumeralValues[ones]) !== null && _romanNumeralValues4 !== void 0 ? _romanNumeralValues4 : 0);
|
|
173
187
|
};
|
|
174
188
|
|
|
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
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region node_modules/@oxc-project/runtime/src/helpers/esm/typeof.js
|
|
191
|
+
function _typeof(o) {
|
|
192
|
+
"@babel/helpers - typeof";
|
|
193
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) {
|
|
194
|
+
return typeof o$1;
|
|
195
|
+
} : function(o$1) {
|
|
196
|
+
return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1;
|
|
197
|
+
}, _typeof(o);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
//#endregion
|
|
201
|
+
//#region node_modules/@oxc-project/runtime/src/helpers/esm/toPrimitive.js
|
|
202
|
+
function toPrimitive(t, r) {
|
|
203
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
204
|
+
var e = t[Symbol.toPrimitive];
|
|
205
|
+
if (void 0 !== e) {
|
|
206
|
+
var i = e.call(t, r || "default");
|
|
207
|
+
if ("object" != _typeof(i)) return i;
|
|
208
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
209
|
+
}
|
|
210
|
+
return ("string" === r ? String : Number)(t);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
//#endregion
|
|
214
|
+
//#region node_modules/@oxc-project/runtime/src/helpers/esm/toPropertyKey.js
|
|
215
|
+
function toPropertyKey(t) {
|
|
216
|
+
var i = toPrimitive(t, "string");
|
|
217
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region node_modules/@oxc-project/runtime/src/helpers/esm/defineProperty.js
|
|
222
|
+
function _defineProperty(e, r, t) {
|
|
223
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
224
|
+
value: t,
|
|
225
|
+
enumerable: !0,
|
|
226
|
+
configurable: !0,
|
|
227
|
+
writable: !0
|
|
228
|
+
}) : e[r] = t, e;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
//#endregion
|
|
232
|
+
//#region node_modules/@oxc-project/runtime/src/helpers/esm/objectSpread2.js
|
|
233
|
+
function ownKeys(e, r) {
|
|
234
|
+
var t = Object.keys(e);
|
|
235
|
+
if (Object.getOwnPropertySymbols) {
|
|
236
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
237
|
+
r && (o = o.filter(function(r$1) {
|
|
238
|
+
return Object.getOwnPropertyDescriptor(e, r$1).enumerable;
|
|
239
|
+
})), t.push.apply(t, o);
|
|
240
|
+
}
|
|
241
|
+
return t;
|
|
242
|
+
}
|
|
243
|
+
function _objectSpread2(e) {
|
|
244
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
245
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
246
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function(r$1) {
|
|
247
|
+
_defineProperty(e, r$1, t[r$1]);
|
|
248
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r$1) {
|
|
249
|
+
Object.defineProperty(e, r$1, Object.getOwnPropertyDescriptor(t, r$1));
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
return e;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
//#endregion
|
|
256
|
+
//#region src/numericQuantity.ts
|
|
257
|
+
const spaceThenSlashRegex = /^\s*\//;
|
|
258
|
+
function numericQuantity(quantity, options = defaultOptions) {
|
|
259
|
+
if (typeof quantity === "number" || typeof quantity === "bigint") return quantity;
|
|
260
|
+
let finalResult = NaN;
|
|
261
|
+
const quantityAsString = `${quantity}`.replace(vulgarFractionsRegex, (_m, vf) => ` ${vulgarFractionToAsciiMap[vf]}`).replace("⁄", "/").trim();
|
|
262
|
+
if (quantityAsString.length === 0) return NaN;
|
|
263
|
+
const opts = _objectSpread2(_objectSpread2({}, defaultOptions), options);
|
|
264
|
+
let normalizedString = quantityAsString;
|
|
265
|
+
if (opts.decimalSeparator === ",") {
|
|
266
|
+
const commaCount = (quantityAsString.match(/,/g) || []).length;
|
|
267
|
+
if (commaCount === 1) normalizedString = quantityAsString.replaceAll(".", "_").replace(",", ".");
|
|
268
|
+
else if (commaCount > 1) {
|
|
269
|
+
if (!opts.allowTrailingInvalid) return NaN;
|
|
270
|
+
const firstCommaIndex = quantityAsString.indexOf(",");
|
|
271
|
+
const secondCommaIndex = quantityAsString.indexOf(",", firstCommaIndex + 1);
|
|
272
|
+
const beforeSecondComma = quantityAsString.substring(0, secondCommaIndex).replaceAll(".", "_").replace(",", ".");
|
|
273
|
+
const afterSecondComma = quantityAsString.substring(secondCommaIndex + 1);
|
|
274
|
+
normalizedString = opts.allowTrailingInvalid ? beforeSecondComma + "&" + afterSecondComma : beforeSecondComma;
|
|
275
|
+
} else normalizedString = quantityAsString.replaceAll(".", "_");
|
|
276
|
+
}
|
|
277
|
+
const regexResult = (opts.allowTrailingInvalid ? numericRegexWithTrailingInvalid : numericRegex).exec(normalizedString);
|
|
278
|
+
if (!regexResult) return opts.romanNumerals ? parseRomanNumerals(quantityAsString) : NaN;
|
|
279
|
+
const [, dash, ng1temp, ng2temp] = regexResult;
|
|
280
|
+
const numberGroup1 = ng1temp.replaceAll(",", "").replaceAll("_", "");
|
|
281
|
+
const numberGroup2 = ng2temp === null || ng2temp === void 0 ? void 0 : ng2temp.replaceAll(",", "").replaceAll("_", "");
|
|
282
|
+
if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith(".")) finalResult = 0;
|
|
283
|
+
else {
|
|
284
|
+
if (opts.bigIntOnOverflow) {
|
|
285
|
+
const asBigInt = dash ? BigInt(`-${numberGroup1}`) : BigInt(numberGroup1);
|
|
286
|
+
if (asBigInt > BigInt(Number.MAX_SAFE_INTEGER) || asBigInt < BigInt(Number.MIN_SAFE_INTEGER)) return asBigInt;
|
|
287
|
+
}
|
|
288
|
+
finalResult = parseInt(numberGroup1);
|
|
289
|
+
}
|
|
290
|
+
if (!numberGroup2) return dash ? finalResult * -1 : finalResult;
|
|
291
|
+
const roundingFactor = opts.round === false ? NaN : parseFloat(`1e${Math.floor(Math.max(0, opts.round))}`);
|
|
292
|
+
if (numberGroup2.startsWith(".") || numberGroup2.startsWith("e") || numberGroup2.startsWith("E")) {
|
|
293
|
+
const decimalValue = parseFloat(`${finalResult}${numberGroup2}`);
|
|
294
|
+
finalResult = isNaN(roundingFactor) ? decimalValue : Math.round(decimalValue * roundingFactor) / roundingFactor;
|
|
295
|
+
} else if (spaceThenSlashRegex.test(numberGroup2)) {
|
|
296
|
+
const numerator = parseInt(numberGroup1);
|
|
297
|
+
const denominator = parseInt(numberGroup2.replace("/", ""));
|
|
298
|
+
finalResult = isNaN(roundingFactor) ? numerator / denominator : Math.round(numerator * roundingFactor / denominator) / roundingFactor;
|
|
299
|
+
} else {
|
|
300
|
+
const fractionArray = numberGroup2.split("/");
|
|
301
|
+
const [numerator, denominator] = fractionArray.map((v) => parseInt(v));
|
|
302
|
+
finalResult += isNaN(roundingFactor) ? numerator / denominator : Math.round(numerator * roundingFactor / denominator) / roundingFactor;
|
|
303
|
+
}
|
|
304
|
+
return dash ? finalResult * -1 : finalResult;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
//#endregion
|
|
308
|
+
export { defaultOptions, numericQuantity, numericRegex, numericRegexWithTrailingInvalid, parseRomanNumerals, romanNumeralRegex, romanNumeralUnicodeRegex, romanNumeralUnicodeToAsciiMap, romanNumeralValues, vulgarFractionToAsciiMap, vulgarFractionsRegex };
|
|
233
309
|
//# sourceMappingURL=numeric-quantity.legacy-esm.js.map
|