format-quantity 2.1.0-beta.0 → 3.0.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,5 +1,3 @@
1
- # format-quantity
2
-
3
1
  [![npm][badge-npm]](https://www.npmjs.com/package/format-quantity)
4
2
  ![workflow status](https://github.com/jakeboone02/format-quantity/actions/workflows/main.yml/badge.svg)
5
3
  [![codecov.io](https://codecov.io/github/jakeboone02/format-quantity/coverage.svg?branch=main)](https://codecov.io/github/jakeboone02/format-quantity?branch=main)
@@ -8,11 +6,13 @@
8
6
 
9
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".
10
8
 
9
+ **[Full documentation](https://jakeboone02.github.io/format-quantity/)**
10
+
11
11
  Features:
12
12
 
13
- - To use vulgar fraction characters like "⅞", pass `true` as the second argument (see other [options](#options), like Roman numerals, below).
13
+ - To use vulgar fraction characters like "⅞", pass `true` as the second argument. Other options like Roman numerals are described below.
14
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.
15
+ - 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
16
 
17
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
18
  >
@@ -118,18 +118,4 @@ formatQuantity(1214, { romanNumerals: true }); // "MCCXIV"
118
118
  formatQuantity(12.14, { romanNumerals: true, vulgarFractions: true }); // "XII"
119
119
  ```
120
120
 
121
- ## Other exports
122
-
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
121
  [badge-npm]: https://img.shields.io/npm/v/numeric-quantity.svg?cacheSeconds=3600&logo=npm
@@ -0,0 +1,113 @@
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 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;
29
+ }
30
+ /**
31
+ * Function signature of {@link formatQuantity}.
32
+ */
33
+ interface FormatQuantity {
34
+ (qty: string | number, options?: boolean | FormatQuantityOptions): string | null;
35
+ }
36
+ /** Any numeric character. */
37
+ type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
38
+ /** Any numeric character except '0'. */
39
+ type NonZeroDigit = Exclude<Digit, '0'>;
40
+ /**
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
+ */
45
+ type SimpleFraction = `${NonZeroDigit}/${NonZeroDigit}` | `${NonZeroDigit}/${NonZeroDigit}${Digit}` | `${NonZeroDigit}${Digit}/${NonZeroDigit}${Digit}`;
46
+ /**
47
+ * Odd numerator sixteenth fraction strings.
48
+ */
49
+ type Sixteenth = `${'1' | '3' | '5' | '7' | '9' | '11' | '13' | '15'}/16`;
50
+ /**
51
+ * Unicode vulgar fraction code points.
52
+ */
53
+ type VulgarFraction = '¼' | '½' | '¾' | '⅐' | '⅑' | '⅒' | '⅓' | '⅔' | '⅕' | '⅖' | '⅗' | '⅘' | '⅙' | '⅚' | '⅛' | '⅜' | '⅝' | '⅞';
54
+ /** @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
+
70
+ /**
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
+ */
74
+ declare const defaultTolerance: 0.0075;
75
+ /**
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
+ };
84
+ /**
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
+ };
107
+ /**
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 };
@@ -20,7 +20,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- default: () => src_default,
24
23
  defaultOptions: () => defaultOptions,
25
24
  defaultTolerance: () => defaultTolerance,
26
25
  formatQuantity: () => formatQuantity,
@@ -172,15 +171,12 @@ var formatQuantity = (qty, options = defaultOptions) => {
172
171
  for (const [num, vf] of fractionDecimalMatches) {
173
172
  if (closeEnough(decimalValue, num, opts.tolerance)) {
174
173
  const fraction = getFraction(vf, opts);
175
- const int = Object.hasOwn(vulgarToAsciiMap, fraction) ? flooredAbsValStr.trim() : flooredAbsValStr;
174
+ const int = fraction in vulgarToAsciiMap ? flooredAbsValStr.trim() : flooredAbsValStr;
176
175
  return `${int}${fraction}`;
177
176
  }
178
177
  }
179
178
  return `${qtyAsNumber}`;
180
179
  };
181
-
182
- // src/index.ts
183
- var src_default = formatQuantity;
184
180
  // Annotate the CommonJS export names for ESM import in node:
185
181
  0 && (module.exports = {
186
182
  defaultOptions,
@@ -1 +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;A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AAiB,GAAG;AACtB,WAAO,GAAG;AAAA,EACZ;AAEA,aAAW,CAAC,KAAK,EAAE,KAAK,wBAAwB;AAC9C,QAAI,YAAY,cAAc,KAAK,KAAK,SAAS,GAAG;AAClD,YAAM,WAAW,YAAY,IAAI,IAAI;AACrC,YAAM,MAAM,OAAO,OAAO,kBAAkB,QAAQ,IAChD,iBAAiB,KAAK,IACtB;AACJ,aAAO,GAAG,MAAM;AAAA,IAClB;AAAA,EACF;AAEA,SAAO,GAAG;AACZ;;;AFjIA,IAAO,cAAQ;","names":[]}
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 };\n","import type {\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Default tolerance used by {@link formatQuantity} when determining if a number\n * is close enough to a fraction value to be considered equivalent.\n */\nexport const defaultTolerance = 0.0075 as const;\n\n/**\n * Default options for {@link formatQuantity}.\n */\nexport const defaultOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const satisfies Required<FormatQuantityOptions>;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n} as const satisfies Record<VulgarFraction, SimpleFraction>;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches = [\n [0.33, '⅓'],\n [0.66, '⅔'],\n [0.2, '⅕'],\n [0.4, '⅖'],\n [0.6, '⅗'],\n [0.8, '⅘'],\n [0.166, '⅙'],\n [0.833, '⅚'],\n [0.143, '⅐'],\n [0.111, '⅑'],\n [0.1, '⅒'],\n [0.125, '⅛'],\n [0.25, '¼'],\n [0.375, '⅜'],\n [0.5, '½'],\n [0.625, '⅝'],\n [0.75, '¾'],\n [0.875, '⅞'],\n [0.0625, '1/16'],\n [0.1875, '3/16'],\n [0.3125, '5/16'],\n [0.4375, '7/16'],\n [0.5625, '9/16'],\n [0.6875, '11/16'],\n [0.8125, '13/16'],\n [0.9375, '15/16'],\n] satisfies [number, VulgarFraction | Sixteenth][];\n","import {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Determines if two numbers are close enough to consider\n * them equal for the purposes of this package.\n */\nconst closeEnough = (n1: number, n2: number, tolerance: number) =>\n Math.abs(n1 - n2) < tolerance;\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: FormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ??\n vulgarFractionOrSixteenth;\n\n if (fractionSlash) {\n return plainFraction.replace('/', '⁄');\n }\n\n return plainFraction;\n};\n\n/**\n * Merges options object with default options, converting boolean to object if necessary.\n */\nconst normalizeOptions = (\n options: Parameters<FormatQuantity>[1]\n): Required<FormatQuantityOptions> => ({\n ...defaultOptions,\n ...(typeof options === 'boolean' ? { vulgarFractions: options } : options),\n});\n\n// prettier-ignore\nconst romanNumeralValueKey = [\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\",\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\",\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\",\n] as const;\n\n/**\n * Formats a number as Roman numerals. The number must be between\n * 1 and 3999, inclusive.\n */\nexport const formatRomanNumerals = (qty: number) => {\n if (typeof qty !== 'number' || isNaN(qty)) {\n return null;\n }\n\n if (qty < 1 || qty >= 4000) {\n return '';\n }\n\n const floored = Math.floor(qty);\n\n const digits = `${floored}`.split('');\n let roman = '';\n let i = 3;\n while (i--) {\n roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;\n }\n\n return `${Array(+digits.join('') + 1).join('M')}${roman}`;\n};\n\n/**\n * Formats a number (or string that appears to be a number)\n * as one would see it written in imperial measurements, e.g.\n * \"1 1/2\" instead of \"1.5\". To use vulgar fraction characters\n * like \"½\", pass `true` as the second argument. For other options\n * see {@link FormatQuantityOptions}.\n */\nexport const formatQuantity: FormatQuantity = (\n qty,\n options = defaultOptions\n) => {\n // TODO: use numericQuantity instead of parseFloat?\n const qtyAsNumber = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like.\n if (isNaN(qtyAsNumber) || qtyAsNumber === null) {\n return null;\n }\n\n // Return an empty string if the value is zero.\n if (qtyAsNumber === 0) {\n return '';\n }\n\n // The default options parameter in the function signature only takes effect\n // if the parameter is `undefined`. The nullish coalescing operator below\n // covers the `null` case.\n const opts = normalizeOptions(options ?? defaultOptions);\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue = Math.abs(qtyAsNumber);\n const flooredAbsVal = Math.floor(absoluteValue);\n const flooredAbsValStr = `${qtyAsNumber < 0 ? '-' : ''}${\n flooredAbsVal === 0 ? '' : `${flooredAbsVal} `\n }`;\n const decimalValue = absoluteValue - flooredAbsVal;\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(decimalValue, num, opts.tolerance)) {\n const fraction = getFraction(vf, opts);\n const int =\n fraction in vulgarToAsciiMap\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACWO,IAAM,mBAAmB;AAKzB,IAAM,iBAAiB;AAAA,EAC5B,iBAAiB;AAAA,EACjB,WAAW;AAAA,EACX,eAAe;AAAA,EACf,eAAe;AACjB;AAKO,IAAM,mBAAmB;AAAA,EAC9B,QAAK;AAAA,EACL,QAAK;AAAA,EACL,QAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AACP;AAMO,IAAM,yBAAyB;AAAA,EACpC,CAAC,MAAM,QAAG;AAAA,EACV,CAAC,MAAM,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,O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AACN,aAAO,GAAG,GAAG,GAAG,QAAQ;AAAA,IAC1B;AAAA,EACF;AAEA,SAAO,GAAG,WAAW;AACvB;","names":[]}
@@ -0,0 +1,113 @@
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 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;
29
+ }
30
+ /**
31
+ * Function signature of {@link formatQuantity}.
32
+ */
33
+ interface FormatQuantity {
34
+ (qty: string | number, options?: boolean | FormatQuantityOptions): string | null;
35
+ }
36
+ /** Any numeric character. */
37
+ type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
38
+ /** Any numeric character except '0'. */
39
+ type NonZeroDigit = Exclude<Digit, '0'>;
40
+ /**
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
+ */
45
+ type SimpleFraction = `${NonZeroDigit}/${NonZeroDigit}` | `${NonZeroDigit}/${NonZeroDigit}${Digit}` | `${NonZeroDigit}${Digit}/${NonZeroDigit}${Digit}`;
46
+ /**
47
+ * Odd numerator sixteenth fraction strings.
48
+ */
49
+ type Sixteenth = `${'1' | '3' | '5' | '7' | '9' | '11' | '13' | '15'}/16`;
50
+ /**
51
+ * Unicode vulgar fraction code points.
52
+ */
53
+ type VulgarFraction = '¼' | '½' | '¾' | '⅐' | '⅑' | '⅒' | '⅓' | '⅔' | '⅕' | '⅖' | '⅗' | '⅘' | '⅙' | '⅚' | '⅛' | '⅜' | '⅝' | '⅞';
54
+ /** @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
+
70
+ /**
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
+ */
74
+ declare const defaultTolerance: 0.0075;
75
+ /**
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
+ };
84
+ /**
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
+ };
107
+ /**
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 };
@@ -1,2 +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});
1
+ "use strict";var l=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var I=(t,r)=>{for(var o in r)l(t,o,{get:r[o],enumerable:!0})},b=(t,r,o,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of X(r))!y.call(t,n)&&n!==o&&l(t,n,{get:()=>r[n],enumerable:!(a=C(r,n))||a.enumerable});return t};var d=t=>b(l({},"__esModule",{value:!0}),t);var Q={};I(Q,{defaultOptions:()=>e,defaultTolerance:()=>D,formatQuantity:()=>p,fractionDecimalMatches:()=>c,vulgarToAsciiMap:()=>i});module.exports=d(Q);var D=.0075,e={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"},c=[[.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,r,o)=>Math.abs(t-r)<o,$=(t,{fractionSlash:r,vulgarFractions:o})=>{if(o)return t;let a=i[t]??t;return r?a.replace("/","\u2044"):a},h=t=>({...e,...typeof t=="boolean"?{vulgarFractions:t}:t}),x=["","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 o=`${Math.floor(t)}`.split(""),a="",n=3;for(;n--;)a=`${x[+o.pop()+n*10]||""}${a}`;return`${Array(+o.join("")+1).join("M")}${a}`},p=(t,r=e)=>{let o=typeof t=="string"?parseFloat(t):t;if(isNaN(o)||o===null)return null;if(o===0)return"";let a=h(r??e);if(a.romanNumerals)return M(o);let n=Math.abs(o),s=Math.floor(n),u=`${o<0?"-":""}${s===0?"":`${s} `}`,m=n-s;if(m===0)return`${o}`;for(let[F,V]of c)if(g(m,F,a.tolerance)){let f=$(V,a);return`${f in i?u.trim():u}${f}`}return`${o}`};0&&(module.exports={defaultOptions,defaultTolerance,formatQuantity,fractionDecimalMatches,vulgarToAsciiMap});
2
2
  //# sourceMappingURL=format-quantity.cjs.production.js.map
@@ -1 +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,CAAC,KAAO,QAAG,EACX,CAAC,GAAK,MAAG,EACT,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IC,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"]}
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 };\n","import type {\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Default tolerance used by {@link formatQuantity} when determining if a number\n * is close enough to a fraction value to be considered equivalent.\n */\nexport const defaultTolerance = 0.0075 as const;\n\n/**\n * Default options for {@link formatQuantity}.\n */\nexport const defaultOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const satisfies Required<FormatQuantityOptions>;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n} as const satisfies Record<VulgarFraction, SimpleFraction>;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches = [\n [0.33, '⅓'],\n [0.66, '⅔'],\n [0.2, '⅕'],\n [0.4, '⅖'],\n [0.6, '⅗'],\n [0.8, '⅘'],\n [0.166, '⅙'],\n [0.833, '⅚'],\n [0.143, '⅐'],\n [0.111, '⅑'],\n [0.1, '⅒'],\n [0.125, '⅛'],\n [0.25, '¼'],\n [0.375, '⅜'],\n [0.5, '½'],\n [0.625, '⅝'],\n [0.75, '¾'],\n [0.875, '⅞'],\n [0.0625, '1/16'],\n [0.1875, '3/16'],\n [0.3125, '5/16'],\n [0.4375, '7/16'],\n [0.5625, '9/16'],\n [0.6875, '11/16'],\n [0.8125, '13/16'],\n [0.9375, '15/16'],\n] satisfies [number, VulgarFraction | Sixteenth][];\n","import {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Determines if two numbers are close enough to consider\n * them equal for the purposes of this package.\n */\nconst closeEnough = (n1: number, n2: number, tolerance: number) =>\n Math.abs(n1 - n2) < tolerance;\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: FormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ??\n vulgarFractionOrSixteenth;\n\n if (fractionSlash) {\n return plainFraction.replace('/', '⁄');\n }\n\n return plainFraction;\n};\n\n/**\n * Merges options object with default options, converting boolean to object if necessary.\n */\nconst normalizeOptions = (\n options: Parameters<FormatQuantity>[1]\n): Required<FormatQuantityOptions> => ({\n ...defaultOptions,\n ...(typeof options === 'boolean' ? { vulgarFractions: options } : options),\n});\n\n// prettier-ignore\nconst romanNumeralValueKey = [\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\",\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\",\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\",\n] as const;\n\n/**\n * Formats a number as Roman numerals. The number must be between\n * 1 and 3999, inclusive.\n */\nexport const formatRomanNumerals = (qty: number) => {\n if (typeof qty !== 'number' || isNaN(qty)) {\n return null;\n }\n\n if (qty < 1 || qty >= 4000) {\n return '';\n }\n\n const floored = Math.floor(qty);\n\n const digits = `${floored}`.split('');\n let roman = '';\n let i = 3;\n while (i--) {\n roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;\n }\n\n return `${Array(+digits.join('') + 1).join('M')}${roman}`;\n};\n\n/**\n * Formats a number (or string that appears to be a number)\n * as one would see it written in imperial measurements, e.g.\n * \"1 1/2\" instead of \"1.5\". To use vulgar fraction characters\n * like \"½\", pass `true` as the second argument. For other options\n * see {@link FormatQuantityOptions}.\n */\nexport const formatQuantity: FormatQuantity = (\n qty,\n options = defaultOptions\n) => {\n // TODO: use numericQuantity instead of parseFloat?\n const qtyAsNumber = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like.\n if (isNaN(qtyAsNumber) || qtyAsNumber === null) {\n return null;\n }\n\n // Return an empty string if the value is zero.\n if (qtyAsNumber === 0) {\n return '';\n }\n\n // The default options parameter in the function signature only takes effect\n // if the parameter is `undefined`. The nullish coalescing operator below\n // covers the `null` case.\n const opts = normalizeOptions(options ?? defaultOptions);\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue = Math.abs(qtyAsNumber);\n const flooredAbsVal = Math.floor(absoluteValue);\n const flooredAbsValStr = `${qtyAsNumber < 0 ? '-' : ''}${\n flooredAbsVal === 0 ? '' : `${flooredAbsVal} `\n }`;\n const decimalValue = absoluteValue - flooredAbsVal;\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(decimalValue, num, opts.tolerance)) {\n const fraction = getFraction(vf, opts);\n const int =\n fraction in vulgarToAsciiMap\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,qBAAAC,EAAA,mBAAAC,EAAA,2BAAAC,EAAA,qBAAAC,IAAA,eAAAC,EAAAP,GCWO,IAAMQ,EAAmB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AAE,IAAKC,EACtB,GAAI5B,EAAYyB,EAAcC,EAAKL,EAAK,SAAS,EAAG,CAClD,IAAMQ,EAAWzB,EAAYuB,EAAIN,CAAI,EAKrC,MAAO,GAHLQ,KAAYpB,EACRe,EAAiB,KAAK,EACtBA,CACO,GAAGK,CAAQ,EAC1B,CAGF,MAAO,GAAGT,CAAW,EACvB","names":["src_exports","__export","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"]}
@@ -0,0 +1,113 @@
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 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;
29
+ }
30
+ /**
31
+ * Function signature of {@link formatQuantity}.
32
+ */
33
+ interface FormatQuantity {
34
+ (qty: string | number, options?: boolean | FormatQuantityOptions): string | null;
35
+ }
36
+ /** Any numeric character. */
37
+ type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
38
+ /** Any numeric character except '0'. */
39
+ type NonZeroDigit = Exclude<Digit, '0'>;
40
+ /**
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
+ */
45
+ type SimpleFraction = `${NonZeroDigit}/${NonZeroDigit}` | `${NonZeroDigit}/${NonZeroDigit}${Digit}` | `${NonZeroDigit}${Digit}/${NonZeroDigit}${Digit}`;
46
+ /**
47
+ * Odd numerator sixteenth fraction strings.
48
+ */
49
+ type Sixteenth = `${'1' | '3' | '5' | '7' | '9' | '11' | '13' | '15'}/16`;
50
+ /**
51
+ * Unicode vulgar fraction code points.
52
+ */
53
+ type VulgarFraction = '¼' | '½' | '¾' | '⅐' | '⅑' | '⅒' | '⅓' | '⅔' | '⅕' | '⅖' | '⅗' | '⅘' | '⅙' | '⅚' | '⅛' | '⅜' | '⅝' | '⅞';
54
+ /** @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
+
70
+ /**
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
+ */
74
+ declare const defaultTolerance: 0.0075;
75
+ /**
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
+ };
84
+ /**
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
+ };
107
+ /**
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 };
@@ -0,0 +1,113 @@
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 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;
29
+ }
30
+ /**
31
+ * Function signature of {@link formatQuantity}.
32
+ */
33
+ interface FormatQuantity {
34
+ (qty: string | number, options?: boolean | FormatQuantityOptions): string | null;
35
+ }
36
+ /** Any numeric character. */
37
+ type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
38
+ /** Any numeric character except '0'. */
39
+ type NonZeroDigit = Exclude<Digit, '0'>;
40
+ /**
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
+ */
45
+ type SimpleFraction = `${NonZeroDigit}/${NonZeroDigit}` | `${NonZeroDigit}/${NonZeroDigit}${Digit}` | `${NonZeroDigit}${Digit}/${NonZeroDigit}${Digit}`;
46
+ /**
47
+ * Odd numerator sixteenth fraction strings.
48
+ */
49
+ type Sixteenth = `${'1' | '3' | '5' | '7' | '9' | '11' | '13' | '15'}/16`;
50
+ /**
51
+ * Unicode vulgar fraction code points.
52
+ */
53
+ type VulgarFraction = '¼' | '½' | '¾' | '⅐' | '⅑' | '⅒' | '⅓' | '⅔' | '⅕' | '⅖' | '⅗' | '⅘' | '⅙' | '⅚' | '⅛' | '⅜' | '⅝' | '⅞';
54
+ /** @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
+
70
+ /**
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
+ */
74
+ declare const defaultTolerance: 0.0075;
75
+ /**
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
+ };
84
+ /**
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
+ };
107
+ /**
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 };
@@ -156,17 +156,13 @@ var formatQuantity = (qty, options = defaultOptions) => {
156
156
  for (const [num, vf] of fractionDecimalMatches) {
157
157
  if (closeEnough(decimalValue, num, opts.tolerance)) {
158
158
  const fraction = getFraction(vf, opts);
159
- const int = Object.hasOwn(vulgarToAsciiMap, fraction) ? flooredAbsValStr.trim() : flooredAbsValStr;
159
+ const int = fraction in vulgarToAsciiMap ? flooredAbsValStr.trim() : flooredAbsValStr;
160
160
  return `${int}${fraction}`;
161
161
  }
162
162
  }
163
163
  return `${qtyAsNumber}`;
164
164
  };
165
-
166
- // src/index.ts
167
- var src_default = formatQuantity;
168
165
  export {
169
- src_default as default,
170
166
  defaultOptions,
171
167
  defaultTolerance,
172
168
  formatQuantity,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/constants.ts","../src/formatQuantity.ts","../src/index.ts"],"sourcesContent":["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","import { formatQuantity } from './formatQuantity';\nexport * from './constants';\nexport * from './types';\nexport { formatQuantity };\nexport default formatQuantity;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAOO,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,UAA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C;AAEA,QAAM,gBAAgB,KAAK,IAAI,WAAW;AAC1C,QAAM,gBAAgB,KAAK,MAAM,aAAa;AAC9C,QAAM,mBAAmB,GAAG,cAAc,IAAI,MAAM,KAClD,kBAAkB,IAAI,KAAK,GAAG;AAEhC,QAAM,eAAe,gBAAgB;AAGrC,MAAI,iBAAiB,GAAG;AACtB,WAAO,GAAG;AAAA,EACZ;AAEA,aAAW,CAAC,KAAK,EAAE,KAAK,wBAAwB;AAC9C,QAAI,YAAY,cAAc,KAAK,KAAK,SAAS,GAAG;AAClD,YAAM,WAAW,YAAY,IAAI,IAAI;AACrC,YAAM,MAAM,OAAO,OAAO,kBAAkB,QAAQ,IAChD,iBAAiB,KAAK,IACtB;AACJ,aAAO,GAAG,MAAM;AAAA,IAClB;AAAA,EACF;AAEA,SAAO,GAAG;AACZ;;;ACjIA,IAAO,cAAQ;","names":[]}
1
+ {"version":3,"sources":["../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import type {\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Default tolerance used by {@link formatQuantity} when determining if a number\n * is close enough to a fraction value to be considered equivalent.\n */\nexport const defaultTolerance = 0.0075 as const;\n\n/**\n * Default options for {@link formatQuantity}.\n */\nexport const defaultOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const satisfies Required<FormatQuantityOptions>;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n} as const satisfies Record<VulgarFraction, SimpleFraction>;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches = [\n [0.33, '⅓'],\n [0.66, '⅔'],\n [0.2, '⅕'],\n [0.4, '⅖'],\n [0.6, '⅗'],\n [0.8, '⅘'],\n [0.166, '⅙'],\n [0.833, '⅚'],\n [0.143, '⅐'],\n [0.111, '⅑'],\n [0.1, '⅒'],\n [0.125, '⅛'],\n [0.25, '¼'],\n [0.375, '⅜'],\n [0.5, '½'],\n [0.625, '⅝'],\n [0.75, '¾'],\n [0.875, '⅞'],\n [0.0625, '1/16'],\n [0.1875, '3/16'],\n [0.3125, '5/16'],\n [0.4375, '7/16'],\n [0.5625, '9/16'],\n [0.6875, '11/16'],\n [0.8125, '13/16'],\n [0.9375, '15/16'],\n] satisfies [number, VulgarFraction | Sixteenth][];\n","import {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Determines if two numbers are close enough to consider\n * them equal for the purposes of this package.\n */\nconst closeEnough = (n1: number, n2: number, tolerance: number) =>\n Math.abs(n1 - n2) < tolerance;\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: FormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ??\n vulgarFractionOrSixteenth;\n\n if (fractionSlash) {\n return plainFraction.replace('/', '⁄');\n }\n\n return plainFraction;\n};\n\n/**\n * Merges options object with default options, converting boolean to object if necessary.\n */\nconst normalizeOptions = (\n options: Parameters<FormatQuantity>[1]\n): Required<FormatQuantityOptions> => ({\n ...defaultOptions,\n ...(typeof options === 'boolean' ? { vulgarFractions: options } : options),\n});\n\n// prettier-ignore\nconst romanNumeralValueKey = [\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\",\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\",\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\",\n] as const;\n\n/**\n * Formats a number as Roman numerals. The number must be between\n * 1 and 3999, inclusive.\n */\nexport const formatRomanNumerals = (qty: number) => {\n if (typeof qty !== 'number' || isNaN(qty)) {\n return null;\n }\n\n if (qty < 1 || qty >= 4000) {\n return '';\n }\n\n const floored = Math.floor(qty);\n\n const digits = `${floored}`.split('');\n let roman = '';\n let i = 3;\n while (i--) {\n roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;\n }\n\n return `${Array(+digits.join('') + 1).join('M')}${roman}`;\n};\n\n/**\n * Formats a number (or string that appears to be a number)\n * as one would see it written in imperial measurements, e.g.\n * \"1 1/2\" instead of \"1.5\". To use vulgar fraction characters\n * like \"½\", pass `true` as the second argument. For other options\n * see {@link FormatQuantityOptions}.\n */\nexport const formatQuantity: FormatQuantity = (\n qty,\n options = defaultOptions\n) => {\n // TODO: use numericQuantity instead of parseFloat?\n const qtyAsNumber = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like.\n if (isNaN(qtyAsNumber) || qtyAsNumber === null) {\n return null;\n }\n\n // Return an empty string if the value is zero.\n if (qtyAsNumber === 0) {\n return '';\n }\n\n // The default options parameter in the function signature only takes effect\n // if the parameter is `undefined`. The nullish coalescing operator below\n // covers the `null` case.\n const opts = normalizeOptions(options ?? defaultOptions);\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue = Math.abs(qtyAsNumber);\n const flooredAbsVal = Math.floor(absoluteValue);\n const flooredAbsValStr = `${qtyAsNumber < 0 ? '-' : ''}${\n flooredAbsVal === 0 ? '' : `${flooredAbsVal} `\n }`;\n const decimalValue = absoluteValue - flooredAbsVal;\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(decimalValue, num, opts.tolerance)) {\n const fraction = getFraction(vf, opts);\n const int =\n fraction in vulgarToAsciiMap\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAWO,IAAM,mBAAmB;AAKzB,IAAM,iBAAiB;AAAA,EAC5B,iBAAiB;AAAA,EACjB,WAAW;AAAA,EACX,eAAe;AAAA,EACf,eAAe;AACjB;AAKO,IAAM,mBAAmB;AAAA,EAC9B,QAAK;AAAA,EACL,QAAK;AAAA,EACL,QAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AACP;AAMO,IAAM,yBAAyB;AAAA,EACpC,CAAC,MAAM,QAAG;AAAA,EACV,CAAC,MAAM,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,EAC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IAAI;AACrC,YAAM,MACJ,YAAY,mBACR,iBAAiB,KAAK,IACtB;AACN,aAAO,GAAG,GAAG,GAAG,QAAQ;AAAA,IAC1B;AAAA,EACF;AAEA,SAAO,GAAG,WAAW;AACvB;","names":[]}
@@ -141,17 +141,13 @@ var formatQuantity = (qty, options = defaultOptions) => {
141
141
  for (const [num, vf] of fractionDecimalMatches) {
142
142
  if (closeEnough(decimalValue, num, opts.tolerance)) {
143
143
  const fraction = getFraction(vf, opts);
144
- const int = Object.hasOwn(vulgarToAsciiMap, fraction) ? flooredAbsValStr.trim() : flooredAbsValStr;
144
+ const int = fraction in vulgarToAsciiMap ? flooredAbsValStr.trim() : flooredAbsValStr;
145
145
  return `${int}${fraction}`;
146
146
  }
147
147
  }
148
148
  return `${qtyAsNumber}`;
149
149
  };
150
-
151
- // src/index.ts
152
- var src_default = formatQuantity;
153
150
  export {
154
- src_default as default,
155
151
  defaultOptions,
156
152
  defaultTolerance,
157
153
  formatQuantity,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/constants.ts","../src/formatQuantity.ts","../src/index.ts"],"sourcesContent":["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","import { formatQuantity } from './formatQuantity';\nexport * from './constants';\nexport * from './types';\nexport { formatQuantity };\nexport default formatQuantity;\n"],"mappings":";AAOO,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,OAA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;AAAA,EACF;AAEA,SAAO,GAAG;AACZ;;;ACjIA,IAAO,cAAQ;","names":[]}
1
+ {"version":3,"sources":["../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import type {\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Default tolerance used by {@link formatQuantity} when determining if a number\n * is close enough to a fraction value to be considered equivalent.\n */\nexport const defaultTolerance = 0.0075 as const;\n\n/**\n * Default options for {@link formatQuantity}.\n */\nexport const defaultOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const satisfies Required<FormatQuantityOptions>;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n} as const satisfies Record<VulgarFraction, SimpleFraction>;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches = [\n [0.33, '⅓'],\n [0.66, '⅔'],\n [0.2, '⅕'],\n [0.4, '⅖'],\n [0.6, '⅗'],\n [0.8, '⅘'],\n [0.166, '⅙'],\n [0.833, '⅚'],\n [0.143, '⅐'],\n [0.111, '⅑'],\n [0.1, '⅒'],\n [0.125, '⅛'],\n [0.25, '¼'],\n [0.375, '⅜'],\n [0.5, '½'],\n [0.625, '⅝'],\n [0.75, '¾'],\n [0.875, '⅞'],\n [0.0625, '1/16'],\n [0.1875, '3/16'],\n [0.3125, '5/16'],\n [0.4375, '7/16'],\n [0.5625, '9/16'],\n [0.6875, '11/16'],\n [0.8125, '13/16'],\n [0.9375, '15/16'],\n] satisfies [number, VulgarFraction | Sixteenth][];\n","import {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Determines if two numbers are close enough to consider\n * them equal for the purposes of this package.\n */\nconst closeEnough = (n1: number, n2: number, tolerance: number) =>\n Math.abs(n1 - n2) < tolerance;\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: FormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ??\n vulgarFractionOrSixteenth;\n\n if (fractionSlash) {\n return plainFraction.replace('/', '⁄');\n }\n\n return plainFraction;\n};\n\n/**\n * Merges options object with default options, converting boolean to object if necessary.\n */\nconst normalizeOptions = (\n options: Parameters<FormatQuantity>[1]\n): Required<FormatQuantityOptions> => ({\n ...defaultOptions,\n ...(typeof options === 'boolean' ? { vulgarFractions: options } : options),\n});\n\n// prettier-ignore\nconst romanNumeralValueKey = [\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\",\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\",\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\",\n] as const;\n\n/**\n * Formats a number as Roman numerals. The number must be between\n * 1 and 3999, inclusive.\n */\nexport const formatRomanNumerals = (qty: number) => {\n if (typeof qty !== 'number' || isNaN(qty)) {\n return null;\n }\n\n if (qty < 1 || qty >= 4000) {\n return '';\n }\n\n const floored = Math.floor(qty);\n\n const digits = `${floored}`.split('');\n let roman = '';\n let i = 3;\n while (i--) {\n roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;\n }\n\n return `${Array(+digits.join('') + 1).join('M')}${roman}`;\n};\n\n/**\n * Formats a number (or string that appears to be a number)\n * as one would see it written in imperial measurements, e.g.\n * \"1 1/2\" instead of \"1.5\". To use vulgar fraction characters\n * like \"½\", pass `true` as the second argument. For other options\n * see {@link FormatQuantityOptions}.\n */\nexport const formatQuantity: FormatQuantity = (\n qty,\n options = defaultOptions\n) => {\n // TODO: use numericQuantity instead of parseFloat?\n const qtyAsNumber = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like.\n if (isNaN(qtyAsNumber) || qtyAsNumber === null) {\n return null;\n }\n\n // Return an empty string if the value is zero.\n if (qtyAsNumber === 0) {\n return '';\n }\n\n // The default options parameter in the function signature only takes effect\n // if the parameter is `undefined`. The nullish coalescing operator below\n // covers the `null` case.\n const opts = normalizeOptions(options ?? defaultOptions);\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue = Math.abs(qtyAsNumber);\n const flooredAbsVal = Math.floor(absoluteValue);\n const flooredAbsValStr = `${qtyAsNumber < 0 ? '-' : ''}${\n flooredAbsVal === 0 ? '' : `${flooredAbsVal} `\n }`;\n const decimalValue = absoluteValue - flooredAbsVal;\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(decimalValue, num, opts.tolerance)) {\n const fraction = getFraction(vf, opts);\n const int =\n fraction in vulgarToAsciiMap\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":";AAWO,IAAM,mBAAmB;AAKzB,IAAM,iBAAiB;AAAA,EAC5B,iBAAiB;AAAA,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K,GAAG,CAAC,GAAG,KAAK;AACzD;AASO,IAAM,iBAAiC,CAC5C,KACA,UAAU,mBACP;AAEH,QAAM,cAAc,OAAO,QAAQ,WAAW,WAAW,GAAG,IAAI;AAGhE,MAAI,MAAM,WAAW,KAAK,gBAAgB,MAAM;AAC9C,WAAO;AAAA,EACT;AAGA,MAAI,gBAAgB,GAAG;AACrB,WAAO;AAAA,EACT;AAKA,QAAM,OAAO,iBAAiB,WAAW,cAAc;AAEvD,MAAI,KAAK,eAAe;AACtB,WAAO,oBAAoB,WAAW;AAAA,EACxC;AAEA,QAAM,gBAAgB,KAAK,IAAI,WAAW;AAC1C,QAAM,gBAAgB,KAAK,MAAM,aAAa;AAC9C,QAAM,mBAAmB,GAAG,cAAc,IAAI,MAAM,EAAE,GACpD,kBAAkB,IAAI,KAAK,GAAG,aAAa,GAC7C;AACA,QAAM,eAAe,gBAAgB;AAGrC,MAAI,iBAAiB,GAAG;AACtB,WAAO,GAAG,WAAW;AAAA,EACvB;AAEA,aAAW,CAAC,KAAK,EAAE,KAAK,wBAAwB;AAC9C,QAAI,YAAY,cAAc,KAAK,KAAK,SAAS,GAAG;AAClD,YAAM,WAAW,YAAY,IAAI,IAAI;AACrC,YAAM,MACJ,YAAY,mBACR,iBAAiB,KAAK,IACtB;AACN,aAAO,GAAG,GAAG,GAAG,QAAQ;AAAA,IAC1B;AAAA,EACF;AAEA,SAAO,GAAG,WAAW;AACvB;","names":[]}
@@ -0,0 +1,113 @@
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 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;
29
+ }
30
+ /**
31
+ * Function signature of {@link formatQuantity}.
32
+ */
33
+ interface FormatQuantity {
34
+ (qty: string | number, options?: boolean | FormatQuantityOptions): string | null;
35
+ }
36
+ /** Any numeric character. */
37
+ type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
38
+ /** Any numeric character except '0'. */
39
+ type NonZeroDigit = Exclude<Digit, '0'>;
40
+ /**
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
+ */
45
+ type SimpleFraction = `${NonZeroDigit}/${NonZeroDigit}` | `${NonZeroDigit}/${NonZeroDigit}${Digit}` | `${NonZeroDigit}${Digit}/${NonZeroDigit}${Digit}`;
46
+ /**
47
+ * Odd numerator sixteenth fraction strings.
48
+ */
49
+ type Sixteenth = `${'1' | '3' | '5' | '7' | '9' | '11' | '13' | '15'}/16`;
50
+ /**
51
+ * Unicode vulgar fraction code points.
52
+ */
53
+ type VulgarFraction = '¼' | '½' | '¾' | '⅐' | '⅑' | '⅒' | '⅓' | '⅔' | '⅕' | '⅖' | '⅗' | '⅘' | '⅙' | '⅚' | '⅛' | '⅜' | '⅝' | '⅞';
54
+ /** @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
+
70
+ /**
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
+ */
74
+ declare const defaultTolerance: 0.0075;
75
+ /**
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
+ };
84
+ /**
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
+ };
107
+ /**
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 };
@@ -1,2 +1,2 @@
1
- var I=.0075,n={vulgarFractions:!1,tolerance:.0075,fractionSlash:!1,romanNumerals:!1},s={"\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"},m=[[.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 V=(t,a,r)=>Math.abs(t-a)<r,C=(t,{fractionSlash:a,vulgarFractions:r})=>{if(r)return t;let o=s[t]??t;return a?o.replace("/","\u2044"):o},X=t=>({...n,...typeof t=="boolean"?{vulgarFractions:t}:t}),b=["","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"],d=t=>{if(typeof t!="number"||isNaN(t))return null;if(t<1||t>=4e3)return"";let r=`${Math.floor(t)}`.split(""),o="",e=3;for(;e--;)o=`${b[+r.pop()+e*10]||""}${o}`;return`${Array(+r.join("")+1).join("M")}${o}`},f=(t,a=n)=>{let r=typeof t=="string"?parseFloat(t):t;if(isNaN(r)||r===null)return null;if(r===0)return"";let o=X(a??n);if(o.romanNumerals)return d(r);let e=Math.abs(r),i=Math.floor(e),l=`${r<0?"-":""}${i===0?"":`${i} `}`,u=e-i;if(u===0)return`${r}`;for(let[p,F]of m)if(V(u,p,o.tolerance)){let c=C(F,o);return`${Object.hasOwn(s,c)?l.trim():l}${c}`}return`${r}`};var M=f;export{M as default,n as defaultOptions,I as defaultTolerance,f as formatQuantity,m as fractionDecimalMatches,s as vulgarToAsciiMap};
1
+ var d=.0075,e={vulgarFractions:!1,tolerance:.0075,fractionSlash:!1,romanNumerals:!1},s={"\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"},m=[[.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 F=(t,a,o)=>Math.abs(t-a)<o,V=(t,{fractionSlash:a,vulgarFractions:o})=>{if(o)return t;let r=s[t]??t;return a?r.replace("/","\u2044"):r},C=t=>({...e,...typeof t=="boolean"?{vulgarFractions:t}:t}),X=["","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"],y=t=>{if(typeof t!="number"||isNaN(t))return null;if(t<1||t>=4e3)return"";let o=`${Math.floor(t)}`.split(""),r="",n=3;for(;n--;)r=`${X[+o.pop()+n*10]||""}${r}`;return`${Array(+o.join("")+1).join("M")}${r}`},I=(t,a=e)=>{let o=typeof t=="string"?parseFloat(t):t;if(isNaN(o)||o===null)return null;if(o===0)return"";let r=C(a??e);if(r.romanNumerals)return y(o);let n=Math.abs(o),i=Math.floor(n),l=`${o<0?"-":""}${i===0?"":`${i} `}`,c=n-i;if(c===0)return`${o}`;for(let[f,p]of m)if(F(c,f,r.tolerance)){let u=V(p,r);return`${u in s?l.trim():l}${u}`}return`${o}`};export{e as defaultOptions,d as defaultTolerance,I as formatQuantity,m as fractionDecimalMatches,s as vulgarToAsciiMap};
2
2
  //# sourceMappingURL=format-quantity.production.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/constants.ts","../src/formatQuantity.ts","../src/index.ts"],"sourcesContent":["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","import { formatQuantity } from './formatQuantity';\nexport * from './constants';\nexport * from './types';\nexport { formatQuantity };\nexport default formatQuantity;\n"],"mappings":"AAOO,IAAMA,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,CAAC,KAAO,QAAG,EACX,CAAC,GAAK,MAAG,EACT,CAAC,KAAO,QAAG,EACX,CAAC,IAAM,MAAG,EACV,CAAC,KAAO,QAAG,EACX,CAAC,MAAQ,MAAM,EACf,CAAC,MAAQ,MAAM,EACf,CAAC,MAAQ,MAAM,EACf,CAAC,M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AAgB,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,ECjIA,IAAOU,EAAQC","names":["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"]}
1
+ {"version":3,"sources":["../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import type {\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Default tolerance used by {@link formatQuantity} when determining if a number\n * is close enough to a fraction value to be considered equivalent.\n */\nexport const defaultTolerance = 0.0075 as const;\n\n/**\n * Default options for {@link formatQuantity}.\n */\nexport const defaultOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const satisfies Required<FormatQuantityOptions>;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n} as const satisfies Record<VulgarFraction, SimpleFraction>;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches = [\n [0.33, '⅓'],\n [0.66, '⅔'],\n [0.2, '⅕'],\n [0.4, '⅖'],\n [0.6, '⅗'],\n [0.8, '⅘'],\n [0.166, '⅙'],\n [0.833, '⅚'],\n [0.143, '⅐'],\n [0.111, '⅑'],\n [0.1, '⅒'],\n [0.125, '⅛'],\n [0.25, '¼'],\n [0.375, '⅜'],\n [0.5, '½'],\n [0.625, '⅝'],\n [0.75, '¾'],\n [0.875, '⅞'],\n [0.0625, '1/16'],\n [0.1875, '3/16'],\n [0.3125, '5/16'],\n [0.4375, '7/16'],\n [0.5625, '9/16'],\n [0.6875, '11/16'],\n [0.8125, '13/16'],\n [0.9375, '15/16'],\n] satisfies [number, VulgarFraction | Sixteenth][];\n","import {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Determines if two numbers are close enough to consider\n * them equal for the purposes of this package.\n */\nconst closeEnough = (n1: number, n2: number, tolerance: number) =>\n Math.abs(n1 - n2) < tolerance;\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: FormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ??\n vulgarFractionOrSixteenth;\n\n if (fractionSlash) {\n return plainFraction.replace('/', '⁄');\n }\n\n return plainFraction;\n};\n\n/**\n * Merges options object with default options, converting boolean to object if necessary.\n */\nconst normalizeOptions = (\n options: Parameters<FormatQuantity>[1]\n): Required<FormatQuantityOptions> => ({\n ...defaultOptions,\n ...(typeof options === 'boolean' ? { vulgarFractions: options } : options),\n});\n\n// prettier-ignore\nconst romanNumeralValueKey = [\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\",\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\",\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\",\n] as const;\n\n/**\n * Formats a number as Roman numerals. The number must be between\n * 1 and 3999, inclusive.\n */\nexport const formatRomanNumerals = (qty: number) => {\n if (typeof qty !== 'number' || isNaN(qty)) {\n return null;\n }\n\n if (qty < 1 || qty >= 4000) {\n return '';\n }\n\n const floored = Math.floor(qty);\n\n const digits = `${floored}`.split('');\n let roman = '';\n let i = 3;\n while (i--) {\n roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;\n }\n\n return `${Array(+digits.join('') + 1).join('M')}${roman}`;\n};\n\n/**\n * Formats a number (or string that appears to be a number)\n * as one would see it written in imperial measurements, e.g.\n * \"1 1/2\" instead of \"1.5\". To use vulgar fraction characters\n * like \"½\", pass `true` as the second argument. For other options\n * see {@link FormatQuantityOptions}.\n */\nexport const formatQuantity: FormatQuantity = (\n qty,\n options = defaultOptions\n) => {\n // TODO: use numericQuantity instead of parseFloat?\n const qtyAsNumber = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like.\n if (isNaN(qtyAsNumber) || qtyAsNumber === null) {\n return null;\n }\n\n // Return an empty string if the value is zero.\n if (qtyAsNumber === 0) {\n return '';\n }\n\n // The default options parameter in the function signature only takes effect\n // if the parameter is `undefined`. The nullish coalescing operator below\n // covers the `null` case.\n const opts = normalizeOptions(options ?? defaultOptions);\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue = Math.abs(qtyAsNumber);\n const flooredAbsVal = Math.floor(absoluteValue);\n const flooredAbsValStr = `${qtyAsNumber < 0 ? '-' : ''}${\n flooredAbsVal === 0 ? '' : `${flooredAbsVal} `\n }`;\n const decimalValue = absoluteValue - flooredAbsVal;\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(decimalValue, num, opts.tolerance)) {\n const fraction = getFraction(vf, opts);\n const int =\n fraction in vulgarToAsciiMap\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":"AAWO,IAAMA,EAAmB,MAKnBC,EAAiB,CAC5B,gBAAiB,GACjB,UAAW,MACX,cAAe,GACf,cAAe,EACjB,EAKaC,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,EAMaC,EAAyB,CACpC,CAAC,IAAM,QAAG,EACV,CAAC,IAAM,QAAG,EACV,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,G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G,CAEL,GAAG,MAAM,EAAE,EAChCE,EAAQ,GACRC,EAAI,EACR,KAAOA,KACLD,EAAQ,GAAGJ,EAAqB,CAACG,EAAO,IAAI,EAAKE,EAAI,EAAE,GAAK,EAAE,GAAGD,CAAK,GAGxE,MAAO,GAAG,MAAM,CAACD,EAAO,KAAK,EAAE,EAAI,CAAC,EAAE,KAAK,GAAG,CAAC,GAAGC,CAAK,EACzD,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,EAAE,GACpDG,IAAkB,EAAI,GAAK,GAAGA,CAAa,GAC7C,GACME,EAAeH,EAAgBC,EAGrC,GAAIE,IAAiB,EACnB,MAAO,GAAGL,CAAW,GAGvB,OAAW,CAACM,EAAKC,CAAE,IAAKC,EACtB,GAAI5B,EAAYyB,EAAcC,EAAKL,EAAK,SAAS,EAAG,CAClD,IAAMQ,EAAWzB,EAAYuB,EAAIN,CAAI,EAKrC,MAAO,GAHLQ,KAAYpB,EACRe,EAAiB,KAAK,EACtBA,CACO,GAAGK,CAAQ,EAC1B,CAGF,MAAO,GAAGT,CAAW,EACvB","names":["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"]}
@@ -1,2 +1,2 @@
1
- "use strict";var FormatQuantity=(()=>{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});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;return I(N);})();
1
+ "use strict";var FormatQuantity=(()=>{var l=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var I=(t,r)=>{for(var o in r)l(t,o,{get:r[o],enumerable:!0})},b=(t,r,o,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of X(r))!y.call(t,n)&&n!==o&&l(t,n,{get:()=>r[n],enumerable:!(a=C(r,n))||a.enumerable});return t};var d=t=>b(l({},"__esModule",{value:!0}),t);var Q={};I(Q,{defaultOptions:()=>e,defaultTolerance:()=>D,formatQuantity:()=>p,fractionDecimalMatches:()=>c,vulgarToAsciiMap:()=>i});var D=.0075,e={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"},c=[[.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,r,o)=>Math.abs(t-r)<o,$=(t,{fractionSlash:r,vulgarFractions:o})=>{if(o)return t;let a=i[t]??t;return r?a.replace("/","\u2044"):a},h=t=>({...e,...typeof t=="boolean"?{vulgarFractions:t}:t}),x=["","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 o=`${Math.floor(t)}`.split(""),a="",n=3;for(;n--;)a=`${x[+o.pop()+n*10]||""}${a}`;return`${Array(+o.join("")+1).join("M")}${a}`},p=(t,r=e)=>{let o=typeof t=="string"?parseFloat(t):t;if(isNaN(o)||o===null)return null;if(o===0)return"";let a=h(r??e);if(a.romanNumerals)return M(o);let n=Math.abs(o),s=Math.floor(n),u=`${o<0?"-":""}${s===0?"":`${s} `}`,m=n-s;if(m===0)return`${o}`;for(let[F,V]of c)if(g(m,F,a.tolerance)){let f=$(V,a);return`${f in i?u.trim():u}${f}`}return`${o}`};return d(Q);})();
2
2
  //# sourceMappingURL=format-quantity.umd.min.js.map
@@ -1 +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":"kcAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,mBAAAC,EAAA,qBAAAC,EAAA,mBAAAC,EAAA,2BAAAC,EAAA,qBAAAC,ICOO,IAAMC,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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_default","defaultOptions","defaultTolerance","formatQuantity","fractionDecimalMatches","vulgarToAsciiMap","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"]}
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 };\n","import type {\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Default tolerance used by {@link formatQuantity} when determining if a number\n * is close enough to a fraction value to be considered equivalent.\n */\nexport const defaultTolerance = 0.0075 as const;\n\n/**\n * Default options for {@link formatQuantity}.\n */\nexport const defaultOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const satisfies Required<FormatQuantityOptions>;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n} as const satisfies Record<VulgarFraction, SimpleFraction>;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches = [\n [0.33, '⅓'],\n [0.66, '⅔'],\n [0.2, '⅕'],\n [0.4, '⅖'],\n [0.6, '⅗'],\n [0.8, '⅘'],\n [0.166, '⅙'],\n [0.833, '⅚'],\n [0.143, '⅐'],\n [0.111, '⅑'],\n [0.1, '⅒'],\n [0.125, '⅛'],\n [0.25, '¼'],\n [0.375, '⅜'],\n [0.5, '½'],\n [0.625, '⅝'],\n [0.75, '¾'],\n [0.875, '⅞'],\n [0.0625, '1/16'],\n [0.1875, '3/16'],\n [0.3125, '5/16'],\n [0.4375, '7/16'],\n [0.5625, '9/16'],\n [0.6875, '11/16'],\n [0.8125, '13/16'],\n [0.9375, '15/16'],\n] satisfies [number, VulgarFraction | Sixteenth][];\n","import {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Determines if two numbers are close enough to consider\n * them equal for the purposes of this package.\n */\nconst closeEnough = (n1: number, n2: number, tolerance: number) =>\n Math.abs(n1 - n2) < tolerance;\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: FormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ??\n vulgarFractionOrSixteenth;\n\n if (fractionSlash) {\n return plainFraction.replace('/', '⁄');\n }\n\n return plainFraction;\n};\n\n/**\n * Merges options object with default options, converting boolean to object if necessary.\n */\nconst normalizeOptions = (\n options: Parameters<FormatQuantity>[1]\n): Required<FormatQuantityOptions> => ({\n ...defaultOptions,\n ...(typeof options === 'boolean' ? { vulgarFractions: options } : options),\n});\n\n// prettier-ignore\nconst romanNumeralValueKey = [\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\",\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\",\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\",\n] as const;\n\n/**\n * Formats a number as Roman numerals. The number must be between\n * 1 and 3999, inclusive.\n */\nexport const formatRomanNumerals = (qty: number) => {\n if (typeof qty !== 'number' || isNaN(qty)) {\n return null;\n }\n\n if (qty < 1 || qty >= 4000) {\n return '';\n }\n\n const floored = Math.floor(qty);\n\n const digits = `${floored}`.split('');\n let roman = '';\n let i = 3;\n while (i--) {\n roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;\n }\n\n return `${Array(+digits.join('') + 1).join('M')}${roman}`;\n};\n\n/**\n * Formats a number (or string that appears to be a number)\n * as one would see it written in imperial measurements, e.g.\n * \"1 1/2\" instead of \"1.5\". To use vulgar fraction characters\n * like \"½\", pass `true` as the second argument. For other options\n * see {@link FormatQuantityOptions}.\n */\nexport const formatQuantity: FormatQuantity = (\n qty,\n options = defaultOptions\n) => {\n // TODO: use numericQuantity instead of parseFloat?\n const qtyAsNumber = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like.\n if (isNaN(qtyAsNumber) || qtyAsNumber === null) {\n return null;\n }\n\n // Return an empty string if the value is zero.\n if (qtyAsNumber === 0) {\n return '';\n }\n\n // The default options parameter in the function signature only takes effect\n // if the parameter is `undefined`. The nullish coalescing operator below\n // covers the `null` case.\n const opts = normalizeOptions(options ?? defaultOptions);\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue = Math.abs(qtyAsNumber);\n const flooredAbsVal = Math.floor(absoluteValue);\n const flooredAbsValStr = `${qtyAsNumber < 0 ? '-' : ''}${\n flooredAbsVal === 0 ? '' : `${flooredAbsVal} `\n }`;\n const decimalValue = absoluteValue - flooredAbsVal;\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(decimalValue, num, opts.tolerance)) {\n const fraction = getFraction(vf, opts);\n const int =\n fraction in vulgarToAsciiMap\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":"kcAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,qBAAAC,EAAA,mBAAAC,EAAA,2BAAAC,EAAA,qBAAAC,ICWO,IAAMC,EAAmB,MAKnBC,EAAiB,CAC5B,gBAAiB,GACjB,UAAW,MACX,cAAe,GACf,cAAe,EACjB,EAKaC,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,EAMaC,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,CAAC,KAAO,QAAG,EACX,CAAC,GAAK,MAAG,EACT,CAAC,KAAO,QAAG,EACX,CAAC,IAAM,MAAG,EACV,CAAC,KAAO,QAAG,EACX,CAAC,MAAQ,MAAM,EACf,CAAC,MAAQ,MAAM,EACf,CAAC,MAAQ,MAAM,EACf,CAAC,MAAQ,MAAM,EACf,CAAC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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,EAAE,GACpDG,IAAkB,EAAI,GAAK,GAAGA,CAAa,GAC7C,GACME,EAAeH,EAAgBC,EAGrC,GAAIE,IAAiB,EACnB,MAAO,GAAGL,CAAW,GAGvB,OAAW,CAACM,EAAKC,CAAE,IAAKC,EACtB,GAAI5B,EAAYyB,EAAcC,EAAKL,EAAK,SAAS,EAAG,CAClD,IAAMQ,EAAWzB,EAAYuB,EAAIN,CAAI,EAKrC,MAAO,GAHLQ,KAAYpB,EACRe,EAAiB,KAAK,EACtBA,CACO,GAAGK,CAAQ,EAC1B,CAGF,MAAO,GAAGT,CAAW,EACvB","names":["src_exports","__export","defaultOptions","defaultTolerance","formatQuantity","fractionDecimalMatches","vulgarToAsciiMap","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"]}
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.1.0-beta.0",
2
+ "version": "3.0.0",
3
3
  "name": "format-quantity",
4
4
  "author": "Jake Boone <jakeboone02@gmail.com>",
5
5
  "description": "Number formatter for imperial measurements with support for vulgar fractions",
@@ -11,12 +11,17 @@
11
11
  "exports": {
12
12
  "./package.json": "./package.json",
13
13
  ".": {
14
- "types": "./dist/format-quantity.d.ts",
15
- "import": "./dist/format-quantity.mjs",
16
- "require": "./dist/cjs/index.js"
14
+ "import": {
15
+ "types": "./dist/format-quantity.d.mts",
16
+ "default": "./dist/format-quantity.mjs"
17
+ },
18
+ "require": {
19
+ "types": "./dist/cjs/format-quantity.cjs.development.d.ts",
20
+ "default": "./dist/cjs/index.js"
21
+ }
17
22
  }
18
23
  },
19
- "types": "./dist/format-quantity.d.ts",
24
+ "types": "./dist/cjs/format-quantity.cjs.production.d.ts",
20
25
  "unpkg": "./dist/format-quantity.umd.min.js",
21
26
  "license": "MIT",
22
27
  "keywords": [
@@ -40,23 +45,25 @@
40
45
  "url": "https://github.com/jakeboone02/format-quantity"
41
46
  },
42
47
  "scripts": {
43
- "start": "bun ./server.ts",
48
+ "start": "bun --hot ./server.ts",
44
49
  "build": "tsup",
45
- "test": "jest",
46
- "watch": "jest --watch",
50
+ "docs": "bunx typedoc",
51
+ "test": "bun test",
52
+ "watch": "bun test --watch",
47
53
  "publish:npm": "np",
48
54
  "pretty-print": "prettier --write *.{mjs,ts,json} src/*.*"
49
55
  },
50
56
  "devDependencies": {
51
- "@types/jest": "^29.5.2",
52
- "@types/node": "^20.3.1",
53
- "bun-types": "^0.6.9",
54
- "jest": "^29.5.0",
55
- "np": "^8.0.4",
56
- "prettier": "^2.8.8",
57
- "ts-jest": "^29.1.0",
58
- "tsup": "^7.0.0",
59
- "typescript": "^5.1.3"
57
+ "@types/node": "^20.11.2",
58
+ "@types/web": "^0.0.135",
59
+ "bun-types": "^1.0.22",
60
+ "np": "^9.2.0",
61
+ "open": "^10.0.3",
62
+ "prettier": "^3.2.2",
63
+ "tsup": "^8.0.1",
64
+ "typedoc": "^0.25.7",
65
+ "typedoc-plugin-katex": "^0.1.2",
66
+ "typescript": "^5.3.3"
60
67
  },
61
68
  "publishConfig": {
62
69
  "registry": "https://registry.npmjs.org"
@@ -1,83 +0,0 @@
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 };