format-quantity 3.1.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.
- package/README.md +63 -18
- package/dist/cjs/format-quantity.cjs.development.d.ts +90 -66
- package/dist/cjs/format-quantity.cjs.development.js +99 -80
- package/dist/cjs/format-quantity.cjs.development.js.map +1 -1
- package/dist/cjs/format-quantity.cjs.production.d.ts +90 -66
- package/dist/cjs/format-quantity.cjs.production.js +1 -1
- package/dist/cjs/format-quantity.cjs.production.js.map +1 -1
- package/dist/format-quantity.d.mts +90 -66
- package/dist/format-quantity.legacy-esm.d.ts +90 -66
- package/dist/format-quantity.legacy-esm.js +100 -86
- package/dist/format-quantity.legacy-esm.js.map +1 -1
- package/dist/format-quantity.mjs +98 -79
- package/dist/format-quantity.mjs.map +1 -1
- package/dist/format-quantity.production.d.mts +90 -66
- package/dist/format-quantity.production.mjs +1 -1
- package/dist/format-quantity.production.mjs.map +1 -1
- package/dist/format-quantity.umd.min.js +3 -0
- package/dist/format-quantity.umd.min.js.map +1 -0
- package/package.json +20 -19
- package/dist/format-quantity.iife.umd.min.js +0 -2
- package/dist/format-quantity.iife.umd.min.js.map +0 -1
|
@@ -1,107 +1,131 @@
|
|
|
1
1
|
//#region src/types.d.ts
|
|
2
2
|
interface FormatQuantityOptions {
|
|
3
3
|
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
* Output vulgar fractions, like "½" instead of "1/2", when appropriate.
|
|
5
|
+
* Overrides the `fractionSlash` option.
|
|
6
|
+
*
|
|
7
|
+
* @default false
|
|
8
|
+
*/
|
|
7
9
|
vulgarFractions?: boolean;
|
|
8
10
|
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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;
|
|
18
26
|
/**
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
+
*/
|
|
23
33
|
fractionSlash?: boolean;
|
|
24
34
|
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
+
*/
|
|
29
41
|
romanNumerals?: boolean;
|
|
30
42
|
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
+
*/
|
|
36
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;
|
|
37
61
|
}
|
|
38
62
|
/**
|
|
39
|
-
* {@link FormatQuantityOptions} with all properties resolved to their
|
|
40
|
-
* default values, except {@link FormatQuantityOptions.separator | separator}
|
|
41
|
-
* which remains optional so that unset vs explicitly-set can be distinguished.
|
|
42
|
-
*/
|
|
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
|
+
*/
|
|
43
67
|
type ResolvedFormatQuantityOptions = Required<Omit<FormatQuantityOptions, "separator">> & Pick<FormatQuantityOptions, "separator">;
|
|
44
68
|
/**
|
|
45
|
-
* Function signature of {@link formatQuantity}.
|
|
46
|
-
*/
|
|
69
|
+
* Function signature of {@link formatQuantity}.
|
|
70
|
+
*/
|
|
47
71
|
interface FormatQuantity {
|
|
48
|
-
(qty: string | number, options?: boolean | FormatQuantityOptions): string | null;
|
|
72
|
+
(qty: string | number | bigint, options?: boolean | FormatQuantityOptions): string | null;
|
|
49
73
|
}
|
|
50
74
|
/** Any numeric character. */
|
|
51
75
|
type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
|
|
52
76
|
/** Any numeric character except '0'. */
|
|
53
77
|
type NonZeroDigit = Exclude<Digit, "0">;
|
|
54
78
|
/**
|
|
55
|
-
* Fraction string with either one or two numeric characters in both the
|
|
56
|
-
* numerator and denominator (but not two characters in the numerator while
|
|
57
|
-
* the denominator only has one).
|
|
58
|
-
*/
|
|
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
|
+
*/
|
|
59
83
|
type SimpleFraction = `${NonZeroDigit}/${NonZeroDigit}` | `${NonZeroDigit}/${NonZeroDigit}${Digit}` | `${NonZeroDigit}${Digit}/${NonZeroDigit}${Digit}`;
|
|
60
84
|
/**
|
|
61
|
-
* Odd numerator sixteenth fraction strings.
|
|
62
|
-
*/
|
|
85
|
+
* Odd numerator sixteenth fraction strings.
|
|
86
|
+
*/
|
|
63
87
|
type Sixteenth = `${"1" | "3" | "5" | "7" | "9" | "11" | "13" | "15"}/16`;
|
|
64
88
|
/**
|
|
65
|
-
* Unicode vulgar fraction code points.
|
|
66
|
-
*/
|
|
89
|
+
* Unicode vulgar fraction code points.
|
|
90
|
+
*/
|
|
67
91
|
type VulgarFraction = "¼" | "½" | "¾" | "⅐" | "⅑" | "⅒" | "⅓" | "⅔" | "⅕" | "⅖" | "⅗" | "⅘" | "⅙" | "⅚" | "⅛" | "⅜" | "⅝" | "⅞";
|
|
68
|
-
/** @hidden */
|
|
69
|
-
type FormatQuantityTests = Record<string, ([Parameters<FormatQuantity>[0], ReturnType<FormatQuantity>] | [Parameters<FormatQuantity>[0], ReturnType<FormatQuantity>, Parameters<FormatQuantity>[1]])[]>;
|
|
70
92
|
//#endregion
|
|
71
93
|
//#region src/constants.d.ts
|
|
72
94
|
/**
|
|
73
|
-
* Default tolerance used by {@link formatQuantity} when determining if a number
|
|
74
|
-
* is close enough to a fraction value to be considered equivalent.
|
|
75
|
-
*/
|
|
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
|
+
*/
|
|
76
98
|
declare const defaultTolerance: 0.0075;
|
|
77
99
|
/**
|
|
78
|
-
* Default options for {@link formatQuantity}.
|
|
79
|
-
*/
|
|
80
|
-
declare const defaultOptions: ResolvedFormatQuantityOptions
|
|
100
|
+
* Default options for {@link formatQuantity}.
|
|
101
|
+
*/
|
|
102
|
+
declare const defaultOptions: Readonly<ResolvedFormatQuantityOptions>;
|
|
81
103
|
/**
|
|
82
|
-
* Map of vulgar fractions to their traditional ASCII equivalents.
|
|
83
|
-
*/
|
|
84
|
-
declare const vulgarToAsciiMap: Record<VulgarFraction, SimpleFraction
|
|
104
|
+
* Map of vulgar fractions to their traditional ASCII equivalents.
|
|
105
|
+
*/
|
|
106
|
+
declare const vulgarToAsciiMap: Readonly<Record<VulgarFraction, SimpleFraction>>;
|
|
85
107
|
/**
|
|
86
|
-
* Map of "close enough"
|
|
87
|
-
* {@link
|
|
88
|
-
|
|
89
|
-
|
|
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])[];
|
|
90
113
|
//#endregion
|
|
91
114
|
//#region src/formatQuantity.d.ts
|
|
92
115
|
/**
|
|
93
|
-
* Formats a number as Roman numerals. The number must be between
|
|
94
|
-
* 1 and 3999, inclusive
|
|
95
|
-
|
|
96
|
-
|
|
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;
|
|
97
121
|
/**
|
|
98
|
-
* Formats a number (or string that appears to be a number)
|
|
99
|
-
* as one would see it written in imperial measurements, e.g.
|
|
100
|
-
* "1 1/2" instead of "1.5". To use vulgar fraction characters
|
|
101
|
-
* like "½", pass `true` as the second argument. For other options
|
|
102
|
-
* see {@link FormatQuantityOptions}.
|
|
103
|
-
*/
|
|
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
|
+
*/
|
|
104
128
|
declare const formatQuantity: FormatQuantity;
|
|
105
129
|
//#endregion
|
|
106
|
-
export { Digit, FormatQuantity, FormatQuantityOptions,
|
|
130
|
+
export { Digit, FormatQuantity, FormatQuantityOptions, NonZeroDigit, ResolvedFormatQuantityOptions, SimpleFraction, Sixteenth, VulgarFraction, defaultOptions, defaultTolerance, formatQuantity, formatRomanNumerals, fractionDecimalMatches, vulgarToAsciiMap };
|
|
107
131
|
//# sourceMappingURL=format-quantity.legacy-esm.d.ts.map
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { numericQuantity } from "numeric-quantity";
|
|
2
|
-
|
|
3
2
|
//#region src/constants.ts
|
|
4
3
|
/**
|
|
5
4
|
* Default tolerance used by {@link formatQuantity} when determining if a number
|
|
@@ -9,16 +8,18 @@ const defaultTolerance = .0075;
|
|
|
9
8
|
/**
|
|
10
9
|
* Default options for {@link formatQuantity}.
|
|
11
10
|
*/
|
|
12
|
-
const defaultOptions = {
|
|
11
|
+
const defaultOptions = Object.freeze({
|
|
13
12
|
vulgarFractions: false,
|
|
14
13
|
tolerance: defaultTolerance,
|
|
15
14
|
fractionSlash: false,
|
|
16
|
-
romanNumerals: false
|
|
17
|
-
|
|
15
|
+
romanNumerals: false,
|
|
16
|
+
zeroFormat: "",
|
|
17
|
+
allowTrailingInvalid: true
|
|
18
|
+
});
|
|
18
19
|
/**
|
|
19
20
|
* Map of vulgar fractions to their traditional ASCII equivalents.
|
|
20
21
|
*/
|
|
21
|
-
const vulgarToAsciiMap = {
|
|
22
|
+
const vulgarToAsciiMap = Object.freeze({
|
|
22
23
|
"¼": "1/4",
|
|
23
24
|
"½": "1/2",
|
|
24
25
|
"¾": "3/4",
|
|
@@ -37,42 +38,42 @@ const vulgarToAsciiMap = {
|
|
|
37
38
|
"⅜": "3/8",
|
|
38
39
|
"⅝": "5/8",
|
|
39
40
|
"⅞": "7/8"
|
|
40
|
-
};
|
|
41
|
+
});
|
|
41
42
|
/**
|
|
42
|
-
* Map of "close enough"
|
|
43
|
-
* {@link
|
|
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.
|
|
44
46
|
*/
|
|
45
|
-
const fractionDecimalMatches = [
|
|
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
|
-
];
|
|
73
|
-
|
|
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)));
|
|
74
75
|
//#endregion
|
|
75
|
-
//#region \0@oxc-project+runtime@0.
|
|
76
|
+
//#region \0@oxc-project+runtime@0.147.0/helpers/esm/typeof.js
|
|
76
77
|
function _typeof(o) {
|
|
77
78
|
"@babel/helpers - typeof";
|
|
78
79
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -81,9 +82,8 @@ function _typeof(o) {
|
|
|
81
82
|
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
82
83
|
}, _typeof(o);
|
|
83
84
|
}
|
|
84
|
-
|
|
85
85
|
//#endregion
|
|
86
|
-
//#region \0@oxc-project+runtime@0.
|
|
86
|
+
//#region \0@oxc-project+runtime@0.147.0/helpers/esm/toPrimitive.js
|
|
87
87
|
function toPrimitive(t, r) {
|
|
88
88
|
if ("object" != _typeof(t) || !t) return t;
|
|
89
89
|
var e = t[Symbol.toPrimitive];
|
|
@@ -94,16 +94,14 @@ function toPrimitive(t, r) {
|
|
|
94
94
|
}
|
|
95
95
|
return ("string" === r ? String : Number)(t);
|
|
96
96
|
}
|
|
97
|
-
|
|
98
97
|
//#endregion
|
|
99
|
-
//#region \0@oxc-project+runtime@0.
|
|
98
|
+
//#region \0@oxc-project+runtime@0.147.0/helpers/esm/toPropertyKey.js
|
|
100
99
|
function toPropertyKey(t) {
|
|
101
100
|
var i = toPrimitive(t, "string");
|
|
102
101
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
103
102
|
}
|
|
104
|
-
|
|
105
103
|
//#endregion
|
|
106
|
-
//#region \0@oxc-project+runtime@0.
|
|
104
|
+
//#region \0@oxc-project+runtime@0.147.0/helpers/esm/defineProperty.js
|
|
107
105
|
function _defineProperty(e, r, t) {
|
|
108
106
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
109
107
|
value: t,
|
|
@@ -112,9 +110,8 @@ function _defineProperty(e, r, t) {
|
|
|
112
110
|
writable: !0
|
|
113
111
|
}) : e[r] = t, e;
|
|
114
112
|
}
|
|
115
|
-
|
|
116
113
|
//#endregion
|
|
117
|
-
//#region \0@oxc-project+runtime@0.
|
|
114
|
+
//#region \0@oxc-project+runtime@0.147.0/helpers/esm/objectSpread2.js
|
|
118
115
|
function ownKeys(e, r) {
|
|
119
116
|
var t = Object.keys(e);
|
|
120
117
|
if (Object.getOwnPropertySymbols) {
|
|
@@ -136,26 +133,12 @@ function _objectSpread2(e) {
|
|
|
136
133
|
}
|
|
137
134
|
return e;
|
|
138
135
|
}
|
|
139
|
-
|
|
140
136
|
//#endregion
|
|
141
137
|
//#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
138
|
const superscriptDigits = "⁰¹²³⁴⁵⁶⁷⁸⁹";
|
|
148
139
|
const subscriptDigits = "₀₁₂₃₄₅₆₇₈₉";
|
|
149
|
-
const toSuperscript = (s) =>
|
|
150
|
-
|
|
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;
|
|
158
|
-
};
|
|
140
|
+
const toSuperscript = (s) => [...s].map((c) => superscriptDigits[+c]).join("");
|
|
141
|
+
const toSubscript = (s) => [...s].map((c) => subscriptDigits[+c]).join("");
|
|
159
142
|
/**
|
|
160
143
|
* Applies the `vulgarFractions` or `fractionSlash` options as necessary.
|
|
161
144
|
*/
|
|
@@ -170,10 +153,22 @@ const getFraction = (vulgarFractionOrSixteenth, { fractionSlash, vulgarFractions
|
|
|
170
153
|
return plainFraction;
|
|
171
154
|
};
|
|
172
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
|
+
/**
|
|
173
162
|
* Merges options object with default options, converting boolean to object if necessary.
|
|
174
163
|
*/
|
|
175
|
-
const normalizeOptions = (options) =>
|
|
176
|
-
const
|
|
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;
|
|
170
|
+
};
|
|
171
|
+
const romanNumeralsByPlace = [
|
|
177
172
|
"",
|
|
178
173
|
"C",
|
|
179
174
|
"CC",
|
|
@@ -206,16 +201,17 @@ const romanNumeralValueKey = [
|
|
|
206
201
|
"IX"
|
|
207
202
|
];
|
|
208
203
|
/**
|
|
209
|
-
* Formats a number as Roman numerals. The number must be between
|
|
210
|
-
* 1 and 3999, inclusive
|
|
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`.
|
|
211
207
|
*/
|
|
212
|
-
const formatRomanNumerals = (
|
|
213
|
-
if (typeof
|
|
214
|
-
|
|
208
|
+
const formatRomanNumerals = (quantity) => {
|
|
209
|
+
if (typeof quantity !== "number" && typeof quantity !== "bigint" || !(quantity >= 1 && quantity < 4e3)) return null;
|
|
210
|
+
const qty = Number(quantity);
|
|
215
211
|
const digits = `${Math.floor(qty)}`.split("");
|
|
216
212
|
let roman = "";
|
|
217
213
|
let i = 3;
|
|
218
|
-
while (i--) roman = `${
|
|
214
|
+
while (i--) roman = `${romanNumeralsByPlace[+digits.pop() + i * 10] || ""}${roman}`;
|
|
219
215
|
return `${Array(+digits.join("") + 1).join("M")}${roman}`;
|
|
220
216
|
};
|
|
221
217
|
/**
|
|
@@ -225,30 +221,48 @@ const formatRomanNumerals = (qty) => {
|
|
|
225
221
|
* like "½", pass `true` as the second argument. For other options
|
|
226
222
|
* see {@link FormatQuantityOptions}.
|
|
227
223
|
*/
|
|
228
|
-
const formatQuantity = (qty, options
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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
232
|
}) : qty;
|
|
233
|
-
if (
|
|
234
|
-
if (qtyAsNumber === 0) return
|
|
235
|
-
const opts = normalizeOptions(options !== null && options !== void 0 ? options : defaultOptions);
|
|
233
|
+
if (typeof qtyAsNumber === "number" && isNaN(qtyAsNumber)) return null;
|
|
234
|
+
if (Number(qtyAsNumber) === 0) return opts.zeroFormat;
|
|
236
235
|
if (opts.romanNumerals) return formatRomanNumerals(qtyAsNumber);
|
|
237
|
-
const absoluteValue = Math.abs(qtyAsNumber);
|
|
238
|
-
const flooredAbsVal = Math.floor(absoluteValue);
|
|
239
|
-
const
|
|
240
|
-
const
|
|
241
|
-
const decimalValue = absoluteValue - flooredAbsVal;
|
|
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;
|
|
242
240
|
if (decimalValue === 0) return `${qtyAsNumber}`;
|
|
243
|
-
|
|
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) {
|
|
244
257
|
var _opts$separator;
|
|
245
|
-
const fraction = getFraction(
|
|
258
|
+
const fraction = getFraction(closestMatch, opts);
|
|
246
259
|
const isVulgar = fraction in vulgarToAsciiMap;
|
|
247
|
-
|
|
260
|
+
const sep = wholeNumberStr ? (_opts$separator = opts.separator) !== null && _opts$separator !== void 0 ? _opts$separator : isVulgar ? "" : " " : "";
|
|
261
|
+
return `${qtyAsNumber < 0 ? "-" : ""}${wholeNumberStr}${sep}${fraction}`;
|
|
248
262
|
}
|
|
249
263
|
return `${qtyAsNumber}`;
|
|
250
264
|
};
|
|
251
|
-
|
|
252
265
|
//#endregion
|
|
253
266
|
export { defaultOptions, defaultTolerance, formatQuantity, formatRomanNumerals, fractionDecimalMatches, vulgarToAsciiMap };
|
|
267
|
+
|
|
254
268
|
//# sourceMappingURL=format-quantity.legacy-esm.js.map
|
|
@@ -1 +1 @@
|
|
|
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}${wholeStr}${sep}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":";;;;;;;AAWA,MAAa,mBAAmB;;;;AAKhC,MAAa,iBAAgD;CAC3D,iBAAiB;CACjB,WAAW;CACX,eAAe;CACf,eAAe;CAChB;;;;AAKD,MAAa,mBAA2D;CACtE,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;CACN;;;;;AAMD,MAAa,yBAAiE;CAC5E,CAAC,KAAM,IAAI;CACX,CAAC,KAAM,IAAI;CACX,CAAC,IAAK,IAAI;CACV,CAAC,IAAK,IAAI;CACV,CA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GAAG;AAGZ,MAAK,MAAM,CAAC,KAAK,OAAO,uBACtB,KAAI,YAAY,cAAc,KAAK,KAAK,UAAU,EAAE;;EAClD,MAAM,WAAW,YAAY,IAAI,KAAK;EACtC,MAAM,WAAW,YAAY;AAI7B,SAAO,GAAG,OAAO,WAHL,8BACP,KAAK,sEAAc,WAAW,KAAK,MACpC,KAC8B;;AAItC,QAAO,GAAG"}
|
|
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,CAAC,IAAI,IAAI,MAAM;CACf,CAAC,IAAI,GAAG,GAAG;CACX,CAAC,IAAI,GAAG,GAAG;CACX,CAAC,IAAI,IAAI,MAAM;CACf,CAAC,IAAI,GAAG,GAAG;CACX,CAAC,IAAI,GAAG,GAAG;C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