format-quantity 2.1.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -23
- package/dist/cjs/format-quantity.cjs.development.d.ts +107 -0
- package/dist/cjs/format-quantity.cjs.development.js +183 -180
- package/dist/cjs/format-quantity.cjs.development.js.map +1 -1
- package/dist/cjs/format-quantity.cjs.production.d.ts +107 -0
- package/dist/cjs/format-quantity.cjs.production.js +1 -1
- package/dist/cjs/format-quantity.cjs.production.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/format-quantity.d.mts +107 -0
- package/dist/format-quantity.iife.umd.min.js +2 -0
- package/dist/format-quantity.iife.umd.min.js.map +1 -0
- package/dist/format-quantity.legacy-esm.d.ts +107 -0
- package/dist/format-quantity.legacy-esm.js +243 -165
- package/dist/format-quantity.legacy-esm.js.map +1 -1
- package/dist/format-quantity.mjs +178 -150
- package/dist/format-quantity.mjs.map +1 -1
- package/dist/format-quantity.production.d.mts +107 -0
- package/dist/format-quantity.production.mjs +1 -1
- package/dist/format-quantity.production.mjs.map +1 -1
- package/package.json +35 -22
- package/dist/format-quantity.d.ts +0 -83
- package/dist/format-quantity.umd.min.js +0 -2
- package/dist/format-quantity.umd.min.js.map +0 -1
|
@@ -1,176 +1,254 @@
|
|
|
1
|
-
|
|
2
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
-
var __spreadValues = (a, b) => {
|
|
7
|
-
for (var prop in b || (b = {}))
|
|
8
|
-
if (__hasOwnProp.call(b, prop))
|
|
9
|
-
__defNormalProp(a, prop, b[prop]);
|
|
10
|
-
if (__getOwnPropSymbols)
|
|
11
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
-
if (__propIsEnum.call(b, prop))
|
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
|
14
|
-
}
|
|
15
|
-
return a;
|
|
16
|
-
};
|
|
1
|
+
import { numericQuantity } from "numeric-quantity";
|
|
17
2
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
3
|
+
//#region src/constants.ts
|
|
4
|
+
/**
|
|
5
|
+
* Default tolerance used by {@link formatQuantity} when determining if a number
|
|
6
|
+
* is close enough to a fraction value to be considered equivalent.
|
|
7
|
+
*/
|
|
8
|
+
const defaultTolerance = .0075;
|
|
9
|
+
/**
|
|
10
|
+
* Default options for {@link formatQuantity}.
|
|
11
|
+
*/
|
|
12
|
+
const defaultOptions = {
|
|
13
|
+
vulgarFractions: false,
|
|
14
|
+
tolerance: defaultTolerance,
|
|
15
|
+
fractionSlash: false,
|
|
16
|
+
romanNumerals: false
|
|
25
17
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Map of vulgar fractions to their traditional ASCII equivalents.
|
|
20
|
+
*/
|
|
21
|
+
const vulgarToAsciiMap = {
|
|
22
|
+
"¼": "1/4",
|
|
23
|
+
"½": "1/2",
|
|
24
|
+
"¾": "3/4",
|
|
25
|
+
"⅐": "1/7",
|
|
26
|
+
"⅑": "1/9",
|
|
27
|
+
"⅒": "1/10",
|
|
28
|
+
"⅓": "1/3",
|
|
29
|
+
"⅔": "2/3",
|
|
30
|
+
"⅕": "1/5",
|
|
31
|
+
"⅖": "2/5",
|
|
32
|
+
"⅗": "3/5",
|
|
33
|
+
"⅘": "4/5",
|
|
34
|
+
"⅙": "1/6",
|
|
35
|
+
"⅚": "5/6",
|
|
36
|
+
"⅛": "1/8",
|
|
37
|
+
"⅜": "3/8",
|
|
38
|
+
"⅝": "5/8",
|
|
39
|
+
"⅞": "7/8"
|
|
45
40
|
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Map of "close enough" decimal values to the {@link VulgarFraction} or
|
|
43
|
+
* {@link Sixteenth} fraction string matches.
|
|
44
|
+
*/
|
|
45
|
+
const fractionDecimalMatches = [
|
|
46
|
+
[.33, "⅓"],
|
|
47
|
+
[.66, "⅔"],
|
|
48
|
+
[.2, "⅕"],
|
|
49
|
+
[.4, "⅖"],
|
|
50
|
+
[.6, "⅗"],
|
|
51
|
+
[.8, "⅘"],
|
|
52
|
+
[.166, "⅙"],
|
|
53
|
+
[.833, "⅚"],
|
|
54
|
+
[.143, "⅐"],
|
|
55
|
+
[.111, "⅑"],
|
|
56
|
+
[.1, "⅒"],
|
|
57
|
+
[.125, "⅛"],
|
|
58
|
+
[.25, "¼"],
|
|
59
|
+
[.375, "⅜"],
|
|
60
|
+
[.5, "½"],
|
|
61
|
+
[.625, "⅝"],
|
|
62
|
+
[.75, "¾"],
|
|
63
|
+
[.875, "⅞"],
|
|
64
|
+
[.0625, "1/16"],
|
|
65
|
+
[.1875, "3/16"],
|
|
66
|
+
[.3125, "5/16"],
|
|
67
|
+
[.4375, "7/16"],
|
|
68
|
+
[.5625, "9/16"],
|
|
69
|
+
[.6875, "11/16"],
|
|
70
|
+
[.8125, "13/16"],
|
|
71
|
+
[.9375, "15/16"]
|
|
73
72
|
];
|
|
74
73
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region \0@oxc-project+runtime@0.112.0/helpers/typeof.js
|
|
76
|
+
function _typeof(o) {
|
|
77
|
+
"@babel/helpers - typeof";
|
|
78
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
79
|
+
return typeof o;
|
|
80
|
+
} : function(o) {
|
|
81
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
82
|
+
}, _typeof(o);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region \0@oxc-project+runtime@0.112.0/helpers/toPrimitive.js
|
|
87
|
+
function toPrimitive(t, r) {
|
|
88
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
89
|
+
var e = t[Symbol.toPrimitive];
|
|
90
|
+
if (void 0 !== e) {
|
|
91
|
+
var i = e.call(t, r || "default");
|
|
92
|
+
if ("object" != _typeof(i)) return i;
|
|
93
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
94
|
+
}
|
|
95
|
+
return ("string" === r ? String : Number)(t);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region \0@oxc-project+runtime@0.112.0/helpers/toPropertyKey.js
|
|
100
|
+
function toPropertyKey(t) {
|
|
101
|
+
var i = toPrimitive(t, "string");
|
|
102
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region \0@oxc-project+runtime@0.112.0/helpers/defineProperty.js
|
|
107
|
+
function _defineProperty(e, r, t) {
|
|
108
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
109
|
+
value: t,
|
|
110
|
+
enumerable: !0,
|
|
111
|
+
configurable: !0,
|
|
112
|
+
writable: !0
|
|
113
|
+
}) : e[r] = t, e;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region \0@oxc-project+runtime@0.112.0/helpers/objectSpread2.js
|
|
118
|
+
function ownKeys(e, r) {
|
|
119
|
+
var t = Object.keys(e);
|
|
120
|
+
if (Object.getOwnPropertySymbols) {
|
|
121
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
122
|
+
r && (o = o.filter(function(r) {
|
|
123
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
124
|
+
})), t.push.apply(t, o);
|
|
125
|
+
}
|
|
126
|
+
return t;
|
|
127
|
+
}
|
|
128
|
+
function _objectSpread2(e) {
|
|
129
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
130
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
131
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function(r) {
|
|
132
|
+
_defineProperty(e, r, t[r]);
|
|
133
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r) {
|
|
134
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return e;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region src/formatQuantity.ts
|
|
142
|
+
/**
|
|
143
|
+
* Determines if two numbers are close enough to consider
|
|
144
|
+
* them equal for the purposes of this package.
|
|
145
|
+
*/
|
|
146
|
+
const closeEnough = (n1, n2, tolerance) => Math.abs(n1 - n2) < tolerance;
|
|
147
|
+
const superscriptDigits = "⁰¹²³⁴⁵⁶⁷⁸⁹";
|
|
148
|
+
const subscriptDigits = "₀₁₂₃₄₅₆₇₈₉";
|
|
149
|
+
const toSuperscript = (s) => {
|
|
150
|
+
let r = "";
|
|
151
|
+
for (let i = 0; i < s.length; i++) r += superscriptDigits[+s[i]];
|
|
152
|
+
return r;
|
|
153
|
+
};
|
|
154
|
+
const toSubscript = (s) => {
|
|
155
|
+
let r = "";
|
|
156
|
+
for (let i = 0; i < s.length; i++) r += subscriptDigits[+s[i]];
|
|
157
|
+
return r;
|
|
87
158
|
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Applies the `vulgarFractions` or `fractionSlash` options as necessary.
|
|
161
|
+
*/
|
|
162
|
+
const getFraction = (vulgarFractionOrSixteenth, { fractionSlash, vulgarFractions }) => {
|
|
163
|
+
var _vulgarToAsciiMap;
|
|
164
|
+
if (vulgarFractions) return vulgarFractionOrSixteenth;
|
|
165
|
+
const plainFraction = (_vulgarToAsciiMap = vulgarToAsciiMap[vulgarFractionOrSixteenth]) !== null && _vulgarToAsciiMap !== void 0 ? _vulgarToAsciiMap : vulgarFractionOrSixteenth;
|
|
166
|
+
if (fractionSlash) {
|
|
167
|
+
const [num, den] = plainFraction.split("/");
|
|
168
|
+
return `${toSuperscript(num)}⁄${toSubscript(den)}`;
|
|
169
|
+
}
|
|
170
|
+
return plainFraction;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Merges options object with default options, converting boolean to object if necessary.
|
|
174
|
+
*/
|
|
175
|
+
const normalizeOptions = (options) => _objectSpread2(_objectSpread2({}, defaultOptions), typeof options === "boolean" ? { vulgarFractions: options } : options);
|
|
176
|
+
const romanNumeralValueKey = [
|
|
177
|
+
"",
|
|
178
|
+
"C",
|
|
179
|
+
"CC",
|
|
180
|
+
"CCC",
|
|
181
|
+
"CD",
|
|
182
|
+
"D",
|
|
183
|
+
"DC",
|
|
184
|
+
"DCC",
|
|
185
|
+
"DCCC",
|
|
186
|
+
"CM",
|
|
187
|
+
"",
|
|
188
|
+
"X",
|
|
189
|
+
"XX",
|
|
190
|
+
"XXX",
|
|
191
|
+
"XL",
|
|
192
|
+
"L",
|
|
193
|
+
"LX",
|
|
194
|
+
"LXX",
|
|
195
|
+
"LXXX",
|
|
196
|
+
"XC",
|
|
197
|
+
"",
|
|
198
|
+
"I",
|
|
199
|
+
"II",
|
|
200
|
+
"III",
|
|
201
|
+
"IV",
|
|
202
|
+
"V",
|
|
203
|
+
"VI",
|
|
204
|
+
"VII",
|
|
205
|
+
"VIII",
|
|
206
|
+
"IX"
|
|
120
207
|
];
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
roman = `${romanNumeralValueKey[+digits.pop() + i * 10] || ""}${roman}`;
|
|
134
|
-
}
|
|
135
|
-
return `${Array(+digits.join("") + 1).join("M")}${roman}`;
|
|
208
|
+
/**
|
|
209
|
+
* Formats a number as Roman numerals. The number must be between
|
|
210
|
+
* 1 and 3999, inclusive.
|
|
211
|
+
*/
|
|
212
|
+
const formatRomanNumerals = (qty) => {
|
|
213
|
+
if (typeof qty !== "number" || isNaN(qty)) return null;
|
|
214
|
+
if (qty < 1 || qty >= 4e3) return "";
|
|
215
|
+
const digits = `${Math.floor(qty)}`.split("");
|
|
216
|
+
let roman = "";
|
|
217
|
+
let i = 3;
|
|
218
|
+
while (i--) roman = `${romanNumeralValueKey[+digits.pop() + i * 10] || ""}${roman}`;
|
|
219
|
+
return `${Array(+digits.join("") + 1).join("M")}${roman}`;
|
|
136
220
|
};
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Formats a number (or string that appears to be a number)
|
|
223
|
+
* as one would see it written in imperial measurements, e.g.
|
|
224
|
+
* "1 1/2" instead of "1.5". To use vulgar fraction characters
|
|
225
|
+
* like "½", pass `true` as the second argument. For other options
|
|
226
|
+
* see {@link FormatQuantityOptions}.
|
|
227
|
+
*/
|
|
228
|
+
const formatQuantity = (qty, options = defaultOptions) => {
|
|
229
|
+
const qtyAsNumber = typeof qty === "string" ? numericQuantity(qty, {
|
|
230
|
+
round: false,
|
|
231
|
+
allowTrailingInvalid: true
|
|
232
|
+
}) : qty;
|
|
233
|
+
if (isNaN(qtyAsNumber) || qtyAsNumber === null) return null;
|
|
234
|
+
if (qtyAsNumber === 0) return "";
|
|
235
|
+
const opts = normalizeOptions(options !== null && options !== void 0 ? options : defaultOptions);
|
|
236
|
+
if (opts.romanNumerals) return formatRomanNumerals(qtyAsNumber);
|
|
237
|
+
const absoluteValue = Math.abs(qtyAsNumber);
|
|
238
|
+
const flooredAbsVal = Math.floor(absoluteValue);
|
|
239
|
+
const sign = qtyAsNumber < 0 ? "-" : "";
|
|
240
|
+
const wholeStr = flooredAbsVal === 0 ? "" : `${flooredAbsVal}`;
|
|
241
|
+
const decimalValue = absoluteValue - flooredAbsVal;
|
|
242
|
+
if (decimalValue === 0) return `${qtyAsNumber}`;
|
|
243
|
+
for (const [num, vf] of fractionDecimalMatches) if (closeEnough(decimalValue, num, opts.tolerance)) {
|
|
244
|
+
var _opts$separator;
|
|
245
|
+
const fraction = getFraction(vf, opts);
|
|
246
|
+
const isVulgar = fraction in vulgarToAsciiMap;
|
|
247
|
+
return `${sign}${wholeStr}${wholeStr ? (_opts$separator = opts.separator) !== null && _opts$separator !== void 0 ? _opts$separator : isVulgar ? "" : " " : ""}${fraction}`;
|
|
248
|
+
}
|
|
249
|
+
return `${qtyAsNumber}`;
|
|
164
250
|
};
|
|
165
251
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
export {
|
|
169
|
-
src_default as default,
|
|
170
|
-
defaultOptions,
|
|
171
|
-
defaultTolerance,
|
|
172
|
-
formatQuantity,
|
|
173
|
-
fractionDecimalMatches,
|
|
174
|
-
vulgarToAsciiMap
|
|
175
|
-
};
|
|
252
|
+
//#endregion
|
|
253
|
+
export { defaultOptions, defaultTolerance, formatQuantity, formatRomanNumerals, fractionDecimalMatches, vulgarToAsciiMap };
|
|
176
254
|
//# sourceMappingURL=format-quantity.legacy-esm.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/constants.ts","../src/formatQuantity.ts"
|
|
1
|
+
{"version":3,"file":"format-quantity.legacy-esm.js","names":[],"sources":["../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import type {\n ResolvedFormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Default tolerance used by {@link formatQuantity} when determining if a number\n * is close enough to a fraction value to be considered equivalent.\n */\nexport const defaultTolerance = 0.0075 as const;\n\n/**\n * Default options for {@link formatQuantity}.\n */\nexport const defaultOptions: ResolvedFormatQuantityOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap: Record<VulgarFraction, SimpleFraction> = {\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} as const;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches: [number, VulgarFraction | Sixteenth][] = [\n [0.33, '⅓'],\n [0.66, '⅔'],\n [0.2, '⅕'],\n [0.4, '⅖'],\n [0.6, '⅗'],\n [0.8, '⅘'],\n [0.166, '⅙'],\n [0.833, '⅚'],\n [0.143, '⅐'],\n [0.111, '⅑'],\n [0.1, '⅒'],\n [0.125, '⅛'],\n [0.25, '¼'],\n [0.375, '⅜'],\n [0.5, '½'],\n [0.625, '⅝'],\n [0.75, '¾'],\n [0.875, '⅞'],\n [0.0625, '1/16'],\n [0.1875, '3/16'],\n [0.3125, '5/16'],\n [0.4375, '7/16'],\n [0.5625, '9/16'],\n [0.6875, '11/16'],\n [0.8125, '13/16'],\n [0.9375, '15/16'],\n] as const;\n","import { numericQuantity } from 'numeric-quantity';\nimport {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\n ResolvedFormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Determines if two numbers are close enough to consider\n * them equal for the purposes of this package.\n */\nconst closeEnough = (n1: number, n2: number, tolerance: number) =>\n Math.abs(n1 - n2) < tolerance;\n\nconst superscriptDigits = '⁰¹²³⁴⁵⁶⁷⁸⁹';\nconst subscriptDigits = '₀₁₂₃₄₅₆₇₈₉';\n\nconst toSuperscript = (s: string) => {\n let r = '';\n for (let i = 0; i < s.length; i++) r += superscriptDigits[+s[i]];\n return r;\n};\nconst toSubscript = (s: string) => {\n let r = '';\n for (let i = 0; i < s.length; i++) r += subscriptDigits[+s[i]];\n return r;\n};\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: FormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ??\n vulgarFractionOrSixteenth;\n\n if (fractionSlash) {\n const [num, den] = plainFraction.split('/');\n return `${toSuperscript(num)}⁄${toSubscript(den)}`;\n }\n\n return plainFraction;\n};\n\n/**\n * Merges options object with default options, converting boolean to object if necessary.\n */\nconst normalizeOptions = (\n options: Parameters<FormatQuantity>[1]\n): ResolvedFormatQuantityOptions => ({\n ...defaultOptions,\n ...(typeof options === 'boolean' ? { vulgarFractions: options } : options),\n});\n\n// prettier-ignore\nconst romanNumeralValueKey = [\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\",\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\",\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\",\n] as const;\n\n/**\n * Formats a number as Roman numerals. The number must be between\n * 1 and 3999, inclusive.\n */\nexport const formatRomanNumerals = (qty: number): string | null => {\n if (typeof qty !== 'number' || isNaN(qty)) {\n return null;\n }\n\n if (qty < 1 || qty >= 4000) {\n return '';\n }\n\n const floored = Math.floor(qty);\n\n const digits = `${floored}`.split('');\n let roman = '';\n let i = 3;\n while (i--) {\n roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;\n }\n\n return `${Array(+digits.join('') + 1).join('M')}${roman}`;\n};\n\n/**\n * Formats a number (or string that appears to be a number)\n * as one would see it written in imperial measurements, e.g.\n * \"1 1/2\" instead of \"1.5\". To use vulgar fraction characters\n * like \"½\", pass `true` as the second argument. For other options\n * see {@link FormatQuantityOptions}.\n */\nexport const formatQuantity: FormatQuantity = (\n qty,\n options = defaultOptions\n) => {\n const qtyAsNumber =\n typeof qty === 'string'\n ? numericQuantity(qty, { round: false, allowTrailingInvalid: true })\n : qty;\n\n // Return `null` if input is not number-like.\n if (isNaN(qtyAsNumber) || qtyAsNumber === null) {\n return null;\n }\n\n // Return an empty string if the value is zero.\n // TODO: Consider a `zeroDisplay` option (e.g. `{ zeroDisplay: \"0\" }`) so\n // callers outside the recipe-ingredient use case can get \"0\" instead of \"\".\n if (qtyAsNumber === 0) {\n return '';\n }\n\n // The default options parameter in the function signature only takes effect\n // if the parameter is `undefined`. The nullish coalescing operator below\n // covers the `null` case.\n const opts = normalizeOptions(options ?? defaultOptions);\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue = Math.abs(qtyAsNumber);\n const flooredAbsVal = Math.floor(absoluteValue);\n const sign = qtyAsNumber < 0 ? '-' : '';\n const wholeStr = flooredAbsVal === 0 ? '' : `${flooredAbsVal}`;\n const decimalValue = absoluteValue - flooredAbsVal;\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(decimalValue, num, opts.tolerance)) {\n const fraction = getFraction(vf, opts);\n const isVulgar = fraction in vulgarToAsciiMap;\n const sep = wholeStr\n ? (opts.separator ?? (isVulgar ? '' : ' '))\n : '';\n return `${sign}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