numeric-quantity 3.1.0 → 3.2.1
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 +173 -17
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/numeric-quantity.cjs.development.d.ts +241 -0
- package/dist/cjs/numeric-quantity.cjs.development.js +132 -16
- package/dist/cjs/numeric-quantity.cjs.development.js.map +1 -1
- package/dist/cjs/numeric-quantity.cjs.production.d.ts +241 -0
- 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.d.mts +241 -0
- package/dist/numeric-quantity.iife.umd.min.js +1 -1
- package/dist/numeric-quantity.iife.umd.min.js.map +1 -1
- package/dist/numeric-quantity.legacy-esm.d.ts +241 -0
- package/dist/numeric-quantity.legacy-esm.js +127 -17
- package/dist/numeric-quantity.legacy-esm.js.map +1 -1
- package/dist/numeric-quantity.mjs +130 -17
- package/dist/numeric-quantity.mjs.map +1 -1
- package/dist/numeric-quantity.production.d.mts +241 -0
- package/dist/numeric-quantity.production.mjs +1 -1
- package/dist/numeric-quantity.production.mjs.map +1 -1
- package/package.json +5 -6
- package/dist/types/constants.d.ts +0 -87
- package/dist/types/dev.d.ts +0 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/numericQuantity.d.ts +0 -12
- package/dist/types/parseRomanNumerals.d.ts +0 -8
- package/dist/types/types.d.ts +0 -49
- package/dist/types-esm/constants.d.mts +0 -87
- package/dist/types-esm/dev.d.mts +0 -1
- package/dist/types-esm/index.d.mts +0 -4
- package/dist/types-esm/numericQuantity.d.mts +0 -12
- package/dist/types-esm/parseRomanNumerals.d.mts +0 -8
- package/dist/types-esm/types.d.mts +0 -49
package/README.md
CHANGED
|
@@ -4,20 +4,16 @@
|
|
|
4
4
|
[](https://npm-stat.com/charts.html?package=numeric-quantity&from=2015-08-01)
|
|
5
5
|
[](https://opensource.org/licenses/MIT)
|
|
6
6
|
|
|
7
|
-
Converts a string to a number, like an enhanced version of `parseFloat`.
|
|
7
|
+
Converts a string to a number, like an enhanced version of `parseFloat`. Returns `NaN` if the provided string does not resemble a number.
|
|
8
8
|
|
|
9
9
|
**[Full documentation](https://jakeboone02.github.io/numeric-quantity/)**
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
In addition to plain integers and decimals, `numeric-quantity` handles:
|
|
12
12
|
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
- To parse numbers with European-style decimal comma (where `'1,0'` means `1`, not `10`), pass `{ decimalSeparator: ',' }` as the second argument.
|
|
18
|
-
- To produce `bigint` values when the input represents an integer that would exceeds the boundaries of `number`, pass `{ bigIntOnOverflow: true }` as the second argument.
|
|
19
|
-
- Results will be rounded to three decimal places by default. To avoid rounding, pass `{ round: false }` as the second argument. To round to a different number of decimal places, assign that number to the `round` option (`{ round: 5 }` will round to five decimal places).
|
|
20
|
-
- Returns `NaN` if the provided string does not resemble a number.
|
|
13
|
+
- **Fractions and mixed numbers**: `'1 2/3'` → `1.667`, `'1⅖'` → `1.4`, `'1 2⁄3'` → `1.667`
|
|
14
|
+
- **Separators**: `'1,000'` → `1000`, `'1_000_000'` → `1000000`
|
|
15
|
+
- **Roman numerals** (see [option](#roman-numerals-romannumerals) below): `'XIV'` → `14`, `'Ⅻ'` → `12`
|
|
16
|
+
- **Non-ASCII numerals**: Arabic-Indic (`'٣'`), Devanagari (`'३'`), Bengali, Thai, Fullwidth, and 70+ other Unicode digit scripts
|
|
21
17
|
|
|
22
18
|
> _For the inverse operation—converting a number to an imperial measurement—check out [format-quantity](https://www.npmjs.com/package/format-quantity)._
|
|
23
19
|
|
|
@@ -57,12 +53,172 @@ As UMD (all exports are properties of the global object `NumericQuantity`):
|
|
|
57
53
|
|
|
58
54
|
## Options
|
|
59
55
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
56
|
+
All options are passed as the second argument to `numericQuantity` (and `isNumericQuantity`).
|
|
57
|
+
|
|
58
|
+
### Rounding (`round`)
|
|
59
|
+
|
|
60
|
+
Results are rounded to three decimal places by default. Use the `round` option to change this behavior.
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
numericQuantity('1/3'); // 0.333 (default: 3 decimal places)
|
|
64
|
+
numericQuantity('1/3', { round: 5 }); // 0.33333
|
|
65
|
+
numericQuantity('1/3', { round: false }); // 0.3333333333333333
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Trailing Invalid Characters (`allowTrailingInvalid`)
|
|
69
|
+
|
|
70
|
+
By default, strings with trailing non-numeric characters return `NaN`. Set `allowTrailingInvalid: true` to ignore trailing invalid characters, similar to `parseFloat`.
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
numericQuantity('100abc'); // NaN
|
|
74
|
+
numericQuantity('100abc', { allowTrailingInvalid: true }); // 100
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Roman Numerals (`romanNumerals`)
|
|
78
|
+
|
|
79
|
+
Parse Roman numerals (ASCII or Unicode) by setting `romanNumerals: true`. You can also use `parseRomanNumerals` directly.
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
numericQuantity('MCCXIV', { romanNumerals: true }); // 1214
|
|
83
|
+
numericQuantity('Ⅻ', { romanNumerals: true }); // 12
|
|
84
|
+
numericQuantity('xiv', { romanNumerals: true }); // 14 (case-insensitive)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Decimal Separator (`decimalSeparator`)
|
|
88
|
+
|
|
89
|
+
For European-style numbers where comma is the decimal separator, set `decimalSeparator: ','`.
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
numericQuantity('1,5'); // 15 (comma treated as thousands separator)
|
|
93
|
+
numericQuantity('1,5', { decimalSeparator: ',' }); // 1.5
|
|
94
|
+
numericQuantity('1.000,50', { decimalSeparator: ',' }); // 1000.5
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Large Integers (`bigIntOnOverflow`)
|
|
98
|
+
|
|
99
|
+
When parsing integers that exceed `Number.MAX_SAFE_INTEGER` or are less than `Number.MIN_SAFE_INTEGER`, set `bigIntOnOverflow: true` to return a `bigint` instead.
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
numericQuantity('9007199254740992'); // 9007199254740992 (loses precision)
|
|
103
|
+
numericQuantity('9007199254740992', { bigIntOnOverflow: true }); // 9007199254740992n
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Percentages (`percentage`)
|
|
107
|
+
|
|
108
|
+
Parse percentage strings by setting the `percentage` option. Use `'decimal'` (or `true`) to divide by 100, or `'number'` to just strip the `%` symbol.
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
numericQuantity('50%'); // NaN
|
|
112
|
+
numericQuantity('50%', { percentage: true }); // 0.5
|
|
113
|
+
numericQuantity('50%', { percentage: 'decimal' }); // 0.5
|
|
114
|
+
numericQuantity('50%', { percentage: 'number' }); // 50
|
|
115
|
+
numericQuantity('1/2%', { percentage: true }); // 0.005
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Currency Symbols (`allowCurrency`)
|
|
119
|
+
|
|
120
|
+
Strip currency symbols from the start or end of the string by setting `allowCurrency: true`. Supports all Unicode currency symbols (`$`, `€`, `£`, `¥`, `₹`, `₽`, `₿`, `₩`, etc.).
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
numericQuantity('$100'); // NaN
|
|
124
|
+
numericQuantity('$100', { allowCurrency: true }); // 100
|
|
125
|
+
numericQuantity('€1.000,50', { allowCurrency: true, decimalSeparator: ',' }); // 1000.5
|
|
126
|
+
numericQuantity('100€', { allowCurrency: true }); // 100
|
|
127
|
+
numericQuantity('-$50', { allowCurrency: true }); // -50
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Verbose Output (`verbose`)
|
|
131
|
+
|
|
132
|
+
Set `verbose: true` to return a detailed result object instead of just the numeric value. This is useful for understanding what was parsed and stripped.
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
numericQuantity('$50%', {
|
|
136
|
+
verbose: true,
|
|
137
|
+
allowCurrency: true,
|
|
138
|
+
percentage: true,
|
|
139
|
+
});
|
|
140
|
+
// {
|
|
141
|
+
// value: 0.5,
|
|
142
|
+
// input: '$50%',
|
|
143
|
+
// currencyPrefix: '$',
|
|
144
|
+
// percentageSuffix: true
|
|
145
|
+
// }
|
|
146
|
+
|
|
147
|
+
numericQuantity('100abc', {
|
|
148
|
+
verbose: true,
|
|
149
|
+
allowTrailingInvalid: true,
|
|
150
|
+
});
|
|
151
|
+
// {
|
|
152
|
+
// value: 100,
|
|
153
|
+
// input: '100abc',
|
|
154
|
+
// trailingInvalid: 'abc'
|
|
155
|
+
// }
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
For fraction and mixed-number inputs, the result also includes parsed fraction components (always unsigned):
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
numericQuantity('1 2/3', { verbose: true });
|
|
162
|
+
// {
|
|
163
|
+
// value: 1.667,
|
|
164
|
+
// input: '1 2/3',
|
|
165
|
+
// whole: 1,
|
|
166
|
+
// numerator: 2,
|
|
167
|
+
// denominator: 3
|
|
168
|
+
// }
|
|
169
|
+
|
|
170
|
+
numericQuantity('½', { verbose: true });
|
|
171
|
+
// {
|
|
172
|
+
// value: 0.5,
|
|
173
|
+
// input: '½',
|
|
174
|
+
// numerator: 1,
|
|
175
|
+
// denominator: 2
|
|
176
|
+
// }
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
The verbose result object has the following shape:
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
interface NumericQuantityVerboseResult {
|
|
183
|
+
value: number | bigint; // The parsed value (NaN if invalid)
|
|
184
|
+
input: string; // Original input string
|
|
185
|
+
currencyPrefix?: string; // Currency symbol(s) stripped from start
|
|
186
|
+
currencySuffix?: string; // Currency symbol(s) stripped from end
|
|
187
|
+
percentageSuffix?: boolean; // True if "%" was stripped
|
|
188
|
+
trailingInvalid?: string; // Characters ignored (if allowTrailingInvalid)
|
|
189
|
+
sign?: '-' | '+'; // Leading sign character, if present
|
|
190
|
+
whole?: number; // Whole part of a mixed fraction (e.g. 1 from "1 2/3")
|
|
191
|
+
numerator?: number; // Fraction numerator (e.g. 2 from "1 2/3")
|
|
192
|
+
denominator?: number; // Fraction denominator (e.g. 3 from "1 2/3")
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Additional Exports
|
|
197
|
+
|
|
198
|
+
### `isNumericQuantity(str, options?): boolean`
|
|
199
|
+
|
|
200
|
+
Returns `true` if the string can be parsed as a valid number, `false` otherwise. Accepts the same options as `numericQuantity`.
|
|
201
|
+
|
|
202
|
+
```js
|
|
203
|
+
import { isNumericQuantity } from 'numeric-quantity';
|
|
204
|
+
|
|
205
|
+
isNumericQuantity('1 1/2'); // true
|
|
206
|
+
isNumericQuantity('abc'); // false
|
|
207
|
+
isNumericQuantity('XII', { romanNumerals: true }); // true
|
|
208
|
+
isNumericQuantity('$100', { allowCurrency: true }); // true
|
|
209
|
+
isNumericQuantity('50%', { percentage: true }); // true
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### `parseRomanNumerals(str): number`
|
|
213
|
+
|
|
214
|
+
Parses a string of Roman numerals directly. Returns `NaN` for invalid input.
|
|
215
|
+
|
|
216
|
+
```js
|
|
217
|
+
import { parseRomanNumerals } from 'numeric-quantity';
|
|
218
|
+
|
|
219
|
+
parseRomanNumerals('MCMXCIX'); // 1999
|
|
220
|
+
parseRomanNumerals('Ⅻ'); // 12
|
|
221
|
+
parseRomanNumerals('invalid'); // NaN
|
|
222
|
+
```
|
|
67
223
|
|
|
68
224
|
[badge-npm]: https://img.shields.io/npm/v/numeric-quantity.svg?cacheSeconds=3600&logo=npm
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './numeric-quantity.cjs.development.js';
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface NumericQuantityOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Round the result to this many decimal places. Defaults to 3; must
|
|
5
|
+
* be greater than or equal to zero.
|
|
6
|
+
*
|
|
7
|
+
* @default 3
|
|
8
|
+
*/
|
|
9
|
+
round?: number | false;
|
|
10
|
+
/**
|
|
11
|
+
* Allow and ignore trailing invalid characters _à la_ `parseFloat`.
|
|
12
|
+
*
|
|
13
|
+
* @default false
|
|
14
|
+
*/
|
|
15
|
+
allowTrailingInvalid?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Attempt to parse Roman numerals if Arabic numeral parsing fails.
|
|
18
|
+
*
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
romanNumerals?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Generates a `bigint` value if the string represents
|
|
24
|
+
* a valid integer too large for the `number` type.
|
|
25
|
+
*/
|
|
26
|
+
bigIntOnOverflow?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Specifies which character ("." or ",") to treat as the decimal separator.
|
|
29
|
+
*
|
|
30
|
+
* @default "."
|
|
31
|
+
*/
|
|
32
|
+
decimalSeparator?: "," | ".";
|
|
33
|
+
/**
|
|
34
|
+
* Allow and strip currency symbols (Unicode `\p{Sc}` category) from the
|
|
35
|
+
* start and/or end of the string.
|
|
36
|
+
*
|
|
37
|
+
* @default false
|
|
38
|
+
*/
|
|
39
|
+
allowCurrency?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Parse percentage strings by stripping the `%` suffix.
|
|
42
|
+
* - `'decimal'` or `true`: `"50%"` → `0.5` (divide by 100)
|
|
43
|
+
* - `'number'`: `"50%"` → `50` (strip `%`, keep value)
|
|
44
|
+
* - `false` or omitted: `"50%"` → `NaN` (default behavior)
|
|
45
|
+
*
|
|
46
|
+
* @default false
|
|
47
|
+
*/
|
|
48
|
+
percentage?: "decimal" | "number" | boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Return a verbose result object with additional parsing metadata.
|
|
51
|
+
*
|
|
52
|
+
* @default false
|
|
53
|
+
*/
|
|
54
|
+
verbose?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Resolves the return type of {@link numericQuantity} based on the options provided.
|
|
58
|
+
*/
|
|
59
|
+
type NumericQuantityReturnType<T extends NumericQuantityOptions | undefined = undefined> = T extends {
|
|
60
|
+
verbose: true;
|
|
61
|
+
} ? NumericQuantityVerboseResult : T extends {
|
|
62
|
+
bigIntOnOverflow: true;
|
|
63
|
+
} ? number | bigint : number;
|
|
64
|
+
/**
|
|
65
|
+
* Verbose result returned when `verbose: true` is set.
|
|
66
|
+
*/
|
|
67
|
+
interface NumericQuantityVerboseResult {
|
|
68
|
+
/** The parsed numeric value (NaN if invalid). */
|
|
69
|
+
value: number | bigint;
|
|
70
|
+
/** The original input string. */
|
|
71
|
+
input: string;
|
|
72
|
+
/** Currency symbol(s) stripped from the start, if any. */
|
|
73
|
+
currencyPrefix?: string;
|
|
74
|
+
/** Currency symbol(s) stripped from the end, if any. */
|
|
75
|
+
currencySuffix?: string;
|
|
76
|
+
/** True if a `%` suffix was stripped. */
|
|
77
|
+
percentageSuffix?: boolean;
|
|
78
|
+
/** Trailing invalid (usually non-numeric) characters detected in the input, if any. Populated even when `allowTrailingInvalid` is `false`. */
|
|
79
|
+
trailingInvalid?: string;
|
|
80
|
+
/** The leading sign character (`'-'` or `'+'`), if present. Omitted when no explicit sign was in the input. */
|
|
81
|
+
sign?: "-" | "+";
|
|
82
|
+
/** The whole-number part of a mixed fraction (e.g. `1` from `"1 2/3"`). Omitted for pure fractions, decimals, and integers. */
|
|
83
|
+
whole?: number;
|
|
84
|
+
/** The numerator of a fraction (e.g. `2` from `"1 2/3"`, or `1` from `"1/2"`). Always unsigned. */
|
|
85
|
+
numerator?: number;
|
|
86
|
+
/** The denominator of a fraction (e.g. `3` from `"1 2/3"`, or `2` from `"1/2"`). Always unsigned. */
|
|
87
|
+
denominator?: number;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Unicode vulgar fraction code points.
|
|
91
|
+
*/
|
|
92
|
+
type VulgarFraction = "¼" | "½" | "¾" | "⅐" | "⅑" | "⅒" | "⅓" | "⅔" | "⅕" | "⅖" | "⅗" | "⅘" | "⅙" | "⅚" | "⅛" | "⅜" | "⅝" | "⅞" | "⅟";
|
|
93
|
+
/**
|
|
94
|
+
* Unicode superscript digit code points.
|
|
95
|
+
*/
|
|
96
|
+
type SuperscriptDigit = "⁰" | "¹" | "²" | "³" | "⁴" | "⁵" | "⁶" | "⁷" | "⁸" | "⁹";
|
|
97
|
+
/**
|
|
98
|
+
* Unicode subscript digit code points.
|
|
99
|
+
*/
|
|
100
|
+
type SubscriptDigit = "₀" | "₁" | "₂" | "₃" | "₄" | "₅" | "₆" | "₇" | "₈" | "₉";
|
|
101
|
+
/**
|
|
102
|
+
* Allowable Roman numeral characters (ASCII, uppercase only).
|
|
103
|
+
*/
|
|
104
|
+
type RomanNumeralAscii = "I" | "V" | "X" | "L" | "C" | "D" | "M";
|
|
105
|
+
/**
|
|
106
|
+
* Unicode Roman numeral code points (uppercase and lowercase,
|
|
107
|
+
* representing 1-12, 50, 100, 500, and 1000).
|
|
108
|
+
*/
|
|
109
|
+
type RomanNumeralUnicode = "Ⅰ" | "Ⅱ" | "Ⅲ" | "Ⅳ" | "Ⅴ" | "Ⅵ" | "Ⅶ" | "Ⅷ" | "Ⅸ" | "Ⅹ" | "Ⅺ" | "Ⅻ" | "Ⅼ" | "Ⅽ" | "Ⅾ" | "Ⅿ" | "ⅰ" | "ⅱ" | "ⅲ" | "ⅳ" | "ⅴ" | "ⅵ" | "ⅶ" | "ⅷ" | "ⅸ" | "ⅹ" | "ⅺ" | "ⅻ" | "ⅼ" | "ⅽ" | "ⅾ" | "ⅿ";
|
|
110
|
+
/**
|
|
111
|
+
* Union of ASCII and Unicode Roman numeral characters/code points.
|
|
112
|
+
*/
|
|
113
|
+
type RomanNumeral = RomanNumeralAscii | RomanNumeralUnicode;
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/constants.d.ts
|
|
116
|
+
/**
|
|
117
|
+
* Normalizes non-ASCII decimal digits to ASCII digits.
|
|
118
|
+
* Converts characters from Unicode decimal digit blocks (e.g., Arabic-Indic,
|
|
119
|
+
* Devanagari, Bengali) to their ASCII equivalents (0-9).
|
|
120
|
+
*
|
|
121
|
+
* All current Unicode \p{Nd} blocks are included in decimalDigitBlockStarts.
|
|
122
|
+
*/
|
|
123
|
+
declare const normalizeDigits: (str: string) => string;
|
|
124
|
+
/**
|
|
125
|
+
* Map of Unicode superscript and subscript digit code points to ASCII digits.
|
|
126
|
+
*/
|
|
127
|
+
declare const superSubDigitToAsciiMap: Record<SuperscriptDigit | SubscriptDigit, string>;
|
|
128
|
+
/**
|
|
129
|
+
* Captures Unicode superscript and subscript digits.
|
|
130
|
+
*/
|
|
131
|
+
declare const superSubDigitsRegex: RegExp;
|
|
132
|
+
/**
|
|
133
|
+
* Map of Unicode fraction code points to their ASCII equivalents.
|
|
134
|
+
*/
|
|
135
|
+
declare const vulgarFractionToAsciiMap: Record<VulgarFraction, `${number}/${number | ""}`>;
|
|
136
|
+
/**
|
|
137
|
+
* Captures the individual elements of a numeric string. Commas and underscores are allowed
|
|
138
|
+
* as separators, as long as they appear between digits and are not consecutive.
|
|
139
|
+
*
|
|
140
|
+
* Capture groups:
|
|
141
|
+
*
|
|
142
|
+
* | # | Description | Example(s) |
|
|
143
|
+
* | --- | ------------------------------------------------ | ------------------------------------------------------------------- |
|
|
144
|
+
* | `0` | entire string | `"2 1/3"` from `"2 1/3"` |
|
|
145
|
+
* | `1` | sign (`-` or `+`) | `"-"` from `"-2 1/3"` |
|
|
146
|
+
* | `2` | whole number or numerator | `"2"` from `"2 1/3"`; `"1"` from `"1/3"` |
|
|
147
|
+
* | `3` | entire fraction, decimal portion, or denominator | `" 1/3"` from `"2 1/3"`; `".33"` from `"2.33"`; `"/3"` from `"1/3"` |
|
|
148
|
+
*
|
|
149
|
+
* _Capture group 2 may include comma/underscore separators._
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
*
|
|
153
|
+
* ```ts
|
|
154
|
+
* numericRegex.exec("1") // [ "1", "1", null, null ]
|
|
155
|
+
* numericRegex.exec("1.23") // [ "1.23", "1", ".23", null ]
|
|
156
|
+
* numericRegex.exec("1 2/3") // [ "1 2/3", "1", " 2/3", " 2" ]
|
|
157
|
+
* numericRegex.exec("2/3") // [ "2/3", "2", "/3", null ]
|
|
158
|
+
* numericRegex.exec("2 / 3") // [ "2 / 3", "2", "/ 3", null ]
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
declare const numericRegex: RegExp;
|
|
162
|
+
/**
|
|
163
|
+
* Same as {@link numericRegex}, but allows (and ignores) trailing invalid characters.
|
|
164
|
+
* Capture group 7 contains the trailing invalid portion.
|
|
165
|
+
*/
|
|
166
|
+
declare const numericRegexWithTrailingInvalid: RegExp;
|
|
167
|
+
/**
|
|
168
|
+
* Captures any Unicode vulgar fractions.
|
|
169
|
+
*/
|
|
170
|
+
declare const vulgarFractionsRegex: RegExp;
|
|
171
|
+
type RomanNumeralSequenceFragment = `${RomanNumeralAscii}` | `${RomanNumeralAscii}${RomanNumeralAscii}` | `${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}` | `${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}`;
|
|
172
|
+
/**
|
|
173
|
+
* Map of Roman numeral sequences to their decimal equivalents.
|
|
174
|
+
*/
|
|
175
|
+
declare const romanNumeralValues: { [k in RomanNumeralSequenceFragment]?: number };
|
|
176
|
+
/**
|
|
177
|
+
* Map of Unicode Roman numeral code points to their ASCII equivalents.
|
|
178
|
+
*/
|
|
179
|
+
declare const romanNumeralUnicodeToAsciiMap: Record<RomanNumeralUnicode, keyof typeof romanNumeralValues>;
|
|
180
|
+
/**
|
|
181
|
+
* Captures all Unicode Roman numeral code points.
|
|
182
|
+
*/
|
|
183
|
+
declare const romanNumeralUnicodeRegex: RegExp;
|
|
184
|
+
/**
|
|
185
|
+
* Captures a valid Roman numeral sequence.
|
|
186
|
+
*
|
|
187
|
+
* Capture groups:
|
|
188
|
+
*
|
|
189
|
+
* | # | Description | Example |
|
|
190
|
+
* | --- | --------------- | ------------------------ |
|
|
191
|
+
* | `0` | Entire string | "MCCXIV" from "MCCXIV" |
|
|
192
|
+
* | `1` | Thousands | "M" from "MCCXIV" |
|
|
193
|
+
* | `2` | Hundreds | "CC" from "MCCXIV" |
|
|
194
|
+
* | `3` | Tens | "X" from "MCCXIV" |
|
|
195
|
+
* | `4` | Ones | "IV" from "MCCXIV" |
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
*
|
|
199
|
+
* ```ts
|
|
200
|
+
* romanNumeralRegex.exec("M") // [ "M", "M", "", "", "" ]
|
|
201
|
+
* romanNumeralRegex.exec("XII") // [ "XII", "", "", "X", "II" ]
|
|
202
|
+
* romanNumeralRegex.exec("MCCXIV") // [ "MCCXIV", "M", "CC", "X", "IV" ]
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
declare const romanNumeralRegex: RegExp;
|
|
206
|
+
/**
|
|
207
|
+
* Default options for {@link numericQuantity}.
|
|
208
|
+
*/
|
|
209
|
+
declare const defaultOptions: Required<NumericQuantityOptions>;
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region src/isNumericQuantity.d.ts
|
|
212
|
+
/**
|
|
213
|
+
* Checks if a string represents a valid numeric quantity.
|
|
214
|
+
*
|
|
215
|
+
* Returns `true` if the string can be parsed as a number, `false` otherwise.
|
|
216
|
+
* Accepts the same options as `numericQuantity`.
|
|
217
|
+
*/
|
|
218
|
+
declare const isNumericQuantity: (quantity: string | number, options?: NumericQuantityOptions) => boolean;
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region src/numericQuantity.d.ts
|
|
221
|
+
/**
|
|
222
|
+
* Converts a string to a number, like an enhanced version of `parseFloat`.
|
|
223
|
+
*
|
|
224
|
+
* The string can include mixed numbers, vulgar fractions, or Roman numerals.
|
|
225
|
+
*/
|
|
226
|
+
declare function numericQuantity(quantity: string | number): number;
|
|
227
|
+
declare function numericQuantity<T extends NumericQuantityOptions>(quantity: string | number, options: T): NumericQuantityReturnType<T>;
|
|
228
|
+
declare function numericQuantity(quantity: string | number, options?: NumericQuantityOptions): number;
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/parseRomanNumerals.d.ts
|
|
231
|
+
/**
|
|
232
|
+
* Converts a string of Roman numerals to a number, like `parseInt`
|
|
233
|
+
* for Roman numerals. Uses modern, strict rules (only 1 to 3999).
|
|
234
|
+
*
|
|
235
|
+
* The string can include ASCII representations of Roman numerals
|
|
236
|
+
* or Unicode Roman numeral code points (`U+2160` through `U+217F`).
|
|
237
|
+
*/
|
|
238
|
+
declare const parseRomanNumerals: (romanNumerals: string) => number;
|
|
239
|
+
//#endregion
|
|
240
|
+
export { NumericQuantityOptions, NumericQuantityReturnType, NumericQuantityVerboseResult, RomanNumeral, RomanNumeralAscii, RomanNumeralUnicode, SubscriptDigit, SuperscriptDigit, VulgarFraction, defaultOptions, isNumericQuantity, normalizeDigits, numericQuantity, numericRegex, numericRegexWithTrailingInvalid, parseRomanNumerals, romanNumeralRegex, romanNumeralUnicodeRegex, romanNumeralUnicodeToAsciiMap, romanNumeralValues, superSubDigitToAsciiMap, superSubDigitsRegex, vulgarFractionToAsciiMap, vulgarFractionsRegex };
|
|
241
|
+
//# sourceMappingURL=numeric-quantity.cjs.development.d.ts.map
|