numeric-quantity 1.0.4 → 2.0.0-beta.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 CHANGED
@@ -1,47 +1,55 @@
1
1
  # numeric-quantity
2
2
 
3
- [![npm version](https://badge.fury.io/js/numeric-quantity.svg)](//npmjs.com/package/numeric-quantity)
4
- ![workflow status](https://github.com/jakeboone02/numeric-quantity/workflows/Continuous%20Integration/badge.svg)
5
- [![codecov.io](https://codecov.io/github/jakeboone02/numeric-quantity/coverage.svg?branch=master)](https://codecov.io/github/jakeboone02/numeric-quantity?branch=master)
3
+ [![npm][badge-npm]](https://www.npmjs.com/package/numeric-quantity)
4
+ ![workflow status](https://github.com/jakeboone02/numeric-quantity/actions/workflows/main.yml/badge.svg)
5
+ [![codecov.io](https://codecov.io/github/jakeboone02/numeric-quantity/coverage.svg?branch=master)](https://codecov.io/github/jakeboone02/numeric-quantity?branch=main)
6
6
  [![downloads](https://img.shields.io/npm/dm/numeric-quantity.svg)](http://npm-stat.com/charts.html?package=numeric-quantity&from=2015-08-01)
7
7
  [![MIT License](https://img.shields.io/npm/l/numeric-quantity.svg)](http://opensource.org/licenses/MIT)
8
8
 
9
- Converts a string to a number. The string can include mixed numbers or vulgar fractions.
9
+ Converts a string to a number, like an enhanced version of `parseFloat`.
10
10
 
11
- For the inverse operation (converting a number to an imperial measurement), check out [format-quantity](https://www.npmjs.com/package/format-quantity).
11
+ In addition to plain integers and decimals, `numeric-quantity` can parse numbers with comma or underscore separators (`'1,000'` or `'1_000'`), mixed numbers (`'1 2/3'`), vulgar fractions (`'1⅖'`), the fraction slash character (`'1 2⁄3'`), and Roman numerals (`'MCCXIV'` or `'Ⅻ'`). The return value will be `NaN` if the provided string does not resemble a number.
12
12
 
13
- For a more complete solution to parsing recipe ingredients, try [parse-ingredient](https://www.npmjs.com/package/parse-ingredient).
13
+ > _For the inverse operation—converting a number to an imperial measurement—check out [format-quantity](https://www.npmjs.com/package/format-quantity)._
14
+ >
15
+ > _For a more complete solution to parsing recipe ingredients, try [parse-ingredient](https://www.npmjs.com/package/parse-ingredient)._
14
16
 
15
- ## Installation
17
+ ## Usage
16
18
 
17
- ### npm
19
+ ### Installed
18
20
 
19
- ```shell
20
- # npm
21
- npm i numeric-quantity
21
+ ```js
22
+ import { numericQuantity } from 'numeric-quantity';
22
23
 
23
- # yarn
24
- yarn add numeric-quantity
24
+ console.log(numericQuantity('1 1/2')); // 1.5
25
+ console.log(numericQuantity('2 2/3')); // 2.667
25
26
  ```
26
27
 
27
- ### Browser
28
-
29
- In the browser, available as a global function `numericQuantity`.
28
+ ### CDN
30
29
 
31
30
  ```html
32
- <script src="https://unpkg.com/numeric-quantity"></script>
33
- <script>
34
- console.log(numericQuantity('10 1/2')); // 10.5
35
- </script>
36
- ```
31
+ <script type="module">
32
+ import { numericQuantity } from 'https://cdn.jsdelivr.net/npm/numeric-quantity/+esm';
37
33
 
38
- ## Usage
39
-
40
- ```js
41
- import numericQuantity from 'numeric-quantity';
42
-
43
- console.log(numericQuantity('1 1/2')); // 1.5
44
- console.log(numericQuantity('2 2/3')); // 2.666
34
+ console.log(numericQuantity('10½')); // 10.5
35
+ </script>
45
36
  ```
46
37
 
47
- The return value will be `NaN` if the provided string does not resemble a number.
38
+ ## Other exports
39
+
40
+ | Name | Type | Description |
41
+ | ------------------------------- | ---------- | ---------------------------------------------------------------------------------------------------- |
42
+ | `numericRegex` | `RegExp` | Regular expression matching a string that resembles a number (using Arabic numerals) in its entirety |
43
+ | `VulgarFraction` | `type` | Union type of all unicode vulgar fraction code points |
44
+ | `vulgarFractionsRegex` | `RegExp` | Regular expression matching the first unicode vulgar fraction code point |
45
+ | `vulgarFractionToAsciiMap` | `object` | Mapping of each vulgar fraction to its traditional ASCII representation (e.g., `'½'` to `'1/2'`) |
46
+ | `parseRomanNumerals` | `function` | Same function signature as `numericQuantity`, but only for Roman numerals (used internally) |
47
+ | `romanNumeralRegex` | `RegExp` | Regular expression matching valid Roman numeral sequences (uses modern, strict rules) |
48
+ | `romanNumeralUnicodeRegex` | `RegExp` | Regular expression matching any unicode Roman numeral code point |
49
+ | `romanNumeralUnicodeToAsciiMap` | `object` | Mapping of each Roman numeral to its traditional ASCII representation (e.g., `'Ⅻ'` to `'XII'`) |
50
+ | `romanNumeralValues` | `object` | Mapping of each valid Roman numeral sequence fragment to its numeric value |
51
+ | `RomanNumeralAscii` | `type` | Union type of allowable Roman numeral characters (uppercase only) |
52
+ | `RomanNumeralUnicode` | `type` | Union type of all Unicode Roman numeral characters (representing 1-12, 50, 100, 500, and 1000) |
53
+ | `RomanNumeral` | `type` | Union type of `RomanNumeralAscii` and `RomanNumeralUnicode` |
54
+
55
+ [badge-npm]: https://img.shields.io/npm/v/numeric-quantity.svg?cacheSeconds=3600&logo=npm
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+ if (process.env.NODE_ENV === 'production') {
3
+ module.exports = require('./numeric-quantity.cjs.production.js');
4
+ } else {
5
+ module.exports = require('./numeric-quantity.cjs.development.js');
6
+ }
@@ -0,0 +1,234 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ numericQuantity: () => numericQuantity,
24
+ numericRegex: () => numericRegex,
25
+ parseRomanNumerals: () => parseRomanNumerals,
26
+ romanNumeralRegex: () => romanNumeralRegex,
27
+ romanNumeralUnicodeRegex: () => romanNumeralUnicodeRegex,
28
+ romanNumeralUnicodeToAsciiMap: () => romanNumeralUnicodeToAsciiMap,
29
+ romanNumeralValues: () => romanNumeralValues,
30
+ vulgarFractionToAsciiMap: () => vulgarFractionToAsciiMap,
31
+ vulgarFractionsRegex: () => vulgarFractionsRegex
32
+ });
33
+ module.exports = __toCommonJS(src_exports);
34
+
35
+ // src/constants.ts
36
+ var vulgarFractionToAsciiMap = {
37
+ "\xBC": "1/4",
38
+ "\xBD": "1/2",
39
+ "\xBE": "3/4",
40
+ "\u2150": "1/7",
41
+ "\u2151": "1/9",
42
+ "\u2152": "1/10",
43
+ "\u2153": "1/3",
44
+ "\u2154": "2/3",
45
+ "\u2155": "1/5",
46
+ "\u2156": "2/5",
47
+ "\u2157": "3/5",
48
+ "\u2158": "4/5",
49
+ "\u2159": "1/6",
50
+ "\u215A": "5/6",
51
+ "\u215B": "1/8",
52
+ "\u215C": "3/8",
53
+ "\u215D": "5/8",
54
+ "\u215E": "7/8",
55
+ "\u215F": "1/"
56
+ };
57
+ var numericRegex = /^(?=-?\s*\.\d|-?\s*\d+)(-)?\s*((?:\d+[\d,_]*)*)(\.\d+|(\s+\d*\s*)?\s*\/\s*\d+)?(?:\s*[^\.\d\/].*)?/;
58
+ var vulgarFractionsRegex = new RegExp(
59
+ `(${Object.keys(vulgarFractionToAsciiMap).join("|")})`
60
+ );
61
+ var romanNumeralValues = {
62
+ MMM: 3e3,
63
+ MM: 2e3,
64
+ M: 1e3,
65
+ CM: 900,
66
+ DCCC: 800,
67
+ DCC: 700,
68
+ DC: 600,
69
+ D: 500,
70
+ CD: 400,
71
+ CCC: 300,
72
+ CC: 200,
73
+ C: 100,
74
+ XC: 90,
75
+ LXXX: 80,
76
+ LXX: 70,
77
+ LX: 60,
78
+ L: 50,
79
+ XL: 40,
80
+ XXX: 30,
81
+ XX: 20,
82
+ // Twelve is only here for tests; not used in practice
83
+ XII: 12,
84
+ // Eleven is only here for tests; not used in practice
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
96
+ };
97
+ var romanNumeralUnicodeToAsciiMap = {
98
+ // Roman Numeral One (U+2160)
99
+ "\u2160": "I",
100
+ // Roman Numeral Two (U+2161)
101
+ "\u2161": "II",
102
+ // Roman Numeral Three (U+2162)
103
+ "\u2162": "III",
104
+ // Roman Numeral Four (U+2163)
105
+ "\u2163": "IV",
106
+ // Roman Numeral Five (U+2164)
107
+ "\u2164": "V",
108
+ // Roman Numeral Six (U+2165)
109
+ "\u2165": "VI",
110
+ // Roman Numeral Seven (U+2166)
111
+ "\u2166": "VII",
112
+ // Roman Numeral Eight (U+2167)
113
+ "\u2167": "VIII",
114
+ // Roman Numeral Nine (U+2168)
115
+ "\u2168": "IX",
116
+ // Roman Numeral Ten (U+2169)
117
+ "\u2169": "X",
118
+ // Roman Numeral Eleven (U+216A)
119
+ "\u216A": "XI",
120
+ // Roman Numeral Twelve (U+216B)
121
+ "\u216B": "XII",
122
+ // Roman Numeral Fifty (U+216C)
123
+ "\u216C": "L",
124
+ // Roman Numeral One Hundred (U+216D)
125
+ "\u216D": "C",
126
+ // Roman Numeral Five Hundred (U+216E)
127
+ "\u216E": "D",
128
+ // Roman Numeral One Thousand (U+216F)
129
+ "\u216F": "M",
130
+ // Small Roman Numeral One (U+2170)
131
+ "\u2170": "I",
132
+ // Small Roman Numeral Two (U+2171)
133
+ "\u2171": "II",
134
+ // Small Roman Numeral Three (U+2172)
135
+ "\u2172": "III",
136
+ // Small Roman Numeral Four (U+2173)
137
+ "\u2173": "IV",
138
+ // Small Roman Numeral Five (U+2174)
139
+ "\u2174": "V",
140
+ // Small Roman Numeral Six (U+2175)
141
+ "\u2175": "VI",
142
+ // Small Roman Numeral Seven (U+2176)
143
+ "\u2176": "VII",
144
+ // Small Roman Numeral Eight (U+2177)
145
+ "\u2177": "VIII",
146
+ // Small Roman Numeral Nine (U+2178)
147
+ "\u2178": "IX",
148
+ // Small Roman Numeral Ten (U+2179)
149
+ "\u2179": "X",
150
+ // Small Roman Numeral Eleven (U+217A)
151
+ "\u217A": "XI",
152
+ // Small Roman Numeral Twelve (U+217B)
153
+ "\u217B": "XII",
154
+ // Small Roman Numeral Fifty (U+217C)
155
+ "\u217C": "L",
156
+ // Small Roman Numeral One Hundred (U+217D)
157
+ "\u217D": "C",
158
+ // Small Roman Numeral Five Hundred (U+217E)
159
+ "\u217E": "D",
160
+ // Small Roman Numeral One Thousand (U+217F)
161
+ "\u217F": "M"
162
+ };
163
+ var romanNumeralUnicodeRegex = new RegExp(
164
+ `(${Object.keys(romanNumeralUnicodeToAsciiMap).join("|")})`,
165
+ "gi"
166
+ );
167
+ var romanNumeralRegex = /^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;
168
+
169
+ // src/parseRomanNumerals.ts
170
+ var parseRomanNumerals = (romanNumerals) => {
171
+ const normalized = `${romanNumerals}`.replace(
172
+ romanNumeralUnicodeRegex,
173
+ (_m, rn) => romanNumeralUnicodeToAsciiMap[rn]
174
+ ).toUpperCase();
175
+ const regexResult = romanNumeralRegex.exec(normalized);
176
+ if (!regexResult) {
177
+ return NaN;
178
+ }
179
+ const [, thousands, hundreds, tens, ones] = regexResult;
180
+ return (romanNumeralValues[thousands] || 0) + (romanNumeralValues[hundreds] || 0) + (romanNumeralValues[tens] || 0) + (romanNumeralValues[ones] || 0);
181
+ };
182
+
183
+ // src/numericQuantity.ts
184
+ var spaceThenSlashRegex = /^\s*\//;
185
+ var numericQuantity = (quantity) => {
186
+ let finalResult = NaN;
187
+ const quantityAsString = `${quantity}`.replace(
188
+ vulgarFractionsRegex,
189
+ (_m, vf) => ` ${vulgarFractionToAsciiMap[vf]}`
190
+ ).replace("\u2044", "/").trim();
191
+ if (quantityAsString.length === 0) {
192
+ return NaN;
193
+ }
194
+ const regexResult = numericRegex.exec(quantityAsString);
195
+ if (!regexResult) {
196
+ return parseRomanNumerals(quantityAsString);
197
+ }
198
+ const [, dash, ng1temp, numberGroup2] = regexResult;
199
+ const numberGroup1 = ng1temp.replace(/[,_]/g, "");
200
+ if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith(".")) {
201
+ finalResult = 0;
202
+ } else {
203
+ finalResult = parseInt(numberGroup1);
204
+ }
205
+ if (!numberGroup2) {
206
+ return finalResult * (dash === "-" ? -1 : 1);
207
+ }
208
+ if (numberGroup2.startsWith(".")) {
209
+ const numerator = parseFloat(numberGroup2);
210
+ finalResult += Math.round(numerator * 1e3) / 1e3;
211
+ } else if (spaceThenSlashRegex.test(numberGroup2)) {
212
+ const numerator = parseInt(numberGroup1);
213
+ const denominator = parseInt(numberGroup2.replace("/", ""));
214
+ finalResult = Math.round(numerator * 1e3 / denominator) / 1e3;
215
+ } else {
216
+ const fractionArray = numberGroup2.split("/");
217
+ const [numerator, denominator] = fractionArray.map((v) => parseInt(v));
218
+ finalResult += Math.round(numerator * 1e3 / denominator) / 1e3;
219
+ }
220
+ return finalResult * (dash === "-" ? -1 : 1);
221
+ };
222
+ // Annotate the CommonJS export names for ESM import in node:
223
+ 0 && (module.exports = {
224
+ numericQuantity,
225
+ numericRegex,
226
+ parseRomanNumerals,
227
+ romanNumeralRegex,
228
+ romanNumeralUnicodeRegex,
229
+ romanNumeralUnicodeToAsciiMap,
230
+ romanNumeralValues,
231
+ vulgarFractionToAsciiMap,
232
+ vulgarFractionsRegex
233
+ });
234
+ //# sourceMappingURL=numeric-quantity.cjs.development.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts","../../src/constants.ts","../../src/parseRomanNumerals.ts","../../src/numericQuantity.ts"],"sourcesContent":["export * from './constants';\nexport * from './numericQuantity';\nexport * from './parseRomanNumerals';\nexport * from './types';\n","import type {\n RomanNumeralAscii,\n RomanNumeralUnicode,\n VulgarFraction,\n} from './types';\n\n// #region Arabic numerals\n/**\n * Map of Unicode fraction code points to their ASCII equivalents\n */\nexport const vulgarFractionToAsciiMap: Record<VulgarFraction, string> = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n '⅟': '1/',\n};\n\n/**\n * Captures the individual elements of a numeric string.\n *\n * Capture groups:\n *\n * +=====+====================+========================+\n * | # | Description | Example |\n * +=====+====================+========================+\n * | 0 | entire string | \"2 1/3\" from \"2 1/3\" |\n * +-----+--------------------+------------------------+\n * | 1 | \"negative\" dash | \"-\" from \"-2 1/3\" |\n * +-----+--------------------+------------------------+\n * | 2 | the whole number | \"2\" from \"2 1/3\" |\n * | | - OR - | |\n * | | the numerator | \"1\" from \"1/3\" |\n * | + + |\n * | (This may include comma/underscore separators) |\n * +-----+--------------------+------------------------+\n * | 3 | entire fraction | \"1/3\" from \"2 1/3\" |\n * | | - OR - | |\n * | | decimal portion | \".33\" from \"2.33\" |\n * | | - OR - | |\n * | | denominator | \"/3\" from \"1/3\" |\n * +=====+====================+========================+\n *\n * @example\n * numericRegex.exec(\"1\") // [ \"1\", \"1\", null, null ]\n * numericRegex.exec(\"1.23\") // [ \"1.23\", \"1\", \".23\", null ]\n * numericRegex.exec(\"1 2/3\") // [ \"1 2/3\", \"1\", \" 2/3\", \" 2\" ]\n * numericRegex.exec(\"2/3\") // [ \"2/3\", \"2\", \"/3\", null ]\n * numericRegex.exec(\"2 / 3\") // [ \"2 / 3\", \"2\", \"/ 3\", null ]\n */\nexport const numericRegex =\n /^(?=-?\\s*\\.\\d|-?\\s*\\d+)(-)?\\s*((?:\\d+[\\d,_]*)*)(\\.\\d+|(\\s+\\d*\\s*)?\\s*\\/\\s*\\d+)?(?:\\s*[^\\.\\d\\/].*)?/;\n\n/**\n * Captures any Unicode vulgar fractions\n */\nexport const vulgarFractionsRegex = new RegExp(\n `(${Object.keys(vulgarFractionToAsciiMap).join('|')})`\n);\n// #endregion\n\n// #region Roman numerals\ntype RomanNumeralSequenceFragment =\n | `${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}`;\n\nexport const romanNumeralValues = {\n MMM: 3000,\n MM: 2000,\n M: 1000,\n CM: 900,\n DCCC: 800,\n DCC: 700,\n DC: 600,\n D: 500,\n CD: 400,\n CCC: 300,\n CC: 200,\n C: 100,\n XC: 90,\n LXXX: 80,\n LXX: 70,\n LX: 60,\n L: 50,\n XL: 40,\n XXX: 30,\n XX: 20,\n // Twelve is only here for tests; not used in practice\n XII: 12,\n // Eleven is only here for tests; not used in practice\n XI: 11,\n X: 10,\n IX: 9,\n VIII: 8,\n VII: 7,\n VI: 6,\n V: 5,\n IV: 4,\n III: 3,\n II: 2,\n I: 1,\n} satisfies { [k in RomanNumeralSequenceFragment]?: number };\n\n/**\n * Map of Unicode Roman numeral code points to their ASCII equivalents\n */\nexport const romanNumeralUnicodeToAsciiMap: Record<\n RomanNumeralUnicode,\n string\n> = {\n // Roman Numeral One (U+2160)\n Ⅰ: 'I',\n // Roman Numeral Two (U+2161)\n Ⅱ: 'II',\n // Roman Numeral Three (U+2162)\n Ⅲ: 'III',\n // Roman Numeral Four (U+2163)\n Ⅳ: 'IV',\n // Roman Numeral Five (U+2164)\n Ⅴ: 'V',\n // Roman Numeral Six (U+2165)\n Ⅵ: 'VI',\n // Roman Numeral Seven (U+2166)\n Ⅶ: 'VII',\n // Roman Numeral Eight (U+2167)\n Ⅷ: 'VIII',\n // Roman Numeral Nine (U+2168)\n Ⅸ: 'IX',\n // Roman Numeral Ten (U+2169)\n Ⅹ: 'X',\n // Roman Numeral Eleven (U+216A)\n Ⅺ: 'XI',\n // Roman Numeral Twelve (U+216B)\n Ⅻ: 'XII',\n // Roman Numeral Fifty (U+216C)\n Ⅼ: 'L',\n // Roman Numeral One Hundred (U+216D)\n Ⅽ: 'C',\n // Roman Numeral Five Hundred (U+216E)\n Ⅾ: 'D',\n // Roman Numeral One Thousand (U+216F)\n Ⅿ: 'M',\n // Small Roman Numeral One (U+2170)\n ⅰ: 'I',\n // Small Roman Numeral Two (U+2171)\n ⅱ: 'II',\n // Small Roman Numeral Three (U+2172)\n ⅲ: 'III',\n // Small Roman Numeral Four (U+2173)\n ⅳ: 'IV',\n // Small Roman Numeral Five (U+2174)\n ⅴ: 'V',\n // Small Roman Numeral Six (U+2175)\n ⅵ: 'VI',\n // Small Roman Numeral Seven (U+2176)\n ⅶ: 'VII',\n // Small Roman Numeral Eight (U+2177)\n ⅷ: 'VIII',\n // Small Roman Numeral Nine (U+2178)\n ⅸ: 'IX',\n // Small Roman Numeral Ten (U+2179)\n ⅹ: 'X',\n // Small Roman Numeral Eleven (U+217A)\n ⅺ: 'XI',\n // Small Roman Numeral Twelve (U+217B)\n ⅻ: 'XII',\n // Small Roman Numeral Fifty (U+217C)\n ⅼ: 'L',\n // Small Roman Numeral One Hundred (U+217D)\n ⅽ: 'C',\n // Small Roman Numeral Five Hundred (U+217E)\n ⅾ: 'D',\n // Small Roman Numeral One Thousand (U+217F)\n ⅿ: 'M',\n};\n\n/**\n * Captures all Unicode Roman numeral code points\n */\nexport const romanNumeralUnicodeRegex = new RegExp(\n `(${Object.keys(romanNumeralUnicodeToAsciiMap).join('|')})`,\n 'gi'\n);\n\n/**\n * Captures a valid Roman numeral sequence\n *\n * Capture groups:\n *\n * +=====+=================+==========================+\n * | # | Description | Example |\n * +=====+=================+==========================+\n * | 0 | Entire string | \"MCCXIV\" from \"MCCXIV\" |\n * +-----+-----------------+--------------------------+\n * | 1 | Thousands | \"M\" from \"MCCXIV\" |\n * +-----+-----------------+--------------------------+\n * | 2 | Hundreds | \"CC\" from \"MCCXIV\" |\n * +-----+-----------------+--------------------------+\n * | 3 | Tens | \"X\" from \"MCCXIV\" |\n * +-----+-----------------+--------------------------+\n * | 4 | Ones | \"IV\" from \"MCCXIV\" |\n * +=====+=================+==========================+\n *\n * @example\n * romanNumeralRegex.exec(\"M\") // [ \"M\", \"M\", \"\", \"\", \"\" ]\n * romanNumeralRegex.exec(\"XII\") // [ \"XII\", \"\", \"\", \"X\", \"II\" ]\n * romanNumeralRegex.exec(\"MCCXIV\") // [ \"MCCXIV\", \"M\", \"CC\", \"X\", \"IV\" ]\n */\nexport const romanNumeralRegex =\n /^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;\n// #endregion\n","import {\n romanNumeralUnicodeRegex,\n romanNumeralUnicodeToAsciiMap,\n romanNumeralRegex,\n romanNumeralValues,\n} from './constants';\n\n// Just a shorthand type alias\ntype RNV = keyof typeof romanNumeralValues;\n\nexport const parseRomanNumerals = (romanNumerals: string) => {\n const normalized = `${romanNumerals}`\n .replace(\n romanNumeralUnicodeRegex,\n (_m, rn: keyof typeof romanNumeralUnicodeToAsciiMap) =>\n romanNumeralUnicodeToAsciiMap[rn]\n )\n .toUpperCase();\n\n const regexResult = romanNumeralRegex.exec(normalized);\n\n if (!regexResult) {\n return NaN;\n }\n\n const [, thousands, hundreds, tens, ones] = regexResult;\n\n return (\n (romanNumeralValues[thousands as RNV] || 0) +\n (romanNumeralValues[hundreds as RNV] || 0) +\n (romanNumeralValues[tens as RNV] || 0) +\n (romanNumeralValues[ones as RNV] || 0)\n );\n};\n","import {\n numericRegex,\n vulgarFractionToAsciiMap,\n vulgarFractionsRegex,\n} from './constants';\nimport { parseRomanNumerals } from './parseRomanNumerals';\n\nconst spaceThenSlashRegex = /^\\s*\\//;\n\n/**\n * Converts a string to a number, like an enhanced version of `parseFloat`.\n *\n * The string can include mixed numbers, vulgar fractions, or Roman numerals.\n */\nexport const numericQuantity = (quantity: string) => {\n let finalResult = NaN;\n\n // Coerce to string in case qty is a number\n const quantityAsString = `${quantity}`\n // Convert vulgar fractions to ASCII, with a leading space\n // to keep the whole number and the fraction separate\n .replace(\n vulgarFractionsRegex,\n (_m, vf: keyof typeof vulgarFractionToAsciiMap) =>\n ` ${vulgarFractionToAsciiMap[vf]}`\n )\n // Convert fraction slash to standard slash\n .replace('⁄', '/')\n .trim();\n\n // Bail out if the string was only white space\n if (quantityAsString.length === 0) {\n return NaN;\n }\n\n const regexResult = numericRegex.exec(quantityAsString);\n\n // If the Arabic numeral regex fails, try Roman numerals\n if (!regexResult) {\n return parseRomanNumerals(quantityAsString);\n }\n\n const [, dash, ng1temp, numberGroup2] = regexResult;\n const numberGroup1 = ng1temp.replace(/[,_]/g, '');\n\n // Numerify capture group 1\n if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith('.')) {\n finalResult = 0;\n } else {\n finalResult = parseInt(numberGroup1);\n }\n\n // If capture group 2 is null, then we're dealing with an integer\n // and there is nothing left to process\n if (!numberGroup2) {\n return finalResult * (dash === '-' ? -1 : 1);\n }\n\n if (numberGroup2.startsWith('.')) {\n // If first char is \".\" it's a decimal so just trim to 3 decimal places\n const numerator = parseFloat(numberGroup2);\n finalResult += Math.round(numerator * 1000) / 1000;\n } else if (spaceThenSlashRegex.test(numberGroup2)) {\n // If the first non-space char is \"/\" it's a pure fraction (e.g. \"1/2\")\n const numerator = parseInt(numberGroup1);\n const denominator = parseInt(numberGroup2.replace('/', ''));\n finalResult = Math.round((numerator * 1000) / denominator) / 1000;\n } else {\n // Otherwise it's a mixed fraction (e.g. \"1 2/3\")\n const fractionArray = numberGroup2.split('/');\n const [numerator, denominator] = fractionArray.map(v => parseInt(v));\n finalResult += Math.round((numerator * 1000) / denominator) / 1000;\n }\n\n return finalResult * (dash === '-' ? -1 : 1);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACUO,IAAM,2BAA2D;AAAA,EACtE,QAAK;AAAA,EACL,QAAK;AAAA,EACL,QAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AACP;AAkCO,IAAM,eACX;AAKK,IAAM,uBAAuB,IAAI;AAAA,EACtC,IAAI,OAAO,KAAK,wBAAwB,EAAE,KAAK,GAAG;AACpD;AAUO,IAAM,qBAAqB;AAAA,EAChC,KAAK;AAAA,EACL,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,KAAK;AAAA,EACL,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,IAAI;AAAA,EACJ,KAAK;AAAA,EACL,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,KAAK;AAAA,EACL,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,IAAI;AAAA,EACJ,KAAK;AAAA,EACL,IAAI;AAAA;AAAA,EAEJ,KAAK;AAAA;AAAA,EAEL,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,KAAK;AAAA,EACL,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,IAAI;AAAA,EACJ,KAAK;AA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BAAe,KAAK,MAAO,YAAY,MAAQ,WAAW,IAAI;AAAA,EAChE;AAEA,SAAO,eAAe,SAAS,MAAM,KAAK;AAC5C;","names":[]}
@@ -0,0 +1,2 @@
1
+ "use strict";var p=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var A=(r,e)=>{for(var a in e)p(r,a,{get:e[a],enumerable:!0})},$=(r,e,a,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of C(e))!M.call(r,o)&&o!==a&&p(r,o,{get:()=>e[o],enumerable:!(s=f(e,o))||s.enumerable});return r};var y=r=>$(p({},"__esModule",{value:!0}),r);var F={};A(F,{numericQuantity:()=>D,numericRegex:()=>N,parseRomanNumerals:()=>x,romanNumeralRegex:()=>X,romanNumeralUnicodeRegex:()=>g,romanNumeralUnicodeToAsciiMap:()=>I,romanNumeralValues:()=>i,vulgarFractionToAsciiMap:()=>u,vulgarFractionsRegex:()=>R});module.exports=y(F);var u={"\xBC":"1/4","\xBD":"1/2","\xBE":"3/4","\u2150":"1/7","\u2151":"1/9","\u2152":"1/10","\u2153":"1/3","\u2154":"2/3","\u2155":"1/5","\u2156":"2/5","\u2157":"3/5","\u2158":"4/5","\u2159":"1/6","\u215A":"5/6","\u215B":"1/8","\u215C":"3/8","\u215D":"5/8","\u215E":"7/8","\u215F":"1/"},N=/^(?=-?\s*\.\d|-?\s*\d+)(-)?\s*((?:\d+[\d,_]*)*)(\.\d+|(\s+\d*\s*)?\s*\/\s*\d+)?(?:\s*[^\.\d\/].*)?/,R=new RegExp(`(${Object.keys(u).join("|")})`),i={MMM:3e3,MM:2e3,M:1e3,CM:900,DCCC:800,DCC:700,DC:600,D:500,CD:400,CCC:300,CC:200,C:100,XC:90,LXXX:80,LXX:70,LX:60,L:50,XL:40,XXX:30,XX:20,XII:12,XI:11,X:10,IX:9,VIII:8,VII:7,VI:6,V:5,IV:4,III:3,II:2,I:1},I={"\u2160":"I","\u2161":"II","\u2162":"III","\u2163":"IV","\u2164":"V","\u2165":"VI","\u2166":"VII","\u2167":"VIII","\u2168":"IX","\u2169":"X","\u216A":"XI","\u216B":"XII","\u216C":"L","\u216D":"C","\u216E":"D","\u216F":"M","\u2170":"I","\u2171":"II","\u2172":"III","\u2173":"IV","\u2174":"V","\u2175":"VI","\u2176":"VII","\u2177":"VIII","\u2178":"IX","\u2179":"X","\u217A":"XI","\u217B":"XII","\u217C":"L","\u217D":"C","\u217E":"D","\u217F":"M"},g=new RegExp(`(${Object.keys(I).join("|")})`,"gi"),X=/^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;var x=r=>{let e=`${r}`.replace(g,(c,t)=>I[t]).toUpperCase(),a=X.exec(e);if(!a)return NaN;let[,s,o,l,n]=a;return(i[s]||0)+(i[o]||0)+(i[l]||0)+(i[n]||0)};var h=/^\s*\//,D=r=>{let e=NaN,a=`${r}`.replace(R,(t,m)=>` ${u[m]}`).replace("\u2044","/").trim();if(a.length===0)return NaN;let s=N.exec(a);if(!s)return x(a);let[,o,l,n]=s,c=l.replace(/[,_]/g,"");if(!c&&n&&n.startsWith(".")?e=0:e=parseInt(c),!n)return e*(o==="-"?-1:1);if(n.startsWith(".")){let t=parseFloat(n);e+=Math.round(t*1e3)/1e3}else if(h.test(n)){let t=parseInt(c),m=parseInt(n.replace("/",""));e=Math.round(t*1e3/m)/1e3}else{let t=n.split("/"),[m,V]=t.map(d=>parseInt(d));e+=Math.round(m*1e3/V)/1e3}return e*(o==="-"?-1:1)};0&&(module.exports={numericQuantity,numericRegex,parseRomanNumerals,romanNumeralRegex,romanNumeralUnicodeRegex,romanNumeralUnicodeToAsciiMap,romanNumeralValues,vulgarFractionToAsciiMap,vulgarFractionsRegex});
2
+ //# sourceMappingURL=numeric-quantity.cjs.production.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts","../../src/constants.ts","../../src/parseRomanNumerals.ts","../../src/numericQuantity.ts"],"sourcesContent":["export * from './constants';\nexport * from './numericQuantity';\nexport * from './parseRomanNumerals';\nexport * from './types';\n","import type {\n RomanNumeralAscii,\n RomanNumeralUnicode,\n VulgarFraction,\n} from './types';\n\n// #region Arabic numerals\n/**\n * Map of Unicode fraction code points to their ASCII equivalents\n */\nexport const vulgarFractionToAsciiMap: Record<VulgarFraction, string> = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n '⅟': '1/',\n};\n\n/**\n * Captures the individual elements of a numeric string.\n *\n * Capture groups:\n *\n * +=====+====================+========================+\n * | # | Description | Example |\n * +=====+====================+========================+\n * | 0 | entire string | \"2 1/3\" from \"2 1/3\" |\n * +-----+--------------------+------------------------+\n * | 1 | \"negative\" dash | \"-\" from \"-2 1/3\" |\n * +-----+--------------------+------------------------+\n * | 2 | the whole number | \"2\" from \"2 1/3\" |\n * | | - OR - | |\n * | | the numerator | \"1\" from \"1/3\" |\n * | + + |\n * | (This may include comma/underscore separators) |\n * +-----+--------------------+------------------------+\n * | 3 | entire fraction | \"1/3\" from \"2 1/3\" |\n * | | - OR - | |\n * | | decimal portion | \".33\" from \"2.33\" |\n * | | - OR - | |\n * | | denominator | \"/3\" from \"1/3\" |\n * +=====+====================+========================+\n *\n * @example\n * numericRegex.exec(\"1\") // [ \"1\", \"1\", null, null ]\n * numericRegex.exec(\"1.23\") // [ \"1.23\", \"1\", \".23\", null ]\n * numericRegex.exec(\"1 2/3\") // [ \"1 2/3\", \"1\", \" 2/3\", \" 2\" ]\n * numericRegex.exec(\"2/3\") // [ \"2/3\", \"2\", \"/3\", null ]\n * numericRegex.exec(\"2 / 3\") // [ \"2 / 3\", \"2\", \"/ 3\", null ]\n */\nexport const numericRegex =\n /^(?=-?\\s*\\.\\d|-?\\s*\\d+)(-)?\\s*((?:\\d+[\\d,_]*)*)(\\.\\d+|(\\s+\\d*\\s*)?\\s*\\/\\s*\\d+)?(?:\\s*[^\\.\\d\\/].*)?/;\n\n/**\n * Captures any Unicode vulgar fractions\n */\nexport const vulgarFractionsRegex = new RegExp(\n `(${Object.keys(vulgarFractionToAsciiMap).join('|')})`\n);\n// #endregion\n\n// #region Roman numerals\ntype RomanNumeralSequenceFragment =\n | `${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}`;\n\nexport const romanNumeralValues = {\n MMM: 3000,\n MM: 2000,\n M: 1000,\n CM: 900,\n DCCC: 800,\n DCC: 700,\n DC: 600,\n D: 500,\n CD: 400,\n CCC: 300,\n CC: 200,\n C: 100,\n XC: 90,\n LXXX: 80,\n LXX: 70,\n LX: 60,\n L: 50,\n XL: 40,\n XXX: 30,\n XX: 20,\n // Twelve is only here for tests; not used in practice\n XII: 12,\n // Eleven is only here for tests; not used in practice\n XI: 11,\n X: 10,\n IX: 9,\n VIII: 8,\n VII: 7,\n VI: 6,\n V: 5,\n IV: 4,\n III: 3,\n II: 2,\n I: 1,\n} satisfies { [k in RomanNumeralSequenceFragment]?: number };\n\n/**\n * Map of Unicode Roman numeral code points to their ASCII equivalents\n */\nexport const romanNumeralUnicodeToAsciiMap: Record<\n RomanNumeralUnicode,\n string\n> = {\n // Roman Numeral One (U+2160)\n Ⅰ: 'I',\n // Roman Numeral Two (U+2161)\n Ⅱ: 'II',\n // Roman Numeral Three (U+2162)\n Ⅲ: 'III',\n // Roman Numeral Four (U+2163)\n Ⅳ: 'IV',\n // Roman Numeral Five (U+2164)\n Ⅴ: 'V',\n // Roman Numeral Six (U+2165)\n Ⅵ: 'VI',\n // Roman Numeral Seven (U+2166)\n Ⅶ: 'VII',\n // Roman Numeral Eight (U+2167)\n Ⅷ: 'VIII',\n // Roman Numeral Nine (U+2168)\n Ⅸ: 'IX',\n // Roman Numeral Ten (U+2169)\n Ⅹ: 'X',\n // Roman Numeral Eleven (U+216A)\n Ⅺ: 'XI',\n // Roman Numeral Twelve (U+216B)\n Ⅻ: 'XII',\n // Roman Numeral Fifty (U+216C)\n Ⅼ: 'L',\n // Roman Numeral One Hundred (U+216D)\n Ⅽ: 'C',\n // Roman Numeral Five Hundred (U+216E)\n Ⅾ: 'D',\n // Roman Numeral One Thousand (U+216F)\n Ⅿ: 'M',\n // Small Roman Numeral One (U+2170)\n ⅰ: 'I',\n // Small Roman Numeral Two (U+2171)\n ⅱ: 'II',\n // Small Roman Numeral Three (U+2172)\n ⅲ: 'III',\n // Small Roman Numeral Four (U+2173)\n ⅳ: 'IV',\n // Small Roman Numeral Five (U+2174)\n ⅴ: 'V',\n // Small Roman Numeral Six (U+2175)\n ⅵ: 'VI',\n // Small Roman Numeral Seven (U+2176)\n ⅶ: 'VII',\n // Small Roman Numeral Eight (U+2177)\n ⅷ: 'VIII',\n // Small Roman Numeral Nine (U+2178)\n ⅸ: 'IX',\n // Small Roman Numeral Ten (U+2179)\n ⅹ: 'X',\n // Small Roman Numeral Eleven (U+217A)\n ⅺ: 'XI',\n // Small Roman Numeral Twelve (U+217B)\n ⅻ: 'XII',\n // Small Roman Numeral Fifty (U+217C)\n ⅼ: 'L',\n // Small Roman Numeral One Hundred (U+217D)\n ⅽ: 'C',\n // Small Roman Numeral Five Hundred (U+217E)\n ⅾ: 'D',\n // Small Roman Numeral One Thousand (U+217F)\n ⅿ: 'M',\n};\n\n/**\n * Captures all Unicode Roman numeral code points\n */\nexport const romanNumeralUnicodeRegex = new RegExp(\n `(${Object.keys(romanNumeralUnicodeToAsciiMap).join('|')})`,\n 'gi'\n);\n\n/**\n * Captures a valid Roman numeral sequence\n *\n * Capture groups:\n *\n * +=====+=================+==========================+\n * | # | Description | Example |\n * +=====+=================+==========================+\n * | 0 | Entire string | \"MCCXIV\" from \"MCCXIV\" |\n * +-----+-----------------+--------------------------+\n * | 1 | Thousands | \"M\" from \"MCCXIV\" |\n * +-----+-----------------+--------------------------+\n * | 2 | Hundreds | \"CC\" from \"MCCXIV\" |\n * +-----+-----------------+--------------------------+\n * | 3 | Tens | \"X\" from \"MCCXIV\" |\n * +-----+-----------------+--------------------------+\n * | 4 | Ones | \"IV\" from \"MCCXIV\" |\n * +=====+=================+==========================+\n *\n * @example\n * romanNumeralRegex.exec(\"M\") // [ \"M\", \"M\", \"\", \"\", \"\" ]\n * romanNumeralRegex.exec(\"XII\") // [ \"XII\", \"\", \"\", \"X\", \"II\" ]\n * romanNumeralRegex.exec(\"MCCXIV\") // [ \"MCCXIV\", \"M\", \"CC\", \"X\", \"IV\" ]\n */\nexport const romanNumeralRegex =\n /^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;\n// #endregion\n","import {\n romanNumeralUnicodeRegex,\n romanNumeralUnicodeToAsciiMap,\n romanNumeralRegex,\n romanNumeralValues,\n} from './constants';\n\n// Just a shorthand type alias\ntype RNV = keyof typeof romanNumeralValues;\n\nexport const parseRomanNumerals = (romanNumerals: string) => {\n const normalized = `${romanNumerals}`\n .replace(\n romanNumeralUnicodeRegex,\n (_m, rn: keyof typeof romanNumeralUnicodeToAsciiMap) =>\n romanNumeralUnicodeToAsciiMap[rn]\n )\n .toUpperCase();\n\n const regexResult = romanNumeralRegex.exec(normalized);\n\n if (!regexResult) {\n return NaN;\n }\n\n const [, thousands, hundreds, tens, ones] = regexResult;\n\n return (\n (romanNumeralValues[thousands as RNV] || 0) +\n (romanNumeralValues[hundreds as RNV] || 0) +\n (romanNumeralValues[tens as RNV] || 0) +\n (romanNumeralValues[ones as RNV] || 0)\n );\n};\n","import {\n numericRegex,\n vulgarFractionToAsciiMap,\n vulgarFractionsRegex,\n} from './constants';\nimport { parseRomanNumerals } from './parseRomanNumerals';\n\nconst spaceThenSlashRegex = /^\\s*\\//;\n\n/**\n * Converts a string to a number, like an enhanced version of `parseFloat`.\n *\n * The string can include mixed numbers, vulgar fractions, or Roman numerals.\n */\nexport const numericQuantity = (quantity: string) => {\n let finalResult = NaN;\n\n // Coerce to string in case qty is a number\n const quantityAsString = `${quantity}`\n // Convert vulgar fractions to ASCII, with a leading space\n // to keep the whole number and the fraction separate\n .replace(\n vulgarFractionsRegex,\n (_m, vf: keyof typeof vulgarFractionToAsciiMap) =>\n ` ${vulgarFractionToAsciiMap[vf]}`\n )\n // Convert fraction slash to standard slash\n .replace('⁄', '/')\n .trim();\n\n // Bail out if the string was only white space\n if (quantityAsString.length === 0) {\n return NaN;\n }\n\n const regexResult = numericRegex.exec(quantityAsString);\n\n // If the Arabic numeral regex fails, try Roman numerals\n if (!regexResult) {\n return parseRomanNumerals(quantityAsString);\n }\n\n const [, dash, ng1temp, numberGroup2] = regexResult;\n const numberGroup1 = ng1temp.replace(/[,_]/g, '');\n\n // Numerify capture group 1\n if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith('.')) {\n finalResult = 0;\n } else {\n finalResult = parseInt(numberGroup1);\n }\n\n // If capture group 2 is null, then we're dealing with an integer\n // and there is nothing left to process\n if (!numberGroup2) {\n return finalResult * (dash === '-' ? -1 : 1);\n }\n\n if (numberGroup2.startsWith('.')) {\n // If first char is \".\" it's a decimal so just trim to 3 decimal places\n const numerator = parseFloat(numberGroup2);\n finalResult += Math.round(numerator * 1000) / 1000;\n } else if (spaceThenSlashRegex.test(numberGroup2)) {\n // If the first non-space char is \"/\" it's a pure fraction (e.g. \"1/2\")\n const numerator = parseInt(numberGroup1);\n const denominator = parseInt(numberGroup2.replace('/', ''));\n finalResult = Math.round((numerator * 1000) / denominator) / 1000;\n } else {\n // Otherwise it's a mixed fraction (e.g. \"1 2/3\")\n const fractionArray = numberGroup2.split('/');\n const [numerator, denominator] = fractionArray.map(v => parseInt(v));\n finalResult += Math.round((numerator * 1000) / denominator) / 1000;\n }\n\n return finalResult * (dash === '-' ? -1 : 1);\n};\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,EAAA,iBAAAC,EAAA,uBAAAC,EAAA,sBAAAC,EAAA,6BAAAC,EAAA,kCAAAC,EAAA,uBAAAC,EAAA,6BAAAC,EAAA,yBAAAC,IAAA,eAAAC,EAAAX,GCUO,IAAMY,EAA2D,CACtE,OAAK,MACL,OAAK,MACL,OAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,OACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,IACP,EAkCaC,EACX,qGAKWC,EAAuB,IAAI,OACtC,IAAI,OAAO,KAAKF,CAAwB,EAAE,KAAK,GAAG,IACpD,EAUaG,EAAqB,CAChC,IAAK,IACL,GAAI,IACJ,EAAG,IACH,GAAI,IACJ,KAAM,IACN,IAAK,IACL,GAAI,IACJ,EAAG,IACH,GAAI,IACJ,IAAK,IACL,GAAI,IACJ,EAAG,IACH,GAAI,GACJ,KAAM,GACN,IAAK,GACL,GAAI,GACJ,EAAG,GACH,GAAI,GACJ,IAAK,GACL,GAAI,GAEJ,IAAK,GAEL,GAAI,GACJ,EAAG,GACH,GAAI,EACJ,KAAM,EACN,IAAK,EACL,GAAI,EACJ,EAAG,EACH,GAAI,EACJ,IAAK,EACL,GAAI,EACJ,EAAG,CACL,EAKaC,EAGT,CAEF,SAAG,IAEH,SAAG,KAEH,SAAG,MAEH,SAAG,KAEH,SAAG,IAEH,SAAG,KAEH,SAAG,MAEH,SAAG,OAEH,SAAG,KAEH,SAAG,IAEH,SAAG,KAEH,SAAG,MAEH,SAAG,IAEH,SAAG,IAEH,S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AK,GAG5C,GAAIE,EAAa,WAAW,GAAG,EAAG,CAEhC,IAAME,EAAY,WAAWF,CAAY,EACzCX,GAAe,KAAK,MAAMa,EAAY,GAAI,EAAI,YACrChB,EAAoB,KAAKc,CAAY,EAAG,CAEjD,IAAME,EAAY,SAASD,CAAY,EACjCE,EAAc,SAASH,EAAa,QAAQ,IAAK,EAAE,CAAC,EAC1DX,EAAc,KAAK,MAAOa,EAAY,IAAQC,CAAW,EAAI,QACxD,CAEL,IAAMC,EAAgBJ,EAAa,MAAM,GAAG,EACtC,CAACE,EAAWC,CAAW,EAAIC,EAAc,IAAIC,GAAK,SAASA,CAAC,CAAC,EACnEhB,GAAe,KAAK,MAAOa,EAAY,IAAQC,CAAW,EAAI,IAGhE,OAAOd,GAAeS,IAAS,IAAM,GAAK,EAC5C","names":["src_exports","__export","numericQuantity","numericRegex","parseRomanNumerals","romanNumeralRegex","romanNumeralUnicodeRegex","romanNumeralUnicodeToAsciiMap","romanNumeralValues","vulgarFractionToAsciiMap","vulgarFractionsRegex","__toCommonJS","vulgarFractionToAsciiMap","numericRegex","vulgarFractionsRegex","romanNumeralValues","romanNumeralUnicodeToAsciiMap","romanNumeralUnicodeRegex","romanNumeralRegex","parseRomanNumerals","romanNumerals","normalized","romanNumeralUnicodeRegex","_m","rn","romanNumeralUnicodeToAsciiMap","regexResult","romanNumeralRegex","thousands","hundreds","tens","ones","romanNumeralValues","spaceThenSlashRegex","numericQuantity","quantity","finalResult","quantityAsString","vulgarFractionsRegex","_m","vf","vulgarFractionToAsciiMap","regexResult","numericRegex","parseRomanNumerals","dash","ng1temp","numberGroup2","numberGroup1","numerator","denominator","fractionArray","v"]}
@@ -0,0 +1,124 @@
1
+ type VulgarFraction = '¼' | '½' | '¾' | '⅐' | '⅑' | '⅒' | '⅓' | '⅔' | '⅕' | '⅖' | '⅗' | '⅘' | '⅙' | '⅚' | '⅛' | '⅜' | '⅝' | '⅞' | '⅟';
2
+ type RomanNumeralAscii = 'I' | 'V' | 'X' | 'L' | 'C' | 'D' | 'M';
3
+ type RomanNumeralUnicode = 'Ⅰ' | 'Ⅱ' | 'Ⅲ' | 'Ⅳ' | 'Ⅴ' | 'Ⅵ' | 'Ⅶ' | 'Ⅷ' | 'Ⅸ' | 'Ⅹ' | 'Ⅺ' | 'Ⅻ' | 'Ⅼ' | 'Ⅽ' | 'Ⅾ' | 'Ⅿ' | 'ⅰ' | 'ⅱ' | 'ⅲ' | 'ⅳ' | 'ⅴ' | 'ⅵ' | 'ⅶ' | 'ⅷ' | 'ⅸ' | 'ⅹ' | 'ⅺ' | 'ⅻ' | 'ⅼ' | 'ⅽ' | 'ⅾ' | 'ⅿ';
4
+ type RomanNumeral = RomanNumeralAscii | RomanNumeralUnicode;
5
+
6
+ /**
7
+ * Map of Unicode fraction code points to their ASCII equivalents
8
+ */
9
+ declare const vulgarFractionToAsciiMap: Record<VulgarFraction, string>;
10
+ /**
11
+ * Captures the individual elements of a numeric string.
12
+ *
13
+ * Capture groups:
14
+ *
15
+ * +=====+====================+========================+
16
+ * | # | Description | Example |
17
+ * +=====+====================+========================+
18
+ * | 0 | entire string | "2 1/3" from "2 1/3" |
19
+ * +-----+--------------------+------------------------+
20
+ * | 1 | "negative" dash | "-" from "-2 1/3" |
21
+ * +-----+--------------------+------------------------+
22
+ * | 2 | the whole number | "2" from "2 1/3" |
23
+ * | | - OR - | |
24
+ * | | the numerator | "1" from "1/3" |
25
+ * | + + |
26
+ * | (This may include comma/underscore separators) |
27
+ * +-----+--------------------+------------------------+
28
+ * | 3 | entire fraction | "1/3" from "2 1/3" |
29
+ * | | - OR - | |
30
+ * | | decimal portion | ".33" from "2.33" |
31
+ * | | - OR - | |
32
+ * | | denominator | "/3" from "1/3" |
33
+ * +=====+====================+========================+
34
+ *
35
+ * @example
36
+ * numericRegex.exec("1") // [ "1", "1", null, null ]
37
+ * numericRegex.exec("1.23") // [ "1.23", "1", ".23", null ]
38
+ * numericRegex.exec("1 2/3") // [ "1 2/3", "1", " 2/3", " 2" ]
39
+ * numericRegex.exec("2/3") // [ "2/3", "2", "/3", null ]
40
+ * numericRegex.exec("2 / 3") // [ "2 / 3", "2", "/ 3", null ]
41
+ */
42
+ declare const numericRegex: RegExp;
43
+ /**
44
+ * Captures any Unicode vulgar fractions
45
+ */
46
+ declare const vulgarFractionsRegex: RegExp;
47
+ declare const romanNumeralValues: {
48
+ MMM: number;
49
+ MM: number;
50
+ M: number;
51
+ CM: number;
52
+ DCCC: number;
53
+ DCC: number;
54
+ DC: number;
55
+ D: number;
56
+ CD: number;
57
+ CCC: number;
58
+ CC: number;
59
+ C: number;
60
+ XC: number;
61
+ LXXX: number;
62
+ LXX: number;
63
+ LX: number;
64
+ L: number;
65
+ XL: number;
66
+ XXX: number;
67
+ XX: number;
68
+ XII: number;
69
+ XI: number;
70
+ X: number;
71
+ IX: number;
72
+ VIII: number;
73
+ VII: number;
74
+ VI: number;
75
+ V: number;
76
+ IV: number;
77
+ III: number;
78
+ II: number;
79
+ I: number;
80
+ };
81
+ /**
82
+ * Map of Unicode Roman numeral code points to their ASCII equivalents
83
+ */
84
+ declare const romanNumeralUnicodeToAsciiMap: Record<RomanNumeralUnicode, string>;
85
+ /**
86
+ * Captures all Unicode Roman numeral code points
87
+ */
88
+ declare const romanNumeralUnicodeRegex: RegExp;
89
+ /**
90
+ * Captures a valid Roman numeral sequence
91
+ *
92
+ * Capture groups:
93
+ *
94
+ * +=====+=================+==========================+
95
+ * | # | Description | Example |
96
+ * +=====+=================+==========================+
97
+ * | 0 | Entire string | "MCCXIV" from "MCCXIV" |
98
+ * +-----+-----------------+--------------------------+
99
+ * | 1 | Thousands | "M" from "MCCXIV" |
100
+ * +-----+-----------------+--------------------------+
101
+ * | 2 | Hundreds | "CC" from "MCCXIV" |
102
+ * +-----+-----------------+--------------------------+
103
+ * | 3 | Tens | "X" from "MCCXIV" |
104
+ * +-----+-----------------+--------------------------+
105
+ * | 4 | Ones | "IV" from "MCCXIV" |
106
+ * +=====+=================+==========================+
107
+ *
108
+ * @example
109
+ * romanNumeralRegex.exec("M") // [ "M", "M", "", "", "" ]
110
+ * romanNumeralRegex.exec("XII") // [ "XII", "", "", "X", "II" ]
111
+ * romanNumeralRegex.exec("MCCXIV") // [ "MCCXIV", "M", "CC", "X", "IV" ]
112
+ */
113
+ declare const romanNumeralRegex: RegExp;
114
+
115
+ /**
116
+ * Converts a string to a number, like an enhanced version of `parseFloat`.
117
+ *
118
+ * The string can include mixed numbers, vulgar fractions, or Roman numerals.
119
+ */
120
+ declare const numericQuantity: (quantity: string) => number;
121
+
122
+ declare const parseRomanNumerals: (romanNumerals: string) => number;
123
+
124
+ export { RomanNumeral, RomanNumeralAscii, RomanNumeralUnicode, VulgarFraction, numericQuantity, numericRegex, parseRomanNumerals, romanNumeralRegex, romanNumeralUnicodeRegex, romanNumeralUnicodeToAsciiMap, romanNumeralValues, vulgarFractionToAsciiMap, vulgarFractionsRegex };