format-quantity 2.0.1 → 2.1.0-beta.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 +68 -41
- package/dist/cjs/format-quantity.cjs.development.js +192 -0
- package/dist/cjs/format-quantity.cjs.development.js.map +1 -0
- package/dist/cjs/format-quantity.cjs.production.js +2 -0
- package/dist/cjs/format-quantity.cjs.production.js.map +1 -0
- package/dist/cjs/index.js +6 -0
- package/dist/format-quantity.d.ts +83 -0
- package/dist/format-quantity.legacy-esm.js +176 -0
- package/dist/format-quantity.legacy-esm.js.map +1 -0
- package/dist/format-quantity.mjs +161 -0
- package/dist/format-quantity.mjs.map +1 -0
- package/dist/format-quantity.production.mjs +2 -0
- package/dist/format-quantity.production.mjs.map +1 -0
- package/dist/format-quantity.umd.min.js +2 -0
- package/dist/format-quantity.umd.min.js.map +1 -0
- package/package.json +33 -23
- package/dist/constants.d.ts +0 -12
- package/dist/format-quantity.cjs.js +0 -2
- package/dist/format-quantity.cjs.js.map +0 -1
- package/dist/format-quantity.es.js +0 -100
- package/dist/format-quantity.es.js.map +0 -1
- package/dist/format-quantity.umd.js +0 -2
- package/dist/format-quantity.umd.js.map +0 -1
- package/dist/formatQuantity.d.ts +0 -9
- package/dist/index.d.ts +0 -5
- package/dist/types.d.ts +0 -36
package/README.md
CHANGED
|
@@ -1,50 +1,56 @@
|
|
|
1
1
|
# format-quantity
|
|
2
2
|
|
|
3
|
-
[
|
|
4
|
+

|
|
5
|
+
[](https://codecov.io/github/jakeboone02/format-quantity?branch=main)
|
|
6
6
|
[](http://npm-stat.com/charts.html?package=format-quantity&from=2015-08-01)
|
|
7
7
|
[](http://opensource.org/licenses/MIT)
|
|
8
8
|
|
|
9
|
-
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".
|
|
9
|
+
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".
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Features:
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
- To use vulgar fraction characters like "⅞", pass `true` as the second argument (see other [options](#options), like Roman numerals, 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`.
|
|
15
|
+
- The return value will be an empty string (`""`) if the first argument is `0` or `"0"`, which is done to fit the primary use case of formatting recipe ingredient quantities.
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
> _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._
|
|
18
|
+
>
|
|
19
|
+
> _If you're interested in parsing recipe ingredient strings, try [parse-ingredient](https://www.npmjs.com/package/parse-ingredient)._
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Installed
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import { formatQuantity } from 'format-quantity';
|
|
20
27
|
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
formatQuantity(1.5); // "1 1/2"
|
|
29
|
+
formatQuantity(2.66); // "2 2/3"
|
|
30
|
+
formatQuantity(3.875, true); // "3⅞"
|
|
23
31
|
```
|
|
24
32
|
|
|
25
|
-
###
|
|
33
|
+
### CDN
|
|
26
34
|
|
|
27
|
-
|
|
35
|
+
As an ES module:
|
|
28
36
|
|
|
29
37
|
```html
|
|
30
|
-
<script
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
<script type="module">
|
|
39
|
+
import { formatQuantity } from 'https://cdn.jsdelivr.net/npm/format-quantity/+esm';
|
|
40
|
+
|
|
41
|
+
console.log(formatQuantity(10.5)); // "10 1/2"
|
|
33
42
|
</script>
|
|
34
43
|
```
|
|
35
44
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
```js
|
|
39
|
-
import { formatQuantity } from 'format-quantity';
|
|
45
|
+
As UMD (all exports are properties of the global object `FormatQuantity`):
|
|
40
46
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
47
|
+
```html
|
|
48
|
+
<script src="https://unpkg.com/format-quantity"></script>
|
|
49
|
+
<script>
|
|
50
|
+
console.log(FormatQuantity.formatQuantity(10.5)); // "10 1/2"
|
|
51
|
+
</script>
|
|
44
52
|
```
|
|
45
53
|
|
|
46
|
-
The return value will be `null` if the provided argument is not a number or a string that evaluates to a number using `parseFloat`. The return value will be an empty string (`""`) if the provided argument is `0` or `"0"` (this is done to fit the primary use case of recipe ingredient quantities).
|
|
47
|
-
|
|
48
54
|
## Options
|
|
49
55
|
|
|
50
56
|
The second parameter to `formatQuantity` can be a `boolean` value or an options object.
|
|
@@ -65,44 +71,65 @@ formatQuantity(3.875, true); // "3⅞"
|
|
|
65
71
|
|
|
66
72
|
Note: `formatQuantity` supports sixteenths, but no vulgar fraction characters exist for that denomination. Therefore the `vulgarFractions` option has no effect if the fraction portion of the final string is an odd numerator over a denominator of `16`.
|
|
67
73
|
|
|
74
|
+
### `fractionSlash`
|
|
75
|
+
|
|
76
|
+
| Type | Default |
|
|
77
|
+
| --------- | ------: |
|
|
78
|
+
| `boolean` | `false` |
|
|
79
|
+
|
|
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
|
+
|
|
82
|
+
```js
|
|
83
|
+
formatQuantity(3.875, { fractionSlash: true }); // "3 7⁄8"
|
|
84
|
+
formatQuantity(3.875, { fractionSlash: true, vulgarFractions: true }); // "3⅞"
|
|
85
|
+
```
|
|
86
|
+
|
|
68
87
|
### `tolerance`
|
|
69
88
|
|
|
70
89
|
| Type | Default |
|
|
71
90
|
| -------- | -------: |
|
|
72
91
|
| `number` | `0.0075` |
|
|
73
92
|
|
|
74
|
-
This option determines how close the decimal portion of a number has to be to the actual quotient of a fraction to be considered a match. For example, consider the fraction 1⁄3:
|
|
93
|
+
This option determines how close the decimal portion of a number has to be to the actual quotient of a fraction to be considered a match. For example, consider the fraction 1⁄3: $1 \div 3 = 0.\overline{333}$, repeating forever. The number `0.333` (exactly 333 thousandths) is not equivalent to 1⁄3, but it's very close. So even though $0.333 \neq 1 \div 3$, both `formatQuantity(0.333)` and `formatQuantity(1/3)` will return `"1/3"`.
|
|
75
94
|
|
|
76
|
-
A lower tolerance increases the likelihood that `formatQuantity` will return a decimal representation instead of a fraction or mixed number since the matching algorithm will be stricter. An
|
|
95
|
+
A lower tolerance increases the likelihood that `formatQuantity` will return a decimal representation instead of a fraction or mixed number since the matching algorithm will be stricter. An higher tolerance increases the likelihood that `formatQuantity` will return a fraction or mixed number, but at the risk of arbitrarily matching an incorrect fraction simply because it gets evaluated first (the export `fractionDecimalMatches` defines the order of evaluation).
|
|
77
96
|
|
|
78
97
|
```js
|
|
79
98
|
// Low tolerance - returns a decimal since 0.333 is not close enough to 1/3
|
|
80
99
|
formatQuantity(0.333, { tolerance: 0.00001 }); // "0.333"
|
|
81
|
-
// High tolerance - matches "1/3" even for
|
|
100
|
+
// High tolerance - matches "1/3" even for 3/10
|
|
82
101
|
formatQuantity(0.3, { tolerance: 0.1 }); // "1/3"
|
|
83
|
-
// Way too high tolerance - incorrect result because thirds get evaluated before halves
|
|
102
|
+
// *Way* too high tolerance - incorrect result because thirds get evaluated before halves
|
|
84
103
|
formatQuantity(0.5, { tolerance: 0.5 }); // "1/3"
|
|
85
104
|
```
|
|
86
105
|
|
|
87
|
-
### `
|
|
106
|
+
### `romanNumerals`
|
|
88
107
|
|
|
89
108
|
| Type | Default |
|
|
90
109
|
| --------- | ------: |
|
|
91
110
|
| `boolean` | `false` |
|
|
92
111
|
|
|
93
|
-
|
|
112
|
+
Coerces the number into an integer using `Math.floor`, then formats the value as Roman numerals. The algorithm uses strict, modern rules, so the number must be between 1 and 3999 (inclusive).
|
|
113
|
+
|
|
114
|
+
When this option is `true`, all other options are ignored.
|
|
94
115
|
|
|
95
116
|
```js
|
|
96
|
-
formatQuantity(
|
|
97
|
-
formatQuantity(
|
|
117
|
+
formatQuantity(1214, { romanNumerals: true }); // "MCCXIV"
|
|
118
|
+
formatQuantity(12.14, { romanNumerals: true, vulgarFractions: true }); // "XII"
|
|
98
119
|
```
|
|
99
120
|
|
|
100
121
|
## Other exports
|
|
101
122
|
|
|
102
|
-
| Name | Type
|
|
103
|
-
| ------------------------ |
|
|
104
|
-
| `defaultTolerance` | `number`
|
|
105
|
-
| `
|
|
106
|
-
| `
|
|
107
|
-
| `
|
|
108
|
-
| `
|
|
123
|
+
| Name | Type | Description |
|
|
124
|
+
| ------------------------ | ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
125
|
+
| `defaultTolerance` | `number` | `0.0075` |
|
|
126
|
+
| `defaultOptions` | `type` | Object representing the default options |
|
|
127
|
+
| `fractionDecimalMatches` | <code>[number, VulgarFraction \| Sixteenth][]</code> | List of decimal values that are close enough to match the associated fraction (inputs are evaluated against the decimal values in the order of this array) |
|
|
128
|
+
| `vulgarToAsciiMap` | `object` | Map of vulgar fraction characters to their equivalent ASCII strings (`"⅓"` to `"1/3"`, `"⅞"` to `"7/8"`, etc.) |
|
|
129
|
+
| `formatRomanNumerals` | `function` | Formats a number as Roman numerals (used internally by `formatQuantity` when the `romanNumerals` option is `true`) |
|
|
130
|
+
| `FormatQuantityOptions` | `interface` | Shape of `formatQuantity`'s second parameter (if not a `boolean` value) |
|
|
131
|
+
| `SimpleFraction` | `type` | String template type for valid (positive, no division by zero) ASCII fraction strings with either one or two digits in the numerator and denominator each |
|
|
132
|
+
| `VulgarFraction` | `type` | The set of [vulgar fraction characters](https://en.wikipedia.org/wiki/Number_Forms) (`"\u00bc"`, `"\u00bd"`, `"\u00be"`, and `"\u2150"` through `"\u215e"`) |
|
|
133
|
+
| `Sixteenth` | `type` | Union type of all ASCII representations of odd-numbered sixteenth fractions less than one, (`"1/16"`, `"3/16"`, etc.) |
|
|
134
|
+
|
|
135
|
+
[badge-npm]: https://img.shields.io/npm/v/numeric-quantity.svg?cacheSeconds=3600&logo=npm
|
|
@@ -0,0 +1,192 @@
|
|
|
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
|
+
default: () => src_default,
|
|
24
|
+
defaultOptions: () => defaultOptions,
|
|
25
|
+
defaultTolerance: () => defaultTolerance,
|
|
26
|
+
formatQuantity: () => formatQuantity,
|
|
27
|
+
fractionDecimalMatches: () => fractionDecimalMatches,
|
|
28
|
+
vulgarToAsciiMap: () => vulgarToAsciiMap
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(src_exports);
|
|
31
|
+
|
|
32
|
+
// src/constants.ts
|
|
33
|
+
var defaultTolerance = 75e-4;
|
|
34
|
+
var defaultOptions = {
|
|
35
|
+
vulgarFractions: false,
|
|
36
|
+
tolerance: defaultTolerance,
|
|
37
|
+
fractionSlash: false,
|
|
38
|
+
romanNumerals: false
|
|
39
|
+
};
|
|
40
|
+
var vulgarToAsciiMap = {
|
|
41
|
+
"\xBC": "1/4",
|
|
42
|
+
"\xBD": "1/2",
|
|
43
|
+
"\xBE": "3/4",
|
|
44
|
+
"\u2150": "1/7",
|
|
45
|
+
"\u2151": "1/9",
|
|
46
|
+
"\u2152": "1/10",
|
|
47
|
+
"\u2153": "1/3",
|
|
48
|
+
"\u2154": "2/3",
|
|
49
|
+
"\u2155": "1/5",
|
|
50
|
+
"\u2156": "2/5",
|
|
51
|
+
"\u2157": "3/5",
|
|
52
|
+
"\u2158": "4/5",
|
|
53
|
+
"\u2159": "1/6",
|
|
54
|
+
"\u215A": "5/6",
|
|
55
|
+
"\u215B": "1/8",
|
|
56
|
+
"\u215C": "3/8",
|
|
57
|
+
"\u215D": "5/8",
|
|
58
|
+
"\u215E": "7/8"
|
|
59
|
+
};
|
|
60
|
+
var fractionDecimalMatches = [
|
|
61
|
+
[0.33, "\u2153"],
|
|
62
|
+
[0.66, "\u2154"],
|
|
63
|
+
[0.2, "\u2155"],
|
|
64
|
+
[0.4, "\u2156"],
|
|
65
|
+
[0.6, "\u2157"],
|
|
66
|
+
[0.8, "\u2158"],
|
|
67
|
+
[0.166, "\u2159"],
|
|
68
|
+
[0.833, "\u215A"],
|
|
69
|
+
[0.143, "\u2150"],
|
|
70
|
+
[0.111, "\u2151"],
|
|
71
|
+
[0.1, "\u2152"],
|
|
72
|
+
[0.125, "\u215B"],
|
|
73
|
+
[0.25, "\xBC"],
|
|
74
|
+
[0.375, "\u215C"],
|
|
75
|
+
[0.5, "\xBD"],
|
|
76
|
+
[0.625, "\u215D"],
|
|
77
|
+
[0.75, "\xBE"],
|
|
78
|
+
[0.875, "\u215E"],
|
|
79
|
+
[0.0625, "1/16"],
|
|
80
|
+
[0.1875, "3/16"],
|
|
81
|
+
[0.3125, "5/16"],
|
|
82
|
+
[0.4375, "7/16"],
|
|
83
|
+
[0.5625, "9/16"],
|
|
84
|
+
[0.6875, "11/16"],
|
|
85
|
+
[0.8125, "13/16"],
|
|
86
|
+
[0.9375, "15/16"]
|
|
87
|
+
];
|
|
88
|
+
|
|
89
|
+
// src/formatQuantity.ts
|
|
90
|
+
var closeEnough = (n1, n2, tolerance) => Math.abs(n1 - n2) < tolerance;
|
|
91
|
+
var getFraction = (vulgarFractionOrSixteenth, { fractionSlash, vulgarFractions }) => {
|
|
92
|
+
if (vulgarFractions) {
|
|
93
|
+
return vulgarFractionOrSixteenth;
|
|
94
|
+
}
|
|
95
|
+
const plainFraction = vulgarToAsciiMap[vulgarFractionOrSixteenth] ?? vulgarFractionOrSixteenth;
|
|
96
|
+
if (fractionSlash) {
|
|
97
|
+
return plainFraction.replace("/", "\u2044");
|
|
98
|
+
}
|
|
99
|
+
return plainFraction;
|
|
100
|
+
};
|
|
101
|
+
var normalizeOptions = (options) => ({
|
|
102
|
+
...defaultOptions,
|
|
103
|
+
...typeof options === "boolean" ? { vulgarFractions: options } : options
|
|
104
|
+
});
|
|
105
|
+
var romanNumeralValueKey = [
|
|
106
|
+
"",
|
|
107
|
+
"C",
|
|
108
|
+
"CC",
|
|
109
|
+
"CCC",
|
|
110
|
+
"CD",
|
|
111
|
+
"D",
|
|
112
|
+
"DC",
|
|
113
|
+
"DCC",
|
|
114
|
+
"DCCC",
|
|
115
|
+
"CM",
|
|
116
|
+
"",
|
|
117
|
+
"X",
|
|
118
|
+
"XX",
|
|
119
|
+
"XXX",
|
|
120
|
+
"XL",
|
|
121
|
+
"L",
|
|
122
|
+
"LX",
|
|
123
|
+
"LXX",
|
|
124
|
+
"LXXX",
|
|
125
|
+
"XC",
|
|
126
|
+
"",
|
|
127
|
+
"I",
|
|
128
|
+
"II",
|
|
129
|
+
"III",
|
|
130
|
+
"IV",
|
|
131
|
+
"V",
|
|
132
|
+
"VI",
|
|
133
|
+
"VII",
|
|
134
|
+
"VIII",
|
|
135
|
+
"IX"
|
|
136
|
+
];
|
|
137
|
+
var formatRomanNumerals = (qty) => {
|
|
138
|
+
if (typeof qty !== "number" || isNaN(qty)) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
if (qty < 1 || qty >= 4e3) {
|
|
142
|
+
return "";
|
|
143
|
+
}
|
|
144
|
+
const floored = Math.floor(qty);
|
|
145
|
+
const digits = `${floored}`.split("");
|
|
146
|
+
let roman = "";
|
|
147
|
+
let i = 3;
|
|
148
|
+
while (i--) {
|
|
149
|
+
roman = `${romanNumeralValueKey[+digits.pop() + i * 10] || ""}${roman}`;
|
|
150
|
+
}
|
|
151
|
+
return `${Array(+digits.join("") + 1).join("M")}${roman}`;
|
|
152
|
+
};
|
|
153
|
+
var formatQuantity = (qty, options = defaultOptions) => {
|
|
154
|
+
const qtyAsNumber = typeof qty === "string" ? parseFloat(qty) : qty;
|
|
155
|
+
if (isNaN(qtyAsNumber) || qtyAsNumber === null) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
if (qtyAsNumber === 0) {
|
|
159
|
+
return "";
|
|
160
|
+
}
|
|
161
|
+
const opts = normalizeOptions(options ?? defaultOptions);
|
|
162
|
+
if (opts.romanNumerals) {
|
|
163
|
+
return formatRomanNumerals(qtyAsNumber);
|
|
164
|
+
}
|
|
165
|
+
const absoluteValue = Math.abs(qtyAsNumber);
|
|
166
|
+
const flooredAbsVal = Math.floor(absoluteValue);
|
|
167
|
+
const flooredAbsValStr = `${qtyAsNumber < 0 ? "-" : ""}${flooredAbsVal === 0 ? "" : `${flooredAbsVal} `}`;
|
|
168
|
+
const decimalValue = absoluteValue - flooredAbsVal;
|
|
169
|
+
if (decimalValue === 0) {
|
|
170
|
+
return `${qtyAsNumber}`;
|
|
171
|
+
}
|
|
172
|
+
for (const [num, vf] of fractionDecimalMatches) {
|
|
173
|
+
if (closeEnough(decimalValue, num, opts.tolerance)) {
|
|
174
|
+
const fraction = getFraction(vf, opts);
|
|
175
|
+
const int = Object.hasOwn(vulgarToAsciiMap, fraction) ? flooredAbsValStr.trim() : flooredAbsValStr;
|
|
176
|
+
return `${int}${fraction}`;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return `${qtyAsNumber}`;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// src/index.ts
|
|
183
|
+
var src_default = formatQuantity;
|
|
184
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
185
|
+
0 && (module.exports = {
|
|
186
|
+
defaultOptions,
|
|
187
|
+
defaultTolerance,
|
|
188
|
+
formatQuantity,
|
|
189
|
+
fractionDecimalMatches,
|
|
190
|
+
vulgarToAsciiMap
|
|
191
|
+
});
|
|
192
|
+
//# sourceMappingURL=format-quantity.cjs.development.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/constants.ts","../../src/formatQuantity.ts"],"sourcesContent":["import { formatQuantity } from './formatQuantity';\nexport * from './constants';\nexport * from './types';\nexport { formatQuantity };\nexport default formatQuantity;\n","import type {\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\nexport const defaultTolerance = 0.0075 as const;\n\nexport const defaultOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} satisfies Required<FormatQuantityOptions>;\n\n/**\n * A map of vulgar or simple sixteenth fractions to their traditional ASCII\n * equivalents. Sixteenths map to themselves.\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} satisfies Record<VulgarFraction, SimpleFraction>;\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\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\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 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 the [documentation](https://jakeboone02.github.io/format-quantity/).\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 = Object.hasOwn(vulgarToAsciiMap, fraction)\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOO,IAAM,mBAAmB;AAEzB,IAAM,iBAAiB;AAAA,EAC5B,iBAAiB;AAAA,EACjB,WAAW;AAAA,EACX,eAAe;AAAA,EACf,eAAe;AACjB;AAMO,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;AAEO,IAAM,yBAAyB;AAAA,EACpC,CAAC,MAAM,QAAG;AAAA,EACV,CAAC,MAAM,QAAG;AAAA,EACV,CAAC,KAAK,QAAG;AAAA,EACT,CAAC,KAAK,QAAG;AAAA,EACT,CAAC,KAAK,QAAG;AAAA,EACT,CAAC,KAAK,QAAG;AAAA,EACT,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,KAAK,QAAG;AAAA,EACT,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,MAAM,MAAG;AAAA,EACV,CAAC,OAAO,QAAG;AAAA,EACX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SAAO,GAAG;AACZ;;;AFjIA,IAAO,cAAQ;","names":[]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var l=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var d=(t,o)=>{for(var r in o)l(t,r,{get:o[r],enumerable:!0})},y=(t,o,r,a)=>{if(o&&typeof o=="object"||typeof o=="function")for(let e of X(o))!b.call(t,e)&&e!==r&&l(t,e,{get:()=>o[e],enumerable:!(a=C(o,e))||a.enumerable});return t};var I=t=>y(l({},"__esModule",{value:!0}),t);var N={};d(N,{default:()=>Q,defaultOptions:()=>n,defaultTolerance:()=>D,formatQuantity:()=>c,fractionDecimalMatches:()=>u,vulgarToAsciiMap:()=>i});module.exports=I(N);var D=.0075,n={vulgarFractions:!1,tolerance:.0075,fractionSlash:!1,romanNumerals:!1},i={"\xBC":"1/4","\xBD":"1/2","\xBE":"3/4","\u2150":"1/7","\u2151":"1/9","\u2152":"1/10","\u2153":"1/3","\u2154":"2/3","\u2155":"1/5","\u2156":"2/5","\u2157":"3/5","\u2158":"4/5","\u2159":"1/6","\u215A":"5/6","\u215B":"1/8","\u215C":"3/8","\u215D":"5/8","\u215E":"7/8"},u=[[.33,"\u2153"],[.66,"\u2154"],[.2,"\u2155"],[.4,"\u2156"],[.6,"\u2157"],[.8,"\u2158"],[.166,"\u2159"],[.833,"\u215A"],[.143,"\u2150"],[.111,"\u2151"],[.1,"\u2152"],[.125,"\u215B"],[.25,"\xBC"],[.375,"\u215C"],[.5,"\xBD"],[.625,"\u215D"],[.75,"\xBE"],[.875,"\u215E"],[.0625,"1/16"],[.1875,"3/16"],[.3125,"5/16"],[.4375,"7/16"],[.5625,"9/16"],[.6875,"11/16"],[.8125,"13/16"],[.9375,"15/16"]];var g=(t,o,r)=>Math.abs(t-o)<r,h=(t,{fractionSlash:o,vulgarFractions:r})=>{if(r)return t;let a=i[t]??t;return o?a.replace("/","\u2044"):a},x=t=>({...n,...typeof t=="boolean"?{vulgarFractions:t}:t}),$=["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","","I","II","III","IV","V","VI","VII","VIII","IX"],M=t=>{if(typeof t!="number"||isNaN(t))return null;if(t<1||t>=4e3)return"";let r=`${Math.floor(t)}`.split(""),a="",e=3;for(;e--;)a=`${$[+r.pop()+e*10]||""}${a}`;return`${Array(+r.join("")+1).join("M")}${a}`},c=(t,o=n)=>{let r=typeof t=="string"?parseFloat(t):t;if(isNaN(r)||r===null)return null;if(r===0)return"";let a=x(o??n);if(a.romanNumerals)return M(r);let e=Math.abs(r),s=Math.floor(e),m=`${r<0?"-":""}${s===0?"":`${s} `}`,f=e-s;if(f===0)return`${r}`;for(let[F,V]of u)if(g(f,F,a.tolerance)){let p=h(V,a);return`${Object.hasOwn(i,p)?m.trim():m}${p}`}return`${r}`};var Q=c;0&&(module.exports={defaultOptions,defaultTolerance,formatQuantity,fractionDecimalMatches,vulgarToAsciiMap});
|
|
2
|
+
//# sourceMappingURL=format-quantity.cjs.production.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/constants.ts","../../src/formatQuantity.ts"],"sourcesContent":["import { formatQuantity } from './formatQuantity';\nexport * from './constants';\nexport * from './types';\nexport { formatQuantity };\nexport default formatQuantity;\n","import type {\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\nexport const defaultTolerance = 0.0075 as const;\n\nexport const defaultOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} satisfies Required<FormatQuantityOptions>;\n\n/**\n * A map of vulgar or simple sixteenth fractions to their traditional ASCII\n * equivalents. Sixteenths map to themselves.\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} satisfies Record<VulgarFraction, SimpleFraction>;\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\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\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 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 the [documentation](https://jakeboone02.github.io/format-quantity/).\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 = Object.hasOwn(vulgarToAsciiMap, fraction)\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,mBAAAC,EAAA,qBAAAC,EAAA,mBAAAC,EAAA,2BAAAC,EAAA,qBAAAC,IAAA,eAAAC,EAAAR,GCOO,IAAMS,EAAmB,MAEnBC,EAAiB,CAC5B,gBAAiB,GACjB,UAAW,MACX,cAAe,GACf,cAAe,EACjB,EAMaC,EAAmB,CAC9B,OAAK,MACL,OAAK,MACL,OAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,OACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,KACP,EAEaC,EAAyB,CACpC,CAAC,IAAM,QAAG,EACV,CAAC,IAAM,QAAG,EACV,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,KAAO,QAAG,EACX,CAAC,KAAO,QAAG,EACX,CAAC,KAAO,QAAG,EACX,CAAC,KAAO,QAAG,EACX,CAAC,GAAK,QAAG,EACT,CAAC,KAAO,QAAG,EACX,CAAC,IAAM,MAAG,EACV,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KAAK,EAAE,EAAI,CAAC,EAAE,KAAK,GAAG,IAAIC,GACpD,EASaE,EAAiC,CAC5CJ,EACAJ,EAAUC,IACP,CAEH,IAAMQ,EAAc,OAAOL,GAAQ,SAAW,WAAWA,CAAG,EAAIA,EAGhE,GAAI,MAAMK,CAAW,GAAKA,IAAgB,KACxC,OAAO,KAIT,GAAIA,IAAgB,EAClB,MAAO,GAMT,IAAMC,EAAOX,EAAiBC,GAAWC,CAAc,EAEvD,GAAIS,EAAK,cACP,OAAOP,EAAoBM,CAAW,EAGxC,IAAME,EAAgB,KAAK,IAAIF,CAAW,EACpCG,EAAgB,KAAK,MAAMD,CAAa,EACxCE,EAAmB,GAAGJ,EAAc,EAAI,IAAM,KAClDG,IAAkB,EAAI,GAAK,GAAGA,OAE1BE,EAAeH,EAAgBC,EAGrC,GAAIE,IAAiB,EACnB,MAAO,GAAGL,IAGZ,OAAW,CAACM,EAAKC,CAAE,IAAKC,EACtB,GAAI5B,EAAYyB,EAAcC,EAAKL,EAAK,SAAS,EAAG,CAClD,IAAMQ,EAAWzB,EAAYuB,EAAIN,CAAI,EAIrC,MAAO,GAHK,OAAO,OAAOZ,EAAkBoB,CAAQ,EAChDL,EAAiB,KAAK,EACtBA,IACYK,IAIpB,MAAO,GAAGT,GACZ,EFjIA,IAAOU,EAAQC","names":["src_exports","__export","src_default","defaultOptions","defaultTolerance","formatQuantity","fractionDecimalMatches","vulgarToAsciiMap","__toCommonJS","defaultTolerance","defaultOptions","vulgarToAsciiMap","fractionDecimalMatches","closeEnough","n1","n2","tolerance","getFraction","vulgarFractionOrSixteenth","fractionSlash","vulgarFractions","plainFraction","vulgarToAsciiMap","normalizeOptions","options","defaultOptions","romanNumeralValueKey","formatRomanNumerals","qty","digits","roman","i","formatQuantity","qtyAsNumber","opts","absoluteValue","flooredAbsVal","flooredAbsValStr","decimalValue","num","vf","fractionDecimalMatches","fraction","src_default","formatQuantity"]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
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 alue must be between 1 and 3999.
|
|
25
|
+
* Decimal values will be ignored.
|
|
26
|
+
*/
|
|
27
|
+
romanNumerals?: boolean;
|
|
28
|
+
}
|
|
29
|
+
type FormatQuantity = (qty: string | number, options?: boolean | FormatQuantityOptions) => string | null;
|
|
30
|
+
type NonZeroNumChar = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
31
|
+
type NumChar = '0' | NonZeroNumChar;
|
|
32
|
+
type SimpleFraction = `${NonZeroNumChar}/${NonZeroNumChar}` | `${NonZeroNumChar}/${NonZeroNumChar}${NumChar}` | `${NonZeroNumChar}${NumChar}/${NonZeroNumChar}${NumChar}`;
|
|
33
|
+
type Sixteenth = `${'1' | '3' | '5' | '7' | '9' | '11' | '13' | '15'}/16`;
|
|
34
|
+
type VulgarFraction = '¼' | '½' | '¾' | '⅐' | '⅑' | '⅒' | '⅓' | '⅔' | '⅕' | '⅖' | '⅗' | '⅘' | '⅙' | '⅚' | '⅛' | '⅜' | '⅝' | '⅞';
|
|
35
|
+
type FormatQuantityTests = Record<string, ([Parameters<FormatQuantity>[0], ReturnType<FormatQuantity>] | [
|
|
36
|
+
Parameters<FormatQuantity>[0],
|
|
37
|
+
ReturnType<FormatQuantity>,
|
|
38
|
+
Parameters<FormatQuantity>[1]
|
|
39
|
+
])[]>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Formats a number (or string that appears to be a number)
|
|
43
|
+
* as one would see it written in imperial measurements, e.g.
|
|
44
|
+
* "1 1/2" instead of "1.5". To use vulgar fraction characters
|
|
45
|
+
* like "½", pass `true` as the second argument. For other options
|
|
46
|
+
* see the [documentation](https://jakeboone02.github.io/format-quantity/).
|
|
47
|
+
*/
|
|
48
|
+
declare const formatQuantity: FormatQuantity;
|
|
49
|
+
|
|
50
|
+
declare const defaultTolerance: 0.0075;
|
|
51
|
+
declare const defaultOptions: {
|
|
52
|
+
vulgarFractions: false;
|
|
53
|
+
tolerance: 0.0075;
|
|
54
|
+
fractionSlash: false;
|
|
55
|
+
romanNumerals: false;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* A map of vulgar or simple sixteenth fractions to their traditional ASCII
|
|
59
|
+
* equivalents. Sixteenths map to themselves.
|
|
60
|
+
*/
|
|
61
|
+
declare const vulgarToAsciiMap: {
|
|
62
|
+
'\u00BC': "1/4";
|
|
63
|
+
'\u00BD': "1/2";
|
|
64
|
+
'\u00BE': "3/4";
|
|
65
|
+
'\u2150': "1/7";
|
|
66
|
+
'\u2151': "1/9";
|
|
67
|
+
'\u2152': "1/10";
|
|
68
|
+
'\u2153': "1/3";
|
|
69
|
+
'\u2154': "2/3";
|
|
70
|
+
'\u2155': "1/5";
|
|
71
|
+
'\u2156': "2/5";
|
|
72
|
+
'\u2157': "3/5";
|
|
73
|
+
'\u2158': "4/5";
|
|
74
|
+
'\u2159': "1/6";
|
|
75
|
+
'\u215A': "5/6";
|
|
76
|
+
'\u215B': "1/8";
|
|
77
|
+
'\u215C': "3/8";
|
|
78
|
+
'\u215D': "5/8";
|
|
79
|
+
'\u215E': "7/8";
|
|
80
|
+
};
|
|
81
|
+
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"])[];
|
|
82
|
+
|
|
83
|
+
export { FormatQuantity, FormatQuantityOptions, FormatQuantityTests, SimpleFraction, Sixteenth, VulgarFraction, formatQuantity as default, defaultOptions, defaultTolerance, formatQuantity, fractionDecimalMatches, vulgarToAsciiMap };
|