format-quantity 3.0.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 CHANGED
@@ -1,8 +1,8 @@
1
1
  [![npm][badge-npm]](https://www.npmjs.com/package/format-quantity)
2
2
  ![workflow status](https://github.com/jakeboone02/format-quantity/actions/workflows/main.yml/badge.svg)
3
3
  [![codecov.io](https://codecov.io/github/jakeboone02/format-quantity/coverage.svg?branch=main)](https://codecov.io/github/jakeboone02/format-quantity?branch=main)
4
- [![downloads](https://img.shields.io/npm/dm/format-quantity.svg)](http://npm-stat.com/charts.html?package=format-quantity&from=2015-08-01)
5
- [![MIT License](https://img.shields.io/npm/l/format-quantity.svg)](http://opensource.org/licenses/MIT)
4
+ [![downloads](https://img.shields.io/npm/dm/format-quantity.svg)](https://npm-stat.com/charts.html?package=format-quantity&from=2015-08-01)
5
+ [![MIT License](https://img.shields.io/npm/l/format-quantity.svg)](https://opensource.org/licenses/MIT)
6
6
 
7
7
  Formats a number (or string that appears to be a number) as one would see it written in imperial measurements, e.g. "1 1/2" instead of "1.5".
8
8
 
@@ -11,7 +11,8 @@ Formats a number (or string that appears to be a number) as one would see it wri
11
11
  Features:
12
12
 
13
13
  - To use vulgar fraction characters like "⅞", pass `true` as the second argument. Other options like Roman numerals are described below.
14
- - The return value will be `null` if the first argument is neither a number nor a string that evaluates to a number using `parseFloat`.
14
+ - String inputs are parsed with [`numeric-quantity`](https://www.npmjs.com/package/numeric-quantity), so mixed numbers (`"1 1/2"`), vulgar fractions (`"½"`), bare fractions (`"1/3"`), and comma/underscore-separated numbers (`"1,000"`) are all accepted in addition to plain decimal strings.
15
+ - The return value will be `null` if the first argument is not a recognized numeric format.
15
16
  - The return value will be an empty string (`""`) if the first argument is `0` or `"0"`, which fits the primary use case of formatting recipe ingredient quantities.
16
17
 
17
18
  > _For the inverse operation—converting a string to a `number`—check out [numeric-quantity](https://www.npmjs.com/package/numeric-quantity). It handles mixed numbers, vulgar fractions, comma/underscore separators, and Roman numerals._
@@ -77,13 +78,27 @@ Note: `formatQuantity` supports sixteenths, but no vulgar fraction characters ex
77
78
  | --------- | ------: |
78
79
  | `boolean` | `false` |
79
80
 
80
- Uses the [fraction slash character](<https://en.wikipedia.org/wiki/Slash_(punctuation)#Fractions>) (`"\u2044"`) to separate the numerator and denominator instead of the regular "solidus" slash (`"\u002f"`). This option is ignored if the `vulgarFractions` option is also `true`.
81
+ Uses the [fraction slash character](<https://en.wikipedia.org/wiki/Slash_(punctuation)#Fractions>) (`"\u2044"`) to separate the numerator and denominator instead of the regular "solidus" slash (`"\u002f"`), with Unicode superscript numerator and subscript denominator digits. This option is ignored if the `vulgarFractions` option is also `true`.
81
82
 
82
83
  ```js
83
- formatQuantity(3.875, { fractionSlash: true }); // "3 7⁄8"
84
+ formatQuantity(3.875, { fractionSlash: true }); // "3 ⁷⁄₈"
84
85
  formatQuantity(3.875, { fractionSlash: true, vulgarFractions: true }); // "3⅞"
85
86
  ```
86
87
 
88
+ ### `separator`
89
+
90
+ | Type | Default |
91
+ | -------- | ------: |
92
+ | `string` | N/A |
93
+
94
+ Overrides the string placed between the whole number and the fraction. When not specified, the default is `" "` (a space) for ASCII and fraction-slash fractions, and `""` (no space) for vulgar fractions. Common alternatives include a hyphen (`"-"`) and a no-break space (`"\u00a0"`).
95
+
96
+ ```js
97
+ formatQuantity(1.5, { separator: '-' }); // "1-1/2"
98
+ formatQuantity(1.5, { separator: ' ', vulgarFractions: true }); // "1 ½"
99
+ formatQuantity(1.5, { separator: '\u00a0' }); // "1\u00a01/2" (no-break space)
100
+ ```
101
+
87
102
  ### `tolerance`
88
103
 
89
104
  | Type | Default |
@@ -1,113 +1,107 @@
1
+ //#region src/types.d.ts
1
2
  interface FormatQuantityOptions {
2
- /**
3
- * Output vulgar fractions, like "½" instead of "1/2", when appropriate.
4
- * Overrides the `fractionSlash` option.
5
- */
6
- vulgarFractions?: boolean;
7
- /**
8
- * Amount by which a number can deviate from the calculated quotient to be
9
- * considered a match. For example, 0.66 is close enough to 2 ÷ 3 (which
10
- * is 0.66666... repeating) to be considered equivalent so the function
11
- * will return "2/3". The smaller this number, the higher the likelihood that
12
- * the function will return a decimal instead of a fraction or mixed number.
13
- *
14
- * @default 0.0075
15
- */
16
- tolerance?: number;
17
- /**
18
- * Output the fraction slash character (⁄) instead of the "solidus"
19
- * slash (/) for fractions. Results appear like "1⁄2" instead of "1/2".
20
- * Overridden by the `vulgarFractions` option.
21
- */
22
- fractionSlash?: boolean;
23
- /**
24
- * Output in Roman numerals. Provided value must be between 1 and 3999, inclusive.
25
- * Decimal values will be ignored (`Math.floor` is used to remove them). Overrides
26
- * all other options.
27
- */
28
- romanNumerals?: boolean;
3
+ /**
4
+ * Output vulgar fractions, like "½" instead of "1/2", when appropriate.
5
+ * Overrides the `fractionSlash` option.
6
+ */
7
+ vulgarFractions?: boolean;
8
+ /**
9
+ * Amount by which a number can deviate from the calculated quotient to be
10
+ * considered a match. For example, 0.66 is close enough to 2 ÷ 3 (which
11
+ * is 0.66666... repeating) to be considered equivalent so the function
12
+ * will return "2/3". The smaller this number, the higher the likelihood that
13
+ * the function will return a decimal instead of a fraction or mixed number.
14
+ *
15
+ * @default 0.0075
16
+ */
17
+ tolerance?: number;
18
+ /**
19
+ * Output the fraction slash character (⁄) instead of the "solidus"
20
+ * slash (/) for fractions. Results appear like "1⁄2" instead of "1/2".
21
+ * Overridden by the `vulgarFractions` option.
22
+ */
23
+ fractionSlash?: boolean;
24
+ /**
25
+ * Output in Roman numerals. Provided value must be between 1 and 3999, inclusive.
26
+ * Decimal values will be ignored (`Math.floor` is used to remove them). Overrides
27
+ * all other options.
28
+ */
29
+ romanNumerals?: boolean;
30
+ /**
31
+ * String to place between the whole number and fraction parts. When not specified,
32
+ * defaults to `" "` for ASCII and fraction-slash fractions, and `""` for vulgar
33
+ * fractions (preserving the standard typographic convention of no space before
34
+ * vulgar fraction characters).
35
+ */
36
+ separator?: string;
29
37
  }
30
38
  /**
31
- * Function signature of {@link formatQuantity}.
32
- */
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
+ */
43
+ type ResolvedFormatQuantityOptions = Required<Omit<FormatQuantityOptions, "separator">> & Pick<FormatQuantityOptions, "separator">;
44
+ /**
45
+ * Function signature of {@link formatQuantity}.
46
+ */
33
47
  interface FormatQuantity {
34
- (qty: string | number, options?: boolean | FormatQuantityOptions): string | null;
48
+ (qty: string | number, options?: boolean | FormatQuantityOptions): string | null;
35
49
  }
36
50
  /** Any numeric character. */
37
- type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
51
+ type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
38
52
  /** Any numeric character except '0'. */
39
- type NonZeroDigit = Exclude<Digit, '0'>;
53
+ type NonZeroDigit = Exclude<Digit, "0">;
40
54
  /**
41
- * Fraction string with either one or two numeric characters in both the
42
- * numerator and denominator (but not two characters in the numerator while
43
- * the denominator only has one).
44
- */
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
+ */
45
59
  type SimpleFraction = `${NonZeroDigit}/${NonZeroDigit}` | `${NonZeroDigit}/${NonZeroDigit}${Digit}` | `${NonZeroDigit}${Digit}/${NonZeroDigit}${Digit}`;
46
60
  /**
47
- * Odd numerator sixteenth fraction strings.
48
- */
49
- type Sixteenth = `${'1' | '3' | '5' | '7' | '9' | '11' | '13' | '15'}/16`;
61
+ * Odd numerator sixteenth fraction strings.
62
+ */
63
+ type Sixteenth = `${"1" | "3" | "5" | "7" | "9" | "11" | "13" | "15"}/16`;
50
64
  /**
51
- * Unicode vulgar fraction code points.
52
- */
53
- type VulgarFraction = '¼' | '½' | '¾' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '';
65
+ * Unicode vulgar fraction code points.
66
+ */
67
+ type VulgarFraction = "¼" | "½" | "¾" | "" | "" | "" | "" | "" | "" | "" | "" | "" | "" | "" | "" | "" | "" | "";
54
68
  /** @hidden */
55
- type FormatQuantityTests = Record<string, ([Parameters<FormatQuantity>[0], ReturnType<FormatQuantity>] | [
56
- Parameters<FormatQuantity>[0],
57
- ReturnType<FormatQuantity>,
58
- Parameters<FormatQuantity>[1]
59
- ])[]>;
60
-
61
- /**
62
- * Formats a number (or string that appears to be a number)
63
- * as one would see it written in imperial measurements, e.g.
64
- * "1 1/2" instead of "1.5". To use vulgar fraction characters
65
- * like "½", pass `true` as the second argument. For other options
66
- * see {@link FormatQuantityOptions}.
67
- */
68
- declare const formatQuantity: FormatQuantity;
69
-
69
+ type FormatQuantityTests = Record<string, ([Parameters<FormatQuantity>[0], ReturnType<FormatQuantity>] | [Parameters<FormatQuantity>[0], ReturnType<FormatQuantity>, Parameters<FormatQuantity>[1]])[]>;
70
+ //#endregion
71
+ //#region src/constants.d.ts
70
72
  /**
71
- * Default tolerance used by {@link formatQuantity} when determining if a number
72
- * is close enough to a fraction value to be considered equivalent.
73
- */
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
+ */
74
76
  declare const defaultTolerance: 0.0075;
75
77
  /**
76
- * Default options for {@link formatQuantity}.
77
- */
78
- declare const defaultOptions: {
79
- readonly vulgarFractions: false;
80
- readonly tolerance: 0.0075;
81
- readonly fractionSlash: false;
82
- readonly romanNumerals: false;
83
- };
78
+ * Default options for {@link formatQuantity}.
79
+ */
80
+ declare const defaultOptions: ResolvedFormatQuantityOptions;
84
81
  /**
85
- * Map of vulgar fractions to their traditional ASCII equivalents.
86
- */
87
- declare const vulgarToAsciiMap: {
88
- readonly '\u00BC': "1/4";
89
- readonly '\u00BD': "1/2";
90
- readonly '\u00BE': "3/4";
91
- readonly '\u2150': "1/7";
92
- readonly '\u2151': "1/9";
93
- readonly '\u2152': "1/10";
94
- readonly '\u2153': "1/3";
95
- readonly '\u2154': "2/3";
96
- readonly '\u2155': "1/5";
97
- readonly '\u2156': "2/5";
98
- readonly '\u2157': "3/5";
99
- readonly '\u2158': "4/5";
100
- readonly '\u2159': "1/6";
101
- readonly '\u215A': "5/6";
102
- readonly '\u215B': "1/8";
103
- readonly '\u215C': "3/8";
104
- readonly '\u215D': "5/8";
105
- readonly '\u215E': "7/8";
106
- };
82
+ * Map of vulgar fractions to their traditional ASCII equivalents.
83
+ */
84
+ declare const vulgarToAsciiMap: Record<VulgarFraction, SimpleFraction>;
107
85
  /**
108
- * Map of "close enough" decimal values to the {@link VulgarFraction} or
109
- * {@link Sixteenth} fraction string matches.
110
- */
111
- declare const fractionDecimalMatches: ([number, "⅓"] | [number, "⅔"] | [number, "⅕"] | [number, "⅖"] | [number, "⅗"] | [number, "⅘"] | [number, "⅙"] | [number, "⅚"] | [number, "⅐"] | [number, "⅑"] | [number, "⅒"] | [number, "⅛"] | [number, "¼"] | [number, "⅜"] | [number, "½"] | [number, "⅝"] | [number, "¾"] | [number, "⅞"] | [number, "1/16"] | [number, "3/16"] | [number, "5/16"] | [number, "7/16"] | [number, "9/16"] | [number, "11/16"] | [number, "13/16"] | [number, "15/16"])[];
112
-
113
- export { type Digit, type FormatQuantity, type FormatQuantityOptions, type FormatQuantityTests, type NonZeroDigit, type SimpleFraction, type Sixteenth, type VulgarFraction, defaultOptions, defaultTolerance, formatQuantity, fractionDecimalMatches, vulgarToAsciiMap };
86
+ * Map of "close enough" decimal values to the {@link VulgarFraction} or
87
+ * {@link Sixteenth} fraction string matches.
88
+ */
89
+ declare const fractionDecimalMatches: [number, VulgarFraction | Sixteenth][];
90
+ //#endregion
91
+ //#region src/formatQuantity.d.ts
92
+ /**
93
+ * Formats a number as Roman numerals. The number must be between
94
+ * 1 and 3999, inclusive.
95
+ */
96
+ declare const formatRomanNumerals: (qty: number) => string | null;
97
+ /**
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
+ */
104
+ declare const formatQuantity: FormatQuantity;
105
+ //#endregion
106
+ export { Digit, FormatQuantity, FormatQuantityOptions, FormatQuantityTests, NonZeroDigit, ResolvedFormatQuantityOptions, SimpleFraction, Sixteenth, VulgarFraction, defaultOptions, defaultTolerance, formatQuantity, formatRomanNumerals, fractionDecimalMatches, vulgarToAsciiMap };
107
+ //# sourceMappingURL=format-quantity.cjs.development.d.ts.map
@@ -1,188 +1,195 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- defaultOptions: () => defaultOptions,
24
- defaultTolerance: () => defaultTolerance,
25
- formatQuantity: () => formatQuantity,
26
- fractionDecimalMatches: () => fractionDecimalMatches,
27
- vulgarToAsciiMap: () => vulgarToAsciiMap
28
- });
29
- module.exports = __toCommonJS(src_exports);
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let numeric_quantity = require("numeric-quantity");
30
3
 
31
- // src/constants.ts
32
- var defaultTolerance = 75e-4;
33
- var defaultOptions = {
34
- vulgarFractions: false,
35
- tolerance: defaultTolerance,
36
- fractionSlash: false,
37
- romanNumerals: false
4
+ //#region src/constants.ts
5
+ /**
6
+ * Default tolerance used by {@link formatQuantity} when determining if a number
7
+ * is close enough to a fraction value to be considered equivalent.
8
+ */
9
+ const defaultTolerance = .0075;
10
+ /**
11
+ * Default options for {@link formatQuantity}.
12
+ */
13
+ const defaultOptions = {
14
+ vulgarFractions: false,
15
+ tolerance: defaultTolerance,
16
+ fractionSlash: false,
17
+ romanNumerals: false
38
18
  };
39
- var vulgarToAsciiMap = {
40
- "\xBC": "1/4",
41
- "\xBD": "1/2",
42
- "\xBE": "3/4",
43
- "\u2150": "1/7",
44
- "\u2151": "1/9",
45
- "\u2152": "1/10",
46
- "\u2153": "1/3",
47
- "\u2154": "2/3",
48
- "\u2155": "1/5",
49
- "\u2156": "2/5",
50
- "\u2157": "3/5",
51
- "\u2158": "4/5",
52
- "\u2159": "1/6",
53
- "\u215A": "5/6",
54
- "\u215B": "1/8",
55
- "\u215C": "3/8",
56
- "\u215D": "5/8",
57
- "\u215E": "7/8"
19
+ /**
20
+ * Map of vulgar fractions to their traditional ASCII equivalents.
21
+ */
22
+ const vulgarToAsciiMap = {
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"
58
41
  };
59
- var fractionDecimalMatches = [
60
- [0.33, "\u2153"],
61
- [0.66, "\u2154"],
62
- [0.2, "\u2155"],
63
- [0.4, "\u2156"],
64
- [0.6, "\u2157"],
65
- [0.8, "\u2158"],
66
- [0.166, "\u2159"],
67
- [0.833, "\u215A"],
68
- [0.143, "\u2150"],
69
- [0.111, "\u2151"],
70
- [0.1, "\u2152"],
71
- [0.125, "\u215B"],
72
- [0.25, "\xBC"],
73
- [0.375, "\u215C"],
74
- [0.5, "\xBD"],
75
- [0.625, "\u215D"],
76
- [0.75, "\xBE"],
77
- [0.875, "\u215E"],
78
- [0.0625, "1/16"],
79
- [0.1875, "3/16"],
80
- [0.3125, "5/16"],
81
- [0.4375, "7/16"],
82
- [0.5625, "9/16"],
83
- [0.6875, "11/16"],
84
- [0.8125, "13/16"],
85
- [0.9375, "15/16"]
42
+ /**
43
+ * Map of "close enough" decimal values to the {@link VulgarFraction} or
44
+ * {@link Sixteenth} fraction string matches.
45
+ */
46
+ const fractionDecimalMatches = [
47
+ [.33, ""],
48
+ [.66, ""],
49
+ [.2, ""],
50
+ [.4, ""],
51
+ [.6, ""],
52
+ [.8, ""],
53
+ [.166, ""],
54
+ [.833, ""],
55
+ [.143, ""],
56
+ [.111, ""],
57
+ [.1, ""],
58
+ [.125, ""],
59
+ [.25, "¼"],
60
+ [.375, ""],
61
+ [.5, "½"],
62
+ [.625, ""],
63
+ [.75, "¾"],
64
+ [.875, ""],
65
+ [.0625, "1/16"],
66
+ [.1875, "3/16"],
67
+ [.3125, "5/16"],
68
+ [.4375, "7/16"],
69
+ [.5625, "9/16"],
70
+ [.6875, "11/16"],
71
+ [.8125, "13/16"],
72
+ [.9375, "15/16"]
86
73
  ];
87
74
 
88
- // src/formatQuantity.ts
89
- var closeEnough = (n1, n2, tolerance) => Math.abs(n1 - n2) < tolerance;
90
- var getFraction = (vulgarFractionOrSixteenth, { fractionSlash, vulgarFractions }) => {
91
- if (vulgarFractions) {
92
- return vulgarFractionOrSixteenth;
93
- }
94
- const plainFraction = vulgarToAsciiMap[vulgarFractionOrSixteenth] ?? vulgarFractionOrSixteenth;
95
- if (fractionSlash) {
96
- return plainFraction.replace("/", "\u2044");
97
- }
98
- return plainFraction;
75
+ //#endregion
76
+ //#region src/formatQuantity.ts
77
+ /**
78
+ * Determines if two numbers are close enough to consider
79
+ * them equal for the purposes of this package.
80
+ */
81
+ const closeEnough = (n1, n2, tolerance) => Math.abs(n1 - n2) < tolerance;
82
+ const superscriptDigits = "⁰¹²³⁴⁵⁶⁷⁸⁹";
83
+ const subscriptDigits = "₀₁₂₃₄₅₆₇₈₉";
84
+ const toSuperscript = (s) => {
85
+ let r = "";
86
+ for (let i = 0; i < s.length; i++) r += superscriptDigits[+s[i]];
87
+ return r;
99
88
  };
100
- var normalizeOptions = (options) => ({
101
- ...defaultOptions,
102
- ...typeof options === "boolean" ? { vulgarFractions: options } : options
89
+ const toSubscript = (s) => {
90
+ let r = "";
91
+ for (let i = 0; i < s.length; i++) r += subscriptDigits[+s[i]];
92
+ return r;
93
+ };
94
+ /**
95
+ * Applies the `vulgarFractions` or `fractionSlash` options as necessary.
96
+ */
97
+ const getFraction = (vulgarFractionOrSixteenth, { fractionSlash, vulgarFractions }) => {
98
+ if (vulgarFractions) return vulgarFractionOrSixteenth;
99
+ const plainFraction = vulgarToAsciiMap[vulgarFractionOrSixteenth] ?? vulgarFractionOrSixteenth;
100
+ if (fractionSlash) {
101
+ const [num, den] = plainFraction.split("/");
102
+ return `${toSuperscript(num)}⁄${toSubscript(den)}`;
103
+ }
104
+ return plainFraction;
105
+ };
106
+ /**
107
+ * Merges options object with default options, converting boolean to object if necessary.
108
+ */
109
+ const normalizeOptions = (options) => ({
110
+ ...defaultOptions,
111
+ ...typeof options === "boolean" ? { vulgarFractions: options } : options
103
112
  });
104
- var romanNumeralValueKey = [
105
- "",
106
- "C",
107
- "CC",
108
- "CCC",
109
- "CD",
110
- "D",
111
- "DC",
112
- "DCC",
113
- "DCCC",
114
- "CM",
115
- "",
116
- "X",
117
- "XX",
118
- "XXX",
119
- "XL",
120
- "L",
121
- "LX",
122
- "LXX",
123
- "LXXX",
124
- "XC",
125
- "",
126
- "I",
127
- "II",
128
- "III",
129
- "IV",
130
- "V",
131
- "VI",
132
- "VII",
133
- "VIII",
134
- "IX"
113
+ const romanNumeralValueKey = [
114
+ "",
115
+ "C",
116
+ "CC",
117
+ "CCC",
118
+ "CD",
119
+ "D",
120
+ "DC",
121
+ "DCC",
122
+ "DCCC",
123
+ "CM",
124
+ "",
125
+ "X",
126
+ "XX",
127
+ "XXX",
128
+ "XL",
129
+ "L",
130
+ "LX",
131
+ "LXX",
132
+ "LXXX",
133
+ "XC",
134
+ "",
135
+ "I",
136
+ "II",
137
+ "III",
138
+ "IV",
139
+ "V",
140
+ "VI",
141
+ "VII",
142
+ "VIII",
143
+ "IX"
135
144
  ];
136
- var formatRomanNumerals = (qty) => {
137
- if (typeof qty !== "number" || isNaN(qty)) {
138
- return null;
139
- }
140
- if (qty < 1 || qty >= 4e3) {
141
- return "";
142
- }
143
- const floored = Math.floor(qty);
144
- const digits = `${floored}`.split("");
145
- let roman = "";
146
- let i = 3;
147
- while (i--) {
148
- roman = `${romanNumeralValueKey[+digits.pop() + i * 10] || ""}${roman}`;
149
- }
150
- return `${Array(+digits.join("") + 1).join("M")}${roman}`;
145
+ /**
146
+ * Formats a number as Roman numerals. The number must be between
147
+ * 1 and 3999, inclusive.
148
+ */
149
+ const formatRomanNumerals = (qty) => {
150
+ if (typeof qty !== "number" || isNaN(qty)) return null;
151
+ if (qty < 1 || qty >= 4e3) return "";
152
+ const digits = `${Math.floor(qty)}`.split("");
153
+ let roman = "";
154
+ let i = 3;
155
+ while (i--) roman = `${romanNumeralValueKey[+digits.pop() + i * 10] || ""}${roman}`;
156
+ return `${Array(+digits.join("") + 1).join("M")}${roman}`;
151
157
  };
152
- var formatQuantity = (qty, options = defaultOptions) => {
153
- const qtyAsNumber = typeof qty === "string" ? parseFloat(qty) : qty;
154
- if (isNaN(qtyAsNumber) || qtyAsNumber === null) {
155
- return null;
156
- }
157
- if (qtyAsNumber === 0) {
158
- return "";
159
- }
160
- const opts = normalizeOptions(options ?? defaultOptions);
161
- if (opts.romanNumerals) {
162
- return formatRomanNumerals(qtyAsNumber);
163
- }
164
- const absoluteValue = Math.abs(qtyAsNumber);
165
- const flooredAbsVal = Math.floor(absoluteValue);
166
- const flooredAbsValStr = `${qtyAsNumber < 0 ? "-" : ""}${flooredAbsVal === 0 ? "" : `${flooredAbsVal} `}`;
167
- const decimalValue = absoluteValue - flooredAbsVal;
168
- if (decimalValue === 0) {
169
- return `${qtyAsNumber}`;
170
- }
171
- for (const [num, vf] of fractionDecimalMatches) {
172
- if (closeEnough(decimalValue, num, opts.tolerance)) {
173
- const fraction = getFraction(vf, opts);
174
- const int = fraction in vulgarToAsciiMap ? flooredAbsValStr.trim() : flooredAbsValStr;
175
- return `${int}${fraction}`;
176
- }
177
- }
178
- return `${qtyAsNumber}`;
158
+ /**
159
+ * Formats a number (or string that appears to be a number)
160
+ * as one would see it written in imperial measurements, e.g.
161
+ * "1 1/2" instead of "1.5". To use vulgar fraction characters
162
+ * like "½", pass `true` as the second argument. For other options
163
+ * see {@link FormatQuantityOptions}.
164
+ */
165
+ const formatQuantity = (qty, options = defaultOptions) => {
166
+ const qtyAsNumber = typeof qty === "string" ? (0, numeric_quantity.numericQuantity)(qty, {
167
+ round: false,
168
+ allowTrailingInvalid: true
169
+ }) : qty;
170
+ if (isNaN(qtyAsNumber) || qtyAsNumber === null) return null;
171
+ if (qtyAsNumber === 0) return "";
172
+ const opts = normalizeOptions(options ?? defaultOptions);
173
+ if (opts.romanNumerals) return formatRomanNumerals(qtyAsNumber);
174
+ const absoluteValue = Math.abs(qtyAsNumber);
175
+ const flooredAbsVal = Math.floor(absoluteValue);
176
+ const sign = qtyAsNumber < 0 ? "-" : "";
177
+ const wholeStr = flooredAbsVal === 0 ? "" : `${flooredAbsVal}`;
178
+ const decimalValue = absoluteValue - flooredAbsVal;
179
+ if (decimalValue === 0) return `${qtyAsNumber}`;
180
+ for (const [num, vf] of fractionDecimalMatches) if (closeEnough(decimalValue, num, opts.tolerance)) {
181
+ const fraction = getFraction(vf, opts);
182
+ const isVulgar = fraction in vulgarToAsciiMap;
183
+ return `${sign}${wholeStr}${wholeStr ? opts.separator ?? (isVulgar ? "" : " ") : ""}${fraction}`;
184
+ }
185
+ return `${qtyAsNumber}`;
179
186
  };
180
- // Annotate the CommonJS export names for ESM import in node:
181
- 0 && (module.exports = {
182
- defaultOptions,
183
- defaultTolerance,
184
- formatQuantity,
185
- fractionDecimalMatches,
186
- vulgarToAsciiMap
187
- });
187
+
188
+ //#endregion
189
+ exports.defaultOptions = defaultOptions;
190
+ exports.defaultTolerance = defaultTolerance;
191
+ exports.formatQuantity = formatQuantity;
192
+ exports.formatRomanNumerals = formatRomanNumerals;
193
+ exports.fractionDecimalMatches = fractionDecimalMatches;
194
+ exports.vulgarToAsciiMap = vulgarToAsciiMap;
188
195
  //# sourceMappingURL=format-quantity.cjs.development.js.map