format-quantity 3.0.0 → 3.2.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.
@@ -0,0 +1,131 @@
1
+ //#region src/types.d.ts
2
+ interface FormatQuantityOptions {
3
+ /**
4
+ * Output vulgar fractions, like "½" instead of "1/2", when appropriate.
5
+ * Overrides the `fractionSlash` option.
6
+ *
7
+ * @default false
8
+ */
9
+ vulgarFractions?: boolean;
10
+ /**
11
+ * Amount by which a number can deviate from the calculated quotient to be
12
+ * considered a match. For example, 0.66 is close enough to 2 ÷ 3 (which
13
+ * is 0.66666... repeating) to be considered equivalent so the function
14
+ * will return "2/3". The smaller this number, the higher the likelihood that
15
+ * the function will return a decimal instead of a fraction or mixed number.
16
+ *
17
+ * `0` means only exact quotients match. `false` disables fraction matching
18
+ * entirely, so decimal values are always returned as decimals.
19
+ *
20
+ * Any other value—negative, non-numeric, `NaN`, `null`, `undefined`—resolves
21
+ * to the default.
22
+ *
23
+ * @default 0.0075
24
+ */
25
+ tolerance?: number | false;
26
+ /**
27
+ * Output the fraction slash character (⁄) instead of the "solidus"
28
+ * slash (/) for fractions. Results appear like "1⁄2" instead of "1/2".
29
+ * Overridden by the `vulgarFractions` option.
30
+ *
31
+ * @default false
32
+ */
33
+ fractionSlash?: boolean;
34
+ /**
35
+ * Output in Roman numerals. Provided value must be between 1 and 3999, inclusive.
36
+ * Decimal values will be ignored (`Math.floor` is used to remove them). Overrides
37
+ * all other options.
38
+ *
39
+ * @default false
40
+ */
41
+ romanNumerals?: boolean;
42
+ /**
43
+ * String to place between the whole number and fraction parts. When not specified,
44
+ * defaults to `" "` for ASCII and fraction-slash fractions, and `""` for vulgar
45
+ * fractions (preserving the standard typographic convention of no space before
46
+ * vulgar fraction characters).
47
+ */
48
+ separator?: string;
49
+ /**
50
+ * String to return when the input evaluates numerically to zero.
51
+ *
52
+ * @default "" (empty string)
53
+ */
54
+ zeroFormat?: string;
55
+ /**
56
+ * If `qty` is a string, allow trailing invalid characters after the numeric portion.
57
+ *
58
+ * @default true
59
+ */
60
+ allowTrailingInvalid?: boolean;
61
+ }
62
+ /**
63
+ * {@link FormatQuantityOptions} with all properties resolved to their
64
+ * default values, except {@link FormatQuantityOptions.separator | separator}
65
+ * which remains optional so that unset vs explicitly-set can be distinguished.
66
+ */
67
+ type ResolvedFormatQuantityOptions = Required<Omit<FormatQuantityOptions, "separator">> & Pick<FormatQuantityOptions, "separator">;
68
+ /**
69
+ * Function signature of {@link formatQuantity}.
70
+ */
71
+ interface FormatQuantity {
72
+ (qty: string | number | bigint, options?: boolean | FormatQuantityOptions): string | null;
73
+ }
74
+ /** Any numeric character. */
75
+ type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
76
+ /** Any numeric character except '0'. */
77
+ type NonZeroDigit = Exclude<Digit, "0">;
78
+ /**
79
+ * Fraction string with either one or two numeric characters in both the
80
+ * numerator and denominator (but not two characters in the numerator while
81
+ * the denominator only has one).
82
+ */
83
+ type SimpleFraction = `${NonZeroDigit}/${NonZeroDigit}` | `${NonZeroDigit}/${NonZeroDigit}${Digit}` | `${NonZeroDigit}${Digit}/${NonZeroDigit}${Digit}`;
84
+ /**
85
+ * Odd numerator sixteenth fraction strings.
86
+ */
87
+ type Sixteenth = `${"1" | "3" | "5" | "7" | "9" | "11" | "13" | "15"}/16`;
88
+ /**
89
+ * Unicode vulgar fraction code points.
90
+ */
91
+ type VulgarFraction = "¼" | "½" | "¾" | "⅐" | "⅑" | "⅒" | "⅓" | "⅔" | "⅕" | "⅖" | "⅗" | "⅘" | "⅙" | "⅚" | "⅛" | "⅜" | "⅝" | "⅞";
92
+ //#endregion
93
+ //#region src/constants.d.ts
94
+ /**
95
+ * Default tolerance used by {@link formatQuantity} when determining if a number
96
+ * is close enough to a fraction value to be considered equivalent.
97
+ */
98
+ declare const defaultTolerance: 0.0075;
99
+ /**
100
+ * Default options for {@link formatQuantity}.
101
+ */
102
+ declare const defaultOptions: Readonly<ResolvedFormatQuantityOptions>;
103
+ /**
104
+ * Map of vulgar fractions to their traditional ASCII equivalents.
105
+ */
106
+ declare const vulgarToAsciiMap: Readonly<Record<VulgarFraction, SimpleFraction>>;
107
+ /**
108
+ * Map of "close enough" values to the {@link VulgarFraction} or {@link Sixteenth} fraction
109
+ * string matches. The value +/- the `tolerance` option (or {@link defaultTolerance} if not
110
+ * specified) is considered close enough to match the fraction.
111
+ */
112
+ declare const fractionDecimalMatches: readonly (readonly [number, VulgarFraction | Sixteenth])[];
113
+ //#endregion
114
+ //#region src/formatQuantity.d.ts
115
+ /**
116
+ * Formats a number or bigint as Roman numerals. The number must be between
117
+ * 1 and 3999, inclusive; any other value—including non-numbers,
118
+ * `NaN`, and non-finite numbers—yields `null`.
119
+ */
120
+ declare const formatRomanNumerals: (quantity: number | bigint) => string | null;
121
+ /**
122
+ * Formats a number (or string that appears to be a number)
123
+ * as one would see it written in imperial measurements, e.g.
124
+ * "1 1/2" instead of "1.5". To use vulgar fraction characters
125
+ * like "½", pass `true` as the second argument. For other options
126
+ * see {@link FormatQuantityOptions}.
127
+ */
128
+ declare const formatQuantity: FormatQuantity;
129
+ //#endregion
130
+ export { Digit, FormatQuantity, FormatQuantityOptions, NonZeroDigit, ResolvedFormatQuantityOptions, SimpleFraction, Sixteenth, VulgarFraction, defaultOptions, defaultTolerance, formatQuantity, formatRomanNumerals, fractionDecimalMatches, vulgarToAsciiMap };
131
+ //# sourceMappingURL=format-quantity.legacy-esm.d.ts.map
@@ -1,172 +1,268 @@
1
- var __defProp = Object.defineProperty;
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;
1
+ import { numericQuantity } from "numeric-quantity";
2
+ //#region src/constants.ts
3
+ /**
4
+ * Default tolerance used by {@link formatQuantity} when determining if a number
5
+ * is close enough to a fraction value to be considered equivalent.
6
+ */
7
+ const defaultTolerance = .0075;
8
+ /**
9
+ * Default options for {@link formatQuantity}.
10
+ */
11
+ const defaultOptions = Object.freeze({
12
+ vulgarFractions: false,
13
+ tolerance: defaultTolerance,
14
+ fractionSlash: false,
15
+ romanNumerals: false,
16
+ zeroFormat: "",
17
+ allowTrailingInvalid: true
18
+ });
19
+ /**
20
+ * Map of vulgar fractions to their traditional ASCII equivalents.
21
+ */
22
+ const vulgarToAsciiMap = Object.freeze({
23
+ "¼": "1/4",
24
+ "½": "1/2",
25
+ "¾": "3/4",
26
+ "⅐": "1/7",
27
+ "⅑": "1/9",
28
+ "⅒": "1/10",
29
+ "⅓": "1/3",
30
+ "⅔": "2/3",
31
+ "⅕": "1/5",
32
+ "⅖": "2/5",
33
+ "⅗": "3/5",
34
+ "⅘": "4/5",
35
+ "⅙": "1/6",
36
+ "⅚": "5/6",
37
+ "⅛": "1/8",
38
+ "⅜": "3/8",
39
+ "⅝": "5/8",
40
+ "⅞": "7/8"
41
+ });
42
+ /**
43
+ * Map of "close enough" values to the {@link VulgarFraction} or {@link Sixteenth} fraction
44
+ * string matches. The value +/- the `tolerance` option (or {@link defaultTolerance} if not
45
+ * specified) is considered close enough to match the fraction.
46
+ */
47
+ const fractionDecimalMatches = Object.freeze([
48
+ [1 / 16, "1/16"],
49
+ [1 / 10, "⅒"],
50
+ [1 / 9, "⅑"],
51
+ [1 / 8, "⅛"],
52
+ [1 / 7, "⅐"],
53
+ [1 / 6, "⅙"],
54
+ [3 / 16, "3/16"],
55
+ [1 / 5, "⅕"],
56
+ [1 / 4, "¼"],
57
+ [5 / 16, "5/16"],
58
+ [1 / 3, "⅓"],
59
+ [3 / 8, "⅜"],
60
+ [2 / 5, "⅖"],
61
+ [7 / 16, "7/16"],
62
+ [1 / 2, "½"],
63
+ [9 / 16, "9/16"],
64
+ [3 / 5, "⅗"],
65
+ [5 / 8, "⅝"],
66
+ [2 / 3, "⅔"],
67
+ [11 / 16, "11/16"],
68
+ [3 / 4, "¾"],
69
+ [4 / 5, "⅘"],
70
+ [13 / 16, "13/16"],
71
+ [5 / 6, "⅚"],
72
+ [7 / 8, "⅞"],
73
+ [15 / 16, "15/16"]
74
+ ].map((entry) => Object.freeze(entry)));
75
+ //#endregion
76
+ //#region \0@oxc-project+runtime@0.147.0/helpers/esm/typeof.js
77
+ function _typeof(o) {
78
+ "@babel/helpers - typeof";
79
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
80
+ return typeof o;
81
+ } : function(o) {
82
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
83
+ }, _typeof(o);
84
+ }
85
+ //#endregion
86
+ //#region \0@oxc-project+runtime@0.147.0/helpers/esm/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
+ //#endregion
98
+ //#region \0@oxc-project+runtime@0.147.0/helpers/esm/toPropertyKey.js
99
+ function toPropertyKey(t) {
100
+ var i = toPrimitive(t, "string");
101
+ return "symbol" == _typeof(i) ? i : i + "";
102
+ }
103
+ //#endregion
104
+ //#region \0@oxc-project+runtime@0.147.0/helpers/esm/defineProperty.js
105
+ function _defineProperty(e, r, t) {
106
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
107
+ value: t,
108
+ enumerable: !0,
109
+ configurable: !0,
110
+ writable: !0
111
+ }) : e[r] = t, e;
112
+ }
113
+ //#endregion
114
+ //#region \0@oxc-project+runtime@0.147.0/helpers/esm/objectSpread2.js
115
+ function ownKeys(e, r) {
116
+ var t = Object.keys(e);
117
+ if (Object.getOwnPropertySymbols) {
118
+ var o = Object.getOwnPropertySymbols(e);
119
+ r && (o = o.filter(function(r) {
120
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
121
+ })), t.push.apply(t, o);
122
+ }
123
+ return t;
124
+ }
125
+ function _objectSpread2(e) {
126
+ for (var r = 1; r < arguments.length; r++) {
127
+ var t = null != arguments[r] ? arguments[r] : {};
128
+ r % 2 ? ownKeys(Object(t), !0).forEach(function(r) {
129
+ _defineProperty(e, r, t[r]);
130
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r) {
131
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
132
+ });
133
+ }
134
+ return e;
135
+ }
136
+ //#endregion
137
+ //#region src/formatQuantity.ts
138
+ const superscriptDigits = "⁰¹²³⁴⁵⁶⁷⁸⁹";
139
+ const subscriptDigits = "₀₁₂₃₄₅₆₇₈₉";
140
+ const toSuperscript = (s) => [...s].map((c) => superscriptDigits[+c]).join("");
141
+ const toSubscript = (s) => [...s].map((c) => subscriptDigits[+c]).join("");
142
+ /**
143
+ * Applies the `vulgarFractions` or `fractionSlash` options as necessary.
144
+ */
145
+ const getFraction = (vulgarFractionOrSixteenth, { fractionSlash, vulgarFractions }) => {
146
+ var _vulgarToAsciiMap;
147
+ if (vulgarFractions) return vulgarFractionOrSixteenth;
148
+ const plainFraction = (_vulgarToAsciiMap = vulgarToAsciiMap[vulgarFractionOrSixteenth]) !== null && _vulgarToAsciiMap !== void 0 ? _vulgarToAsciiMap : vulgarFractionOrSixteenth;
149
+ if (fractionSlash) {
150
+ const [num, den] = plainFraction.split("/");
151
+ return `${toSuperscript(num)}⁄${toSubscript(den)}`;
152
+ }
153
+ return plainFraction;
16
154
  };
17
-
18
- // src/constants.ts
19
- var defaultTolerance = 75e-4;
20
- var defaultOptions = {
21
- vulgarFractions: false,
22
- tolerance: defaultTolerance,
23
- fractionSlash: false,
24
- romanNumerals: false
25
- };
26
- var vulgarToAsciiMap = {
27
- "\xBC": "1/4",
28
- "\xBD": "1/2",
29
- "\xBE": "3/4",
30
- "\u2150": "1/7",
31
- "\u2151": "1/9",
32
- "\u2152": "1/10",
33
- "\u2153": "1/3",
34
- "\u2154": "2/3",
35
- "\u2155": "1/5",
36
- "\u2156": "2/5",
37
- "\u2157": "3/5",
38
- "\u2158": "4/5",
39
- "\u2159": "1/6",
40
- "\u215A": "5/6",
41
- "\u215B": "1/8",
42
- "\u215C": "3/8",
43
- "\u215D": "5/8",
44
- "\u215E": "7/8"
155
+ /**
156
+ * Only `false` (matching disabled) or a non-negative number is a valid
157
+ * `tolerance`. Everything else—negative numbers, `NaN`, non-numbers,
158
+ * `null`, `undefined`—resolves to {@link defaultTolerance}.
159
+ */
160
+ const isValidTolerance = (tolerance) => tolerance === false || typeof tolerance === "number" && tolerance >= 0;
161
+ /**
162
+ * Merges options object with default options, converting boolean to object if necessary.
163
+ */
164
+ const normalizeOptions = (options) => {
165
+ const opts = _objectSpread2(_objectSpread2({}, defaultOptions), typeof options === "boolean" ? { vulgarFractions: options } : typeof options === "object" && options !== null ? options : {});
166
+ if (!isValidTolerance(opts.tolerance)) opts.tolerance = defaultOptions.tolerance;
167
+ if (typeof opts.zeroFormat !== "string") opts.zeroFormat = defaultOptions.zeroFormat;
168
+ if (opts.allowTrailingInvalid !== false) opts.allowTrailingInvalid = true;
169
+ return opts;
45
170
  };
46
- var fractionDecimalMatches = [
47
- [0.33, "\u2153"],
48
- [0.66, "\u2154"],
49
- [0.2, "\u2155"],
50
- [0.4, "\u2156"],
51
- [0.6, "\u2157"],
52
- [0.8, "\u2158"],
53
- [0.166, "\u2159"],
54
- [0.833, "\u215A"],
55
- [0.143, "\u2150"],
56
- [0.111, "\u2151"],
57
- [0.1, "\u2152"],
58
- [0.125, "\u215B"],
59
- [0.25, "\xBC"],
60
- [0.375, "\u215C"],
61
- [0.5, "\xBD"],
62
- [0.625, "\u215D"],
63
- [0.75, "\xBE"],
64
- [0.875, "\u215E"],
65
- [0.0625, "1/16"],
66
- [0.1875, "3/16"],
67
- [0.3125, "5/16"],
68
- [0.4375, "7/16"],
69
- [0.5625, "9/16"],
70
- [0.6875, "11/16"],
71
- [0.8125, "13/16"],
72
- [0.9375, "15/16"]
171
+ const romanNumeralsByPlace = [
172
+ "",
173
+ "C",
174
+ "CC",
175
+ "CCC",
176
+ "CD",
177
+ "D",
178
+ "DC",
179
+ "DCC",
180
+ "DCCC",
181
+ "CM",
182
+ "",
183
+ "X",
184
+ "XX",
185
+ "XXX",
186
+ "XL",
187
+ "L",
188
+ "LX",
189
+ "LXX",
190
+ "LXXX",
191
+ "XC",
192
+ "",
193
+ "I",
194
+ "II",
195
+ "III",
196
+ "IV",
197
+ "V",
198
+ "VI",
199
+ "VII",
200
+ "VIII",
201
+ "IX"
73
202
  ];
74
-
75
- // src/formatQuantity.ts
76
- var closeEnough = (n1, n2, tolerance) => Math.abs(n1 - n2) < tolerance;
77
- var getFraction = (vulgarFractionOrSixteenth, { fractionSlash, vulgarFractions }) => {
78
- var _a;
79
- if (vulgarFractions) {
80
- return vulgarFractionOrSixteenth;
81
- }
82
- const plainFraction = (_a = vulgarToAsciiMap[vulgarFractionOrSixteenth]) != null ? _a : vulgarFractionOrSixteenth;
83
- if (fractionSlash) {
84
- return plainFraction.replace("/", "\u2044");
85
- }
86
- return plainFraction;
203
+ /**
204
+ * Formats a number or bigint as Roman numerals. The number must be between
205
+ * 1 and 3999, inclusive; any other value—including non-numbers,
206
+ * `NaN`, and non-finite numbers—yields `null`.
207
+ */
208
+ const formatRomanNumerals = (quantity) => {
209
+ if (typeof quantity !== "number" && typeof quantity !== "bigint" || !(quantity >= 1 && quantity < 4e3)) return null;
210
+ const qty = Number(quantity);
211
+ const digits = `${Math.floor(qty)}`.split("");
212
+ let roman = "";
213
+ let i = 3;
214
+ while (i--) roman = `${romanNumeralsByPlace[+digits.pop() + i * 10] || ""}${roman}`;
215
+ return `${Array(+digits.join("") + 1).join("M")}${roman}`;
87
216
  };
88
- var normalizeOptions = (options) => __spreadValues(__spreadValues({}, defaultOptions), typeof options === "boolean" ? { vulgarFractions: options } : options);
89
- var romanNumeralValueKey = [
90
- "",
91
- "C",
92
- "CC",
93
- "CCC",
94
- "CD",
95
- "D",
96
- "DC",
97
- "DCC",
98
- "DCCC",
99
- "CM",
100
- "",
101
- "X",
102
- "XX",
103
- "XXX",
104
- "XL",
105
- "L",
106
- "LX",
107
- "LXX",
108
- "LXXX",
109
- "XC",
110
- "",
111
- "I",
112
- "II",
113
- "III",
114
- "IV",
115
- "V",
116
- "VI",
117
- "VII",
118
- "VIII",
119
- "IX"
120
- ];
121
- var formatRomanNumerals = (qty) => {
122
- if (typeof qty !== "number" || isNaN(qty)) {
123
- return null;
124
- }
125
- if (qty < 1 || qty >= 4e3) {
126
- return "";
127
- }
128
- const floored = Math.floor(qty);
129
- const digits = `${floored}`.split("");
130
- let roman = "";
131
- let i = 3;
132
- while (i--) {
133
- roman = `${romanNumeralValueKey[+digits.pop() + i * 10] || ""}${roman}`;
134
- }
135
- return `${Array(+digits.join("") + 1).join("M")}${roman}`;
136
- };
137
- var formatQuantity = (qty, options = defaultOptions) => {
138
- const qtyAsNumber = typeof qty === "string" ? parseFloat(qty) : qty;
139
- if (isNaN(qtyAsNumber) || qtyAsNumber === null) {
140
- return null;
141
- }
142
- if (qtyAsNumber === 0) {
143
- return "";
144
- }
145
- const opts = normalizeOptions(options != null ? options : defaultOptions);
146
- if (opts.romanNumerals) {
147
- return formatRomanNumerals(qtyAsNumber);
148
- }
149
- const absoluteValue = Math.abs(qtyAsNumber);
150
- const flooredAbsVal = Math.floor(absoluteValue);
151
- const flooredAbsValStr = `${qtyAsNumber < 0 ? "-" : ""}${flooredAbsVal === 0 ? "" : `${flooredAbsVal} `}`;
152
- const decimalValue = absoluteValue - flooredAbsVal;
153
- if (decimalValue === 0) {
154
- return `${qtyAsNumber}`;
155
- }
156
- for (const [num, vf] of fractionDecimalMatches) {
157
- if (closeEnough(decimalValue, num, opts.tolerance)) {
158
- const fraction = getFraction(vf, opts);
159
- const int = fraction in vulgarToAsciiMap ? flooredAbsValStr.trim() : flooredAbsValStr;
160
- return `${int}${fraction}`;
161
- }
162
- }
163
- return `${qtyAsNumber}`;
164
- };
165
- export {
166
- defaultOptions,
167
- defaultTolerance,
168
- formatQuantity,
169
- fractionDecimalMatches,
170
- vulgarToAsciiMap
217
+ /**
218
+ * Formats a number (or string that appears to be a number)
219
+ * as one would see it written in imperial measurements, e.g.
220
+ * "1 1/2" instead of "1.5". To use vulgar fraction characters
221
+ * like "½", pass `true` as the second argument. For other options
222
+ * see {@link FormatQuantityOptions}.
223
+ */
224
+ const formatQuantity = (qty, options) => {
225
+ if (typeof qty !== "number" && typeof qty !== "string" && typeof qty !== "bigint") return null;
226
+ const opts = normalizeOptions(options);
227
+ const qtyAsNumber = typeof qty !== "number" && typeof qty !== "bigint" ? numericQuantity(qty, {
228
+ allowTrailingInvalid: opts.allowTrailingInvalid,
229
+ romanNumerals: opts.romanNumerals,
230
+ bigIntOnOverflow: true,
231
+ round: false
232
+ }) : qty;
233
+ if (typeof qtyAsNumber === "number" && isNaN(qtyAsNumber)) return null;
234
+ if (Number(qtyAsNumber) === 0) return opts.zeroFormat;
235
+ if (opts.romanNumerals) return formatRomanNumerals(qtyAsNumber);
236
+ const absoluteValue = typeof qtyAsNumber === "bigint" ? qtyAsNumber < 0n ? -qtyAsNumber : qtyAsNumber : Math.abs(qtyAsNumber);
237
+ const flooredAbsVal = typeof absoluteValue === "bigint" ? absoluteValue : Math.floor(absoluteValue);
238
+ const wholeNumberStr = `${flooredAbsVal || ""}`;
239
+ const decimalValue = typeof absoluteValue === "bigint" ? 0 : absoluteValue - flooredAbsVal;
240
+ if (decimalValue === 0) return `${qtyAsNumber}`;
241
+ let closestMatch = null;
242
+ let closestMatchDiff = Infinity;
243
+ if (opts.tolerance !== false) for (const [num, vf] of fractionDecimalMatches) {
244
+ const diff = Math.abs(decimalValue - num);
245
+ if (diff < opts.tolerance || diff === 0) {
246
+ if (diff === 0) {
247
+ closestMatch = vf;
248
+ break;
249
+ }
250
+ if (diff < closestMatchDiff) {
251
+ closestMatch = vf;
252
+ closestMatchDiff = diff;
253
+ }
254
+ }
255
+ }
256
+ if (closestMatch) {
257
+ var _opts$separator;
258
+ const fraction = getFraction(closestMatch, opts);
259
+ const isVulgar = fraction in vulgarToAsciiMap;
260
+ const sep = wholeNumberStr ? (_opts$separator = opts.separator) !== null && _opts$separator !== void 0 ? _opts$separator : isVulgar ? "" : " " : "";
261
+ return `${qtyAsNumber < 0 ? "-" : ""}${wholeNumberStr}${sep}${fraction}`;
262
+ }
263
+ return `${qtyAsNumber}`;
171
264
  };
265
+ //#endregion
266
+ export { defaultOptions, defaultTolerance, formatQuantity, formatRomanNumerals, fractionDecimalMatches, vulgarToAsciiMap };
267
+
172
268
  //# sourceMappingURL=format-quantity.legacy-esm.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import type {\n FormatQuantityOptions,\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 = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const satisfies Required<FormatQuantityOptions>;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap = {\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 satisfies Record<VulgarFraction, SimpleFraction>;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches = [\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] satisfies [number, VulgarFraction | Sixteenth][];\n","import {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\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\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 return plainFraction.replace('/', '⁄');\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): Required<FormatQuantityOptions> => ({\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) => {\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 // TODO: use numericQuantity instead of parseFloat?\n const qtyAsNumber = typeof qty === 'string' ? parseFloat(qty) : 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 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 flooredAbsValStr = `${qtyAsNumber < 0 ? '-' : ''}${\n flooredAbsVal === 0 ? '' : `${flooredAbsVal} `\n }`;\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 int =\n fraction in vulgarToAsciiMap\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAWO,IAAM,mBAAmB;AAKzB,IAAM,iBAAiB;AAAA,EAC5B,iBAAiB;AAAA,EACjB,WAAW;AAAA,EACX,eAAe;AAAA,EACf,eAAe;AACjB;AAKO,IAAM,mBAAmB;AAAA,EAC9B,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;AACP;AAMO,IAAM,yBAAyB;AAAA,EACpC,CAAC,MAAM,QAAG;AAAA,EACV,CAAC,MAAM,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AI,MAAM,EAAE,GACpD,kBAAkB,IAAI,KAAK,GAAG,aAAa,GAC7C;AACA,QAAM,eAAe,gBAAgB;AAGrC,MAAI,iBAAiB,GAAG;AACtB,WAAO,GAAG,WAAW;AAAA,EACvB;AAEA,aAAW,CAAC,KAAK,EAAE,KAAK,wBAAwB;AAC9C,QAAI,YAAY,cAAc,KAAK,KAAK,SAAS,GAAG;AAClD,YAAM,WAAW,YAAY,IAAI,IAAI;AACrC,YAAM,MACJ,YAAY,mBACR,iBAAiB,KAAK,IACtB;AACN,aAAO,GAAG,GAAG,GAAG,QAAQ;AAAA,IAC1B;AAAA,EACF;AAEA,SAAO,GAAG,WAAW;AACvB;","names":[]}
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: Readonly<ResolvedFormatQuantityOptions> = Object.freeze({\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n zeroFormat: '',\n allowTrailingInvalid: true,\n});\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap: Readonly<Record<VulgarFraction, SimpleFraction>> = Object.freeze({\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});\n\n/**\n * Map of \"close enough\" values to the {@link VulgarFraction} or {@link Sixteenth} fraction\n * string matches. The value +/- the `tolerance` option (or {@link defaultTolerance} if not\n * specified) is considered close enough to match the fraction.\n */\nexport const fractionDecimalMatches: readonly (readonly [number, VulgarFraction | Sixteenth])[] =\n Object.freeze(\n (\n [\n [1 / 16, '1/16'],\n [1 / 10, '⅒'],\n [1 / 9, '⅑'],\n [1 / 8, '⅛'],\n [1 / 7, '⅐'],\n [1 / 6, '⅙'],\n [3 / 16, '3/16'],\n [1 / 5, '⅕'],\n [1 / 4, '¼'],\n [5 / 16, '5/16'],\n [1 / 3, '⅓'],\n [3 / 8, '⅜'],\n [2 / 5, '⅖'],\n [7 / 16, '7/16'],\n [1 / 2, '½'],\n [9 / 16, '9/16'],\n [3 / 5, '⅗'],\n [5 / 8, '⅝'],\n [2 / 3, '⅔'],\n [11 / 16, '11/16'],\n [3 / 4, '¾'],\n [4 / 5, '⅘'],\n [13 / 16, '13/16'],\n [5 / 6, '⅚'],\n [7 / 8, '⅞'],\n [15 / 16, '15/16'],\n ] satisfies [number, VulgarFraction | Sixteenth][]\n ).map(entry => Object.freeze(entry))\n );\n","import { numericQuantity } from 'numeric-quantity';\nimport { defaultOptions, fractionDecimalMatches, vulgarToAsciiMap } from './constants';\nimport type {\n FormatQuantity,\n ResolvedFormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\nconst superscriptDigits = '⁰¹²³⁴⁵⁶⁷⁸⁹';\nconst subscriptDigits = '₀₁₂₃₄₅₆₇₈₉';\n\n// oxlint-disable-next-line typescript/no-misused-spread\nconst toSuperscript = (s: string) => [...s].map(c => superscriptDigits[+c]).join('');\n// oxlint-disable-next-line typescript/no-misused-spread\nconst toSubscript = (s: string) => [...s].map(c => subscriptDigits[+c]).join('');\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: ResolvedFormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ?? 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 * Only `false` (matching disabled) or a non-negative number is a valid\n * `tolerance`. Everything else—negative numbers, `NaN`, non-numbers,\n * `null`, `undefined`—resolves to {@link defaultTolerance}.\n */\nconst isValidTolerance = (tolerance: unknown): tolerance is number | false =>\n tolerance === false || (typeof tolerance === 'number' && tolerance >= 0);\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 const opts: ResolvedFormatQuantityOptions = {\n ...defaultOptions,\n ...(typeof options === 'boolean'\n ? { vulgarFractions: options }\n : typeof options === 'object' && options !== null\n ? options\n : {}),\n };\n\n if (!isValidTolerance(opts.tolerance)) {\n opts.tolerance = defaultOptions.tolerance;\n }\n\n if (typeof opts.zeroFormat !== 'string') {\n opts.zeroFormat = defaultOptions.zeroFormat;\n }\n\n if (opts.allowTrailingInvalid !== false) {\n opts.allowTrailingInvalid = true;\n }\n\n return opts;\n};\n\n// oxfmt-ignore\nconst romanNumeralsByPlace = [\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 or bigint as Roman numerals. The number must be between\n * 1 and 3999, inclusive; any other value—including non-numbers,\n * `NaN`, and non-finite numbers—yields `null`.\n */\nexport const formatRomanNumerals = (quantity: number | bigint): string | null => {\n if (\n (typeof quantity !== 'number' && typeof quantity !== 'bigint') ||\n !(quantity >= 1 && quantity < 4000)\n ) {\n return null;\n }\n\n const qty = Number(quantity);\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 = `${romanNumeralsByPlace[+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 = (qty, options) => {\n // Only numbers, bigints, and strings are accepted. Anything else (objects, booleans,\n // nullish, arrays like `[1]` that would coerce to a numeric string) is\n // rejected up front so off-type inputs behave consistently.\n if (typeof qty !== 'number' && typeof qty !== 'string' && typeof qty !== 'bigint') {\n return null;\n }\n\n const opts = normalizeOptions(options);\n\n const qtyAsNumber =\n typeof qty !== 'number' && typeof qty !== 'bigint'\n ? numericQuantity(qty, {\n allowTrailingInvalid: opts.allowTrailingInvalid,\n romanNumerals: opts.romanNumerals,\n bigIntOnOverflow: true,\n round: false,\n })\n : qty;\n\n // Return `null` if input is not number-like.\n if (typeof qtyAsNumber === 'number' && isNaN(qtyAsNumber)) {\n return null;\n }\n\n // Return empty string (or configured `zeroFormat`) if the value is zero.\n if (Number(qtyAsNumber) === 0) {\n return opts.zeroFormat;\n }\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue =\n typeof qtyAsNumber === 'bigint'\n ? qtyAsNumber < 0n\n ? -qtyAsNumber\n : qtyAsNumber\n : Math.abs(qtyAsNumber);\n const flooredAbsVal =\n typeof absoluteValue === 'bigint' ? absoluteValue : Math.floor(absoluteValue);\n const wholeNumberStr = `${flooredAbsVal || ''}`;\n const decimalValue =\n typeof absoluteValue === 'bigint' ? 0 : absoluteValue - (flooredAbsVal as number);\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n let closestMatch: VulgarFraction | Sixteenth | null = null;\n let closestMatchDiff = Infinity;\n // `tolerance: false` disables fraction matching entirely.\n if (opts.tolerance !== false) {\n for (const [num, vf] of fractionDecimalMatches) {\n const diff = Math.abs(decimalValue - num);\n // `diff === 0` keeps exact quotients matching even at `tolerance: 0`.\n if (diff < opts.tolerance || diff === 0) {\n if (diff === 0) {\n closestMatch = vf;\n break;\n }\n if (diff < closestMatchDiff) {\n closestMatch = vf;\n closestMatchDiff = diff;\n }\n }\n }\n }\n\n if (closestMatch) {\n const fraction = getFraction(closestMatch, opts);\n const isVulgar = fraction in vulgarToAsciiMap;\n const sep = wholeNumberStr ? (opts.separator ?? (isVulgar ? '' : ' ')) : '';\n return `${qtyAsNumber < 0 ? '-' : ''}${wholeNumberStr}${sep}${fraction}`;\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":";;;;;;AAWA,MAAa,mBAAmB;;;;AAKhC,MAAa,iBAA0D,OAAO,OAAO;CACnF,iBAAiB;CACjB,WAAW;CACX,eAAe;CACf,eAAe;CACf,YAAY;CACZ,sBAAsB;AACxB,CAAC;;;;AAKD,MAAa,mBAAqE,OAAO,OAAO;CAC9F,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;AACP,CAAC;;;;;;AAOD,MAAa,yBACX,OAAO,OAEH;CACE,CAAC,IAAI,IAAI,MAAM;CACf,CAAC,IAAI,IAAI,GAAG;CACZ,CAAC,IAAI,GAAG,GAAG;CACX,CAAC,IAAI,GAAG,GAAG;CACX,CAAC,IAAI,GAAG,GAAG;CACX,CAAC,IAAI,GAAG,GAAG;CACX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