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 +20 -5
- package/dist/cjs/format-quantity.cjs.development.d.ts +90 -96
- package/dist/cjs/format-quantity.cjs.development.js +184 -177
- package/dist/cjs/format-quantity.cjs.development.js.map +1 -1
- package/dist/cjs/format-quantity.cjs.production.d.ts +90 -96
- package/dist/cjs/format-quantity.cjs.production.js +1 -1
- package/dist/cjs/format-quantity.cjs.production.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/format-quantity.d.mts +90 -96
- package/dist/format-quantity.iife.umd.min.js +2 -0
- package/dist/format-quantity.iife.umd.min.js.map +1 -0
- package/dist/format-quantity.legacy-esm.d.ts +107 -0
- package/dist/format-quantity.legacy-esm.js +244 -162
- package/dist/format-quantity.legacy-esm.js.map +1 -1
- package/dist/format-quantity.mjs +179 -147
- package/dist/format-quantity.mjs.map +1 -1
- package/dist/format-quantity.production.d.mts +90 -96
- package/dist/format-quantity.production.mjs +1 -1
- package/dist/format-quantity.production.mjs.map +1 -1
- package/package.json +25 -19
- package/dist/format-quantity.legacy-esm.d.mts +0 -113
- package/dist/format-quantity.umd.min.js +0 -2
- package/dist/format-quantity.umd.min.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
[![npm][badge-npm]](https://www.npmjs.com/package/format-quantity)
|
|
2
2
|

|
|
3
3
|
[](https://codecov.io/github/jakeboone02/format-quantity?branch=main)
|
|
4
|
-
[](
|
|
5
|
-
[](
|
|
4
|
+
[](https://npm-stat.com/charts.html?package=format-quantity&from=2015-08-01)
|
|
5
|
+
[](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
|
-
-
|
|
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
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
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
|
-
|
|
48
|
+
(qty: string | number, options?: boolean | FormatQuantityOptions): string | null;
|
|
35
49
|
}
|
|
36
50
|
/** Any numeric character. */
|
|
37
|
-
type Digit =
|
|
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,
|
|
53
|
+
type NonZeroDigit = Exclude<Digit, "0">;
|
|
40
54
|
/**
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
type Sixteenth = `${
|
|
61
|
+
* Odd numerator sixteenth fraction strings.
|
|
62
|
+
*/
|
|
63
|
+
type Sixteenth = `${"1" | "3" | "5" | "7" | "9" | "11" | "13" | "15"}/16`;
|
|
50
64
|
/**
|
|
51
|
-
|
|
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
|
-
|
|
57
|
-
|
|
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
|
-
|
|
72
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
declare const fractionDecimalMatches:
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
2
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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
|