numeric-quantity 1.0.4 → 2.0.0-beta.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 CHANGED
@@ -1,47 +1,66 @@
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
+ ### CDN
29
+
30
+ As an ES module:
31
+
32
+ ```html
33
+ <script type="module">
34
+ import { numericQuantity } from 'https://cdn.jsdelivr.net/npm/numeric-quantity/+esm';
35
+
36
+ console.log(numericQuantity('10½')); // 10.5
37
+ </script>
38
+ ```
28
39
 
29
- In the browser, available as a global function `numericQuantity`.
40
+ As UMD (all exports are properties of the global object `NumericQuantity`):
30
41
 
31
42
  ```html
32
43
  <script src="https://unpkg.com/numeric-quantity"></script>
33
44
  <script>
34
- console.log(numericQuantity('10 1/2')); // 10.5
45
+ console.log(NumericQuantity.numericQuantity('xii')); // 12
35
46
  </script>
36
47
  ```
37
48
 
38
- ## Usage
49
+ ## Other exports
39
50
 
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
45
- ```
51
+ | Name | Type | Description |
52
+ | ------------------------------- | ---------- | ---------------------------------------------------------------------------------------------------- |
53
+ | `numericRegex` | `RegExp` | Regular expression matching a string that resembles a number (using Arabic numerals) in its entirety |
54
+ | `VulgarFraction` | `type` | Union type of all unicode vulgar fraction code points |
55
+ | `vulgarFractionsRegex` | `RegExp` | Regular expression matching the first unicode vulgar fraction code point |
56
+ | `vulgarFractionToAsciiMap` | `object` | Mapping of each vulgar fraction to its traditional ASCII representation (e.g., `'½'` to `'1/2'`) |
57
+ | `parseRomanNumerals` | `function` | Same function signature as `numericQuantity`, but only for Roman numerals (used internally) |
58
+ | `romanNumeralRegex` | `RegExp` | Regular expression matching valid Roman numeral sequences (uses modern, strict rules) |
59
+ | `romanNumeralUnicodeRegex` | `RegExp` | Regular expression matching any unicode Roman numeral code point |
60
+ | `romanNumeralUnicodeToAsciiMap` | `object` | Mapping of each Roman numeral to its traditional ASCII representation (e.g., `'Ⅻ'` to `'XII'`) |
61
+ | `romanNumeralValues` | `object` | Mapping of each valid Roman numeral sequence fragment to its numeric value |
62
+ | `RomanNumeralAscii` | `type` | Union type of allowable Roman numeral characters (uppercase only) |
63
+ | `RomanNumeralUnicode` | `type` | Union type of all Unicode Roman numeral characters (representing 1-12, 50, 100, 500, and 1000) |
64
+ | `RomanNumeral` | `type` | Union type of `RomanNumeralAscii` and `RomanNumeralUnicode` |
46
65
 
47
- The return value will be `NaN` if the provided string does not resemble a number.
66
+ [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,238 @@
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)?)*)(\.\d(?:[\d,_]*\d)?|(\s+\d(?:[\d,_]*\d)?\s*)?\s*\/\s*\d(?:[\d,_]*\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
+ if (typeof quantity === "number" || typeof quantity === "bigint") {
187
+ return quantity;
188
+ }
189
+ let finalResult = NaN;
190
+ const quantityAsString = `${quantity}`.replace(
191
+ vulgarFractionsRegex,
192
+ (_m, vf) => ` ${vulgarFractionToAsciiMap[vf]}`
193
+ ).replace("\u2044", "/").trim();
194
+ if (quantityAsString.length === 0) {
195
+ return NaN;
196
+ }
197
+ const regexResult = numericRegex.exec(quantityAsString);
198
+ if (!regexResult) {
199
+ return parseRomanNumerals(quantityAsString);
200
+ }
201
+ const [, dash, ng1temp, ng2temp] = regexResult;
202
+ const numberGroup1 = ng1temp.replace(/[,_]/g, "");
203
+ const numberGroup2 = ng2temp == null ? void 0 : ng2temp.replace(/[,_]/g, "");
204
+ if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith(".")) {
205
+ finalResult = 0;
206
+ } else {
207
+ finalResult = parseInt(numberGroup1);
208
+ }
209
+ if (!numberGroup2) {
210
+ return dash ? finalResult * -1 : finalResult;
211
+ }
212
+ if (numberGroup2.startsWith(".")) {
213
+ const numerator = parseFloat(numberGroup2);
214
+ finalResult += Math.round(numerator * 1e3) / 1e3;
215
+ } else if (spaceThenSlashRegex.test(numberGroup2)) {
216
+ const numerator = parseInt(numberGroup1);
217
+ const denominator = parseInt(numberGroup2.replace("/", ""));
218
+ finalResult = Math.round(numerator * 1e3 / denominator) / 1e3;
219
+ } else {
220
+ const fractionArray = numberGroup2.split("/");
221
+ const [numerator, denominator] = fractionArray.map((v) => parseInt(v));
222
+ finalResult += Math.round(numerator * 1e3 / denominator) / 1e3;
223
+ }
224
+ return dash ? finalResult * -1 : finalResult;
225
+ };
226
+ // Annotate the CommonJS export names for ESM import in node:
227
+ 0 && (module.exports = {
228
+ numericQuantity,
229
+ numericRegex,
230
+ parseRomanNumerals,
231
+ romanNumeralRegex,
232
+ romanNumeralUnicodeRegex,
233
+ romanNumeralUnicodeToAsciiMap,
234
+ romanNumeralValues,
235
+ vulgarFractionToAsciiMap,
236
+ vulgarFractionsRegex
237
+ });
238
+ //# 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)?)*)(\\.\\d(?:[\\d,_]*\\d)?|(\\s+\\d(?:[\\d,_]*\\d)?\\s*)?\\s*\\/\\s*\\d(?:[\\d,_]*\\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 keyof typeof romanNumeralValues\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 romanNumeralRegex,\n romanNumeralUnicodeRegex,\n romanNumeralUnicodeToAsciiMap,\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 | number) => {\n if (typeof quantity === 'number' || typeof quantity === 'bigint') {\n return quantity;\n }\n\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, ng2temp] = regexResult;\n const numberGroup1 = ng1temp.replace(/[,_]/g, '');\n const numberGroup2 = ng2temp?.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 dash ? finalResult * -1 : finalResult;\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 dash ? finalResult * -1 : finalResult;\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,KA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,OAAO;AACL,kBAAc,SAAS,YAAY;AAAA,EACrC;AAIA,MAAI,CAAC,cAAc;AACjB,WAAO,OAAO,cAAc,KAAK;AAAA,EACnC;AAEA,MAAI,aAAa,WAAW,GAAG,GAAG;AAEhC,UAAM,YAAY,WAAW,YAAY;AACzC,mBAAe,KAAK,MAAM,YAAY,GAAI,IAAI;AAAA,EAChD,WAAW,oBAAoB,KAAK,YAAY,GAAG;AAEjD,UAAM,YAAY,SAAS,YAAY;AACvC,UAAM,cAAc,SAAS,aAAa,QAAQ,KAAK,EAAE,CAAC;AAC1D,kBAAc,KAAK,MAAO,YAAY,MAAQ,WAAW,IAAI;AAAA,EAC/D,OAAO;AAEL,UAAM,gBAAgB,aAAa,MAAM,GAAG;AAC5C,UAAM,CAAC,WAAW,WAAW,IAAI,cAAc,IAAI,OAAK,SAAS,CAAC,CAAC;AACnE,mBAAe,KAAK,MAAO,YAAY,MAAQ,WAAW,IAAI;AAAA,EAChE;AAEA,SAAO,OAAO,cAAc,KAAK;AACnC;","names":[]}
@@ -0,0 +1,2 @@
1
+ "use strict";var N=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var y=(r,e)=>{for(var o in e)N(r,o,{get:e[o],enumerable:!0})},$=(r,e,o,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of M(e))!A.call(r,a)&&a!==o&&N(r,a,{get:()=>e[a],enumerable:!(t=C(e,a))||t.enumerable});return r};var h=r=>$(N({},"__esModule",{value:!0}),r);var L={};y(L,{numericQuantity:()=>F,numericRegex:()=>R,parseRomanNumerals:()=>V,romanNumeralRegex:()=>f,romanNumeralUnicodeRegex:()=>g,romanNumeralUnicodeToAsciiMap:()=>I,romanNumeralValues:()=>m,vulgarFractionToAsciiMap:()=>l,vulgarFractionsRegex:()=>d});module.exports=h(L);var l={"\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/"},R=/^(?=-?\s*\.\d|-?\s*\d)(-)?\s*((?:\d(?:[\d,_]*\d)?)*)(\.\d(?:[\d,_]*\d)?|(\s+\d(?:[\d,_]*\d)?\s*)?\s*\/\s*\d(?:[\d,_]*\d)?)?(?:\s*[^\.\d\/].*)?/,d=new RegExp(`(${Object.keys(l).join("|")})`),m={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"),f=/^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;var V=r=>{let e=`${r}`.replace(g,(u,n)=>I[n]).toUpperCase(),o=f.exec(e);if(!o)return NaN;let[,t,a,p,i]=o;return(m[t]||0)+(m[a]||0)+(m[p]||0)+(m[i]||0)};var D=/^\s*\//,F=r=>{if(typeof r=="number"||typeof r=="bigint")return r;let e=NaN,o=`${r}`.replace(d,(s,c)=>` ${l[c]}`).replace("\u2044","/").trim();if(o.length===0)return NaN;let t=R.exec(o);if(!t)return V(o);let[,a,p,i]=t,u=p.replace(/[,_]/g,""),n=i==null?void 0:i.replace(/[,_]/g,"");if(!u&&n&&n.startsWith(".")?e=0:e=parseInt(u),!n)return a?e*-1:e;if(n.startsWith(".")){let s=parseFloat(n);e+=Math.round(s*1e3)/1e3}else if(D.test(n)){let s=parseInt(u),c=parseInt(n.replace("/",""));e=Math.round(s*1e3/c)/1e3}else{let s=n.split("/"),[c,X]=s.map(x=>parseInt(x));e+=Math.round(c*1e3/X)/1e3}return a?e*-1:e};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)?)*)(\\.\\d(?:[\\d,_]*\\d)?|(\\s+\\d(?:[\\d,_]*\\d)?\\s*)?\\s*\\/\\s*\\d(?:[\\d,_]*\\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 keyof typeof romanNumeralValues\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 romanNumeralRegex,\n romanNumeralUnicodeRegex,\n romanNumeralUnicodeToAsciiMap,\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 | number) => {\n if (typeof quantity === 'number' || typeof quantity === 'bigint') {\n return quantity;\n }\n\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, ng2temp] = regexResult;\n const numberGroup1 = ng1temp.replace(/[,_]/g, '');\n const numberGroup2 = ng2temp?.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 dash ? finalResult * -1 : finalResult;\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 dash ? finalResult * -1 : finalResult;\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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G,EACtC,CAACC,EAAWC,CAAW,EAAIC,EAAc,IAAIC,GAAK,SAASA,CAAC,CAAC,EACnEjB,GAAe,KAAK,MAAOc,EAAY,IAAQC,CAAW,EAAI,IAGhE,OAAON,EAAOT,EAAc,GAAKA,CACnC","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","ng2temp","numberGroup1","numberGroup2","numerator","denominator","fractionArray","v"]}
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Unicode vulgar fraction code points
3
+ */
4
+ type VulgarFraction = '¼' | '½' | '¾' | '⅐' | '⅑' | '⅒' | '⅓' | '⅔' | '⅕' | '⅖' | '⅗' | '⅘' | '⅙' | '⅚' | '⅛' | '⅜' | '⅝' | '⅞' | '⅟';
5
+ /**
6
+ * Allowable Roman numeral characters (ASCII, uppercase only)
7
+ */
8
+ type RomanNumeralAscii = 'I' | 'V' | 'X' | 'L' | 'C' | 'D' | 'M';
9
+ /**
10
+ * Unicode Roman numeral code points (uppercase and lowercase,
11
+ * representing 1-12, 50, 100, 500, and 1000)
12
+ */
13
+ type RomanNumeralUnicode = 'Ⅰ' | 'Ⅱ' | 'Ⅲ' | 'Ⅳ' | 'Ⅴ' | 'Ⅵ' | 'Ⅶ' | 'Ⅷ' | 'Ⅸ' | 'Ⅹ' | 'Ⅺ' | 'Ⅻ' | 'Ⅼ' | 'Ⅽ' | 'Ⅾ' | 'Ⅿ' | 'ⅰ' | 'ⅱ' | 'ⅲ' | 'ⅳ' | 'ⅴ' | 'ⅵ' | 'ⅶ' | 'ⅷ' | 'ⅸ' | 'ⅹ' | 'ⅺ' | 'ⅻ' | 'ⅼ' | 'ⅽ' | 'ⅾ' | 'ⅿ';
14
+ /**
15
+ * Union of ASCII and Unicode Roman numeral characters/code points
16
+ */
17
+ type RomanNumeral = RomanNumeralAscii | RomanNumeralUnicode;
18
+
19
+ /**
20
+ * Map of Unicode fraction code points to their ASCII equivalents
21
+ */
22
+ declare const vulgarFractionToAsciiMap: Record<VulgarFraction, string>;
23
+ /**
24
+ * Captures the individual elements of a numeric string.
25
+ *
26
+ * Capture groups:
27
+ *
28
+ * +=====+====================+========================+
29
+ * | # | Description | Example |
30
+ * +=====+====================+========================+
31
+ * | 0 | entire string | "2 1/3" from "2 1/3" |
32
+ * +-----+--------------------+------------------------+
33
+ * | 1 | "negative" dash | "-" from "-2 1/3" |
34
+ * +-----+--------------------+------------------------+
35
+ * | 2 | the whole number | "2" from "2 1/3" |
36
+ * | | - OR - | |
37
+ * | | the numerator | "1" from "1/3" |
38
+ * | + + |
39
+ * | (This may include comma/underscore separators) |
40
+ * +-----+--------------------+------------------------+
41
+ * | 3 | entire fraction | "1/3" from "2 1/3" |
42
+ * | | - OR - | |
43
+ * | | decimal portion | ".33" from "2.33" |
44
+ * | | - OR - | |
45
+ * | | denominator | "/3" from "1/3" |
46
+ * +=====+====================+========================+
47
+ *
48
+ * @example
49
+ * numericRegex.exec("1") // [ "1", "1", null, null ]
50
+ * numericRegex.exec("1.23") // [ "1.23", "1", ".23", null ]
51
+ * numericRegex.exec("1 2/3") // [ "1 2/3", "1", " 2/3", " 2" ]
52
+ * numericRegex.exec("2/3") // [ "2/3", "2", "/3", null ]
53
+ * numericRegex.exec("2 / 3") // [ "2 / 3", "2", "/ 3", null ]
54
+ */
55
+ declare const numericRegex: RegExp;
56
+ /**
57
+ * Captures any Unicode vulgar fractions
58
+ */
59
+ declare const vulgarFractionsRegex: RegExp;
60
+ declare const romanNumeralValues: {
61
+ MMM: number;
62
+ MM: number;
63
+ M: number;
64
+ CM: number;
65
+ DCCC: number;
66
+ DCC: number;
67
+ DC: number;
68
+ D: number;
69
+ CD: number;
70
+ CCC: number;
71
+ CC: number;
72
+ C: number;
73
+ XC: number;
74
+ LXXX: number;
75
+ LXX: number;
76
+ LX: number;
77
+ L: number;
78
+ XL: number;
79
+ XXX: number;
80
+ XX: number;
81
+ XII: number;
82
+ XI: number;
83
+ X: number;
84
+ IX: number;
85
+ VIII: number;
86
+ VII: number;
87
+ VI: number;
88
+ V: number;
89
+ IV: number;
90
+ III: number;
91
+ II: number;
92
+ I: number;
93
+ };
94
+ /**
95
+ * Map of Unicode Roman numeral code points to their ASCII equivalents
96
+ */
97
+ declare const romanNumeralUnicodeToAsciiMap: Record<RomanNumeralUnicode, keyof typeof romanNumeralValues>;
98
+ /**
99
+ * Captures all Unicode Roman numeral code points
100
+ */
101
+ declare const romanNumeralUnicodeRegex: RegExp;
102
+ /**
103
+ * Captures a valid Roman numeral sequence
104
+ *
105
+ * Capture groups:
106
+ *
107
+ * +=====+=================+==========================+
108
+ * | # | Description | Example |
109
+ * +=====+=================+==========================+
110
+ * | 0 | Entire string | "MCCXIV" from "MCCXIV" |
111
+ * +-----+-----------------+--------------------------+
112
+ * | 1 | Thousands | "M" from "MCCXIV" |
113
+ * +-----+-----------------+--------------------------+
114
+ * | 2 | Hundreds | "CC" from "MCCXIV" |
115
+ * +-----+-----------------+--------------------------+
116
+ * | 3 | Tens | "X" from "MCCXIV" |
117
+ * +-----+-----------------+--------------------------+
118
+ * | 4 | Ones | "IV" from "MCCXIV" |
119
+ * +=====+=================+==========================+
120
+ *
121
+ * @example
122
+ * romanNumeralRegex.exec("M") // [ "M", "M", "", "", "" ]
123
+ * romanNumeralRegex.exec("XII") // [ "XII", "", "", "X", "II" ]
124
+ * romanNumeralRegex.exec("MCCXIV") // [ "MCCXIV", "M", "CC", "X", "IV" ]
125
+ */
126
+ declare const romanNumeralRegex: RegExp;
127
+
128
+ /**
129
+ * Converts a string to a number, like an enhanced version of `parseFloat`.
130
+ *
131
+ * The string can include mixed numbers, vulgar fractions, or Roman numerals.
132
+ */
133
+ declare const numericQuantity: (quantity: string | number) => number;
134
+
135
+ declare const parseRomanNumerals: (romanNumerals: string) => number;
136
+
137
+ export { RomanNumeral, RomanNumeralAscii, RomanNumeralUnicode, VulgarFraction, numericQuantity, numericRegex, parseRomanNumerals, romanNumeralRegex, romanNumeralUnicodeRegex, romanNumeralUnicodeToAsciiMap, romanNumeralValues, vulgarFractionToAsciiMap, vulgarFractionsRegex };