format-quantity 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.0.2 (2022-04-16)
2
+
3
+ - Update dependencies
4
+ - Migrate from tsdx to Vite
5
+
1
6
  ## 1.0.1 (2021-02-15)
2
7
 
3
8
  - Added description to package.json
package/README.md CHANGED
@@ -6,9 +6,9 @@
6
6
  [![downloads](https://img.shields.io/npm/dm/format-quantity.svg)](http://npm-stat.com/charts.html?package=format-quantity&from=2015-08-01)
7
7
  [![MIT License](https://img.shields.io/npm/l/format-quantity.svg)](http://opensource.org/licenses/MIT)
8
8
 
9
- Formats a number (or string that appears to be a number) as one would see it written in imperial measurements, e.g. "1 1/2" instead of "1.5". To use unicode vulgar fractions like "⅞", pass `true` as the second argument.
9
+ Formats a number (or string that appears to be a number) as one would see it written in imperial measurements, e.g. "1 1/2" instead of "1.5". To use vulgar fraction characters like "⅞", pass `true` as the second argument.
10
10
 
11
- For the inverse operation, converting a string (which may include mixed numbers or vulgar fractions) to a number, check out [numeric-quantity](https://www.npmjs.com/package/numeric-quantity) or, if you're interested in parsing recipe ingredient strings, try [parse-ingredient](https://www.npmjs.com/package/parse-ingredient).
11
+ For the inverse operation, converting a string (which may include mixed numbers or vulgar fractions) to a `number`, check out [numeric-quantity](https://www.npmjs.com/package/numeric-quantity) or, if you're interested in parsing recipe ingredient strings, try [parse-ingredient](https://www.npmjs.com/package/parse-ingredient).
12
12
 
13
13
  ## Installation
14
14
 
@@ -24,23 +24,83 @@ yarn add format-quantity
24
24
 
25
25
  ### Browser
26
26
 
27
- In the browser, available as a global function `formatQuantity`.
27
+ In the browser, all exports including the `formatQuantity` function are available on the global object `FormatQuantity`.
28
28
 
29
29
  ```html
30
30
  <script src="https://unpkg.com/format-quantity"></script>
31
31
  <script>
32
- console.log(formatQuantity(10.5)); // "10 1/2"
32
+ console.log(FormatQuantity.formatQuantity(10.5)); // "10 1/2"
33
33
  </script>
34
34
  ```
35
35
 
36
36
  ## Usage
37
37
 
38
38
  ```js
39
- import formatQuantity from 'format-quantity';
39
+ import { formatQuantity } from 'format-quantity';
40
40
 
41
- console.log(formatQuantity(1.5)); // "1 1/2"
42
- console.log(formatQuantity(2.66)); // "2 2/3"
43
- console.log(formatQuantity(3.875, true)); // "3⅞"
41
+ formatQuantity(1.5); // "1 1/2"
42
+ formatQuantity(2.66); // "2 2/3"
43
+ formatQuantity(3.875, true); // "3⅞"
44
44
  ```
45
45
 
46
- The return value will be `null` if the provided argument is not a number or a string that evaluates to a number using `parseFloat`. The return value will be an empty string (`""`) if the provided argument is `0` or `"0"` (this is done to fit the primary use case of recipe ingredients).
46
+ The return value will be `null` if the provided argument is not a number or a string that evaluates to a number using `parseFloat`. The return value will be an empty string (`""`) if the provided argument is `0` or `"0"` (this is done to fit the primary use case of recipe ingredient quantities).
47
+
48
+ ## Options
49
+
50
+ The second parameter to `formatQuantity` can be a `boolean` value or an options object.
51
+
52
+ ### `vulgarFractions`
53
+
54
+ | Type | Default |
55
+ | --------- | ------: |
56
+ | `boolean` | `false` |
57
+
58
+ Returns vulgar fractions when appropriate. This option has the same effect as passing a plain `boolean` value as the second parameter.
59
+
60
+ ```js
61
+ formatQuantity(3.875, { vulgarFractions: true }); // "3⅞"
62
+ // is the same as
63
+ formatQuantity(3.875, true); // "3⅞"
64
+ ```
65
+
66
+ ### `tolerance`
67
+
68
+ | Type | Default |
69
+ | -------- | ------: |
70
+ | `number` | `0.009` |
71
+
72
+ This option determines how close the decimal portion of a number has to be to the actual quotient of a fraction to be considered a match. For example, consider the fraction 1⁄3: `1 ÷ 3 = 0.3333...` repeating forever. The number `0.333` (333 thousandths) is not equivalent to 1⁄3, but it's very close. So even though `0.333 !== (1 / 3)`, `formatQuantity(0.333)` will return `"1/3"` the same as `formatQuantity(1/3)`.
73
+
74
+ A lower tolerance increases the likelihood that `formatQuantity` will return a decimal representation instead of a fraction or mixed number since the matching algorithm will be stricter. An greater tolerance increases the likelihood that `formatQuantity` will return a fraction or mixed number, but at the risk of arbitrarily matching an incorrect fraction simply because it gets evaluated first (see [`src/index.ts`](src/index.ts) for the actual order of evaluation).
75
+
76
+ ```js
77
+ // Low tolerance - returns a decimal since 0.333 is not close enough to 1/3
78
+ formatQuantity(0.333, { tolerance: 0.00001 }); // "0.333"
79
+ // High tolerance - matches "1/3" even for "3/10"
80
+ formatQuantity(0.3, { tolerance: 0.1 }); // "1/3"
81
+ // Way too high tolerance - incorrect result because thirds get evaluated before halves
82
+ formatQuantity(0.5, { tolerance: 0.5 }); // "1/3"
83
+ ```
84
+
85
+ ### `fractionSlash`
86
+
87
+ | Type | Default |
88
+ | --------- | ------: |
89
+ | `boolean` | `false` |
90
+
91
+ Uses the [fraction slash character](<https://en.wikipedia.org/wiki/Slash_(punctuation)#Fractions>) (`"\u2044"`) to separate the numerator and denominator instead of the regular "solidus" slash (`"\u002f"`). This option is ignored if the `vulgarFractions` option is also `true`.
92
+
93
+ ```js
94
+ formatQuantity(3.875, { fractionSlash: true }); // "3 7⁄8"
95
+ formatQuantity(3.875, { fractionSlash: true, vulgarFractions: true }); // "3⅞"
96
+ ```
97
+
98
+ ## Other exports
99
+
100
+ | Name | Type | Description |
101
+ | ------------------------ | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
102
+ | `defaultTolerance` | `number` | `0.009` |
103
+ | `fractionDecimalMatches` | `[number, VulgarFraction][]` | List of fractions and the decimal values that are close enough to match them (inputs are evaluated against the decimal values in the order of this array) |
104
+ | `vulgarToPlainMap` | `object` | Map of vulgar fraction characters to their equivalent ASCII strings (`"⅓"` to `"1/3"`, `"⅞"` to `"7/8"`, etc.) |
105
+ | `FormatQuantityOptions` | `interface` | Shape of `formatQuantity`'s second parameter, if not a `boolean` value |
106
+ | `VulgarFraction` | `type` | The set of [vulgar fraction characters](https://en.wikipedia.org/wiki/Number_Forms) (`"\u00bc"`, `"\u00bd"`, `"\u00be"`, and `"\u2150"` through `"\u215e"`) |
@@ -0,0 +1,9 @@
1
+ import type { VulgarFraction } from './types';
2
+ export declare const defaultTolerance = 0.009;
3
+ /**
4
+ * A map of vulgar fractions to their traditional ASCII equivalents.
5
+ */
6
+ export declare const vulgarToPlainMap: {
7
+ [vf in VulgarFraction]: `${number}/${number}`;
8
+ };
9
+ export declare const fractionDecimalMatches: [number, VulgarFraction][];
@@ -1,2 +1,2 @@
1
- "use strict";const r=(f,e)=>Math.abs(f-e)<.009;function c(f,e){const n=typeof f=="string"?parseFloat(f):f;if(isNaN(n)||n===null)return null;if(n===0)return"";const s=Math.abs(n),o=Math.floor(s),u=o===0?"":`${n<0?"-":""}${o} `,t=s-o;if(t===0)return`${n}`;const $=e?u.trim():u;if(r(t,.33))return`${$}${e?"\u2153":"1/3"}`;if(r(t,.66))return`${$}${e?"\u2154":"2/3"}`;if(r(t,.2))return`${$}${e?"\u2155":"1/5"}`;if(r(t,.4))return`${$}${e?"\u2156":"2/5"}`;if(r(t,.6))return`${$}${e?"\u2157":"3/5"}`;if(r(t,.8))return`${$}${e?"\u2158":"4/5"}`;switch(t){case .125:return`${$}${e?"\u215B":"1/8"}`;case .25:return`${$}${e?"\xBC":"1/4"}`;case .375:return`${$}${e?"\u215C":"3/8"}`;case .5:return`${$}${e?"\xBD":"1/2"}`;case .625:return`${$}${e?"\u215D":"5/8"}`;case .75:return`${$}${e?"\xBE":"3/4"}`;case .875:return`${$}${e?"\u215E":"7/8"}`}return`${n}`}module.exports=c;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const i=.009,n={"\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"},d=[[.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"]],h=(a,t,u)=>Math.abs(a-t)<u,M=(a,t)=>{var f;const u=typeof a=="string"?parseFloat(a):a;if(isNaN(u)||u===null)return null;if(u===0)return"";const l=Math.abs(u),r=Math.floor(l),c=`${u<0?"-":""}${r===0?"":`${r} `}`,s=l-r;if(s===0)return`${u}`;const o=typeof t=="boolean"?{vulgarFractions:t}:t!=null?t:{},g=o.vulgarFractions?c.trim():c,m=e=>o.vulgarFractions?e:o.fractionSlash?n[e].replace("/","\u2044"):n[e];for(const[e,B]of d)if(h(s,e,(f=o.tolerance)!=null?f:i))return`${g}${m(B)}`;return`${u}`};exports.default=M;exports.defaultTolerance=i;exports.formatQuantity=M;exports.fractionDecimalMatches=d;exports.vulgarToPlainMap=n;
2
2
  //# sourceMappingURL=format-quantity.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"format-quantity.cjs.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Determines if two numbers are close enough to consider\n * them equal for our purposes\n */\nconst closeEnough = (a: number, b: number) => Math.abs(a - b) < 0.009;\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 fractions, e.g. \"½\",\n * pass `true` as the second argument.\n */\nfunction formatQuantity(qty: string | number, useVulgarFractions?: boolean) {\n const dQty = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Bomb out if not a number\n if (isNaN(dQty) || dQty === null) {\n return null;\n }\n\n // Return an empty string if the value is zero\n if (dQty === 0) {\n return '';\n }\n\n const dQtyAbs = Math.abs(dQty);\n const iFloor = Math.floor(dQtyAbs);\n const sFloor = iFloor === 0.0 ? '' : `${dQty < 0 ? '-' : ''}${iFloor} `;\n const dDecimal = dQtyAbs - iFloor;\n\n // Handle integers first. Just return the given value as a string.\n if (dDecimal === 0) {\n return `${dQty}`;\n }\n\n const sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor;\n\n // Handle infinitely repeating decimals next, since\n // we'll never get an exact match for a switch case:\n if (closeEnough(dDecimal, 0.33)) {\n return `${sFloorFinal}${useVulgarFractions ? '' : '1/3'}`;\n } else if (closeEnough(dDecimal, 0.66)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅔' : '2/3'}`;\n } else if (closeEnough(dDecimal, 0.2)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅕' : '1/5'}`;\n } else if (closeEnough(dDecimal, 0.4)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅖' : '2/5'}`;\n } else if (closeEnough(dDecimal, 0.6)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅗' : '3/5'}`;\n } else if (closeEnough(dDecimal, 0.8)) {\n return `${sFloorFinal}${useVulgarFractions ? '' : '4/5'}`;\n } else {\n switch (dDecimal) {\n case 0.125:\n return `${sFloorFinal}${useVulgarFractions ? '⅛' : '1/8'}`;\n case 0.25:\n return `${sFloorFinal}${useVulgarFractions ? '¼' : '1/4'}`;\n case 0.375:\n return `${sFloorFinal}${useVulgarFractions ? '⅜' : '3/8'}`;\n case 0.5:\n return `${sFloorFinal}${useVulgarFractions ? '½' : '1/2'}`;\n case 0.625:\n return `${sFloorFinal}${useVulgarFractions ? '⅝' : '5/8'}`;\n case 0.75:\n return `${sFloorFinal}${useVulgarFractions ? '¾' : '3/4'}`;\n case 0.875:\n return `${sFloorFinal}${useVulgarFractions ? '⅞' : '7/8'}`;\n }\n }\n\n return `${dQty}`;\n}\n\nexport default formatQuantity;\n"],"names":[],"mappings":"aAIA,KAAM,GAAc,CAAC,EAAW,IAAc,KAAK,IAAI,EAAI,CAAC,EAAI,KAQhE,WAAwB,EAAsB,EAA8B,CAC1E,KAAM,GAAO,MAAO,IAAQ,SAAW,WAAW,CAAG,EAAI,EAGzD,GAAI,MAAM,CAAI,GAAK,IAAS,KACnB,MAAA,MAIT,GAAI,IAAS,EACJ,MAAA,GAGH,KAAA,GAAU,KAAK,IAAI,CAAI,EACvB,EAAS,KAAK,MAAM,CAAO,EAC3B,EAAS,IAAW,EAAM,GAAK,GAAG,EAAO,EAAI,IAAM,KAAK,KACxD,EAAW,EAAU,EAG3B,GAAI,IAAa,EACf,MAAO,GAAG,IAGZ,KAAM,GAAc,EAAqB,EAAO,KAAA,EAAS,EAIrD,GAAA,EAAY,EAAU,GAAI,EACrB,MAAA,GAAG,IAAc,EAAqB,SAAM,QAC1C,GAAA,EAAY,EAAU,GAAI,EAC5B,MAAA,GAAG,IAAc,EAAqB,SAAM,QAC1C,GAAA,EAAY,EAAU,EAAG,EAC3B,MAAA,GAAG,IAAc,EAAqB,SAAM,QAC1C,GAAA,EAAY,EAAU,EAAG,EAC3B,MAAA,GAAG,IAAc,EAAqB,SAAM,QAC1C,GAAA,EAAY,EAAU,EAAG,EAC3B,MAAA,GAAG,IAAc,EAAqB,SAAM,QAC1C,GAAA,EAAY,EAAU,EAAG,EAC3B,MAAA,GAAG,IAAc,EAAqB,SAAM,QAE3C,OAAA,OACD,MACI,MAAA,GAAG,IAAc,EAAqB,SAAM,YAChD,KACI,MAAA,GAAG,IAAc,EAAqB,OAAM,YAChD,MACI,MAAA,GAAG,IAAc,EAAqB,SAAM,YAChD,IACI,MAAA,GAAG,IAAc,EAAqB,OAAM,YAChD,MACI,MAAA,GAAG,IAAc,EAAqB,SAAM,YAChD,KACI,MAAA,GAAG,IAAc,EAAqB,OAAM,YAChD,MACI,MAAA,GAAG,IAAc,EAAqB,SAAM,QAIzD,MAAO,GAAG,GACZ"}
1
+ {"version":3,"file":"format-quantity.cjs.js","sources":["../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import type { VulgarFraction } from './types';\n\nexport const defaultTolerance = 0.009;\n\n/**\n * A map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToPlainMap: {\n [vf in VulgarFraction]: `${number}/${number}`;\n} = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n};\n\nexport const fractionDecimalMatches: [number, VulgarFraction][] = [\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];\n","import {\n defaultTolerance,\n fractionDecimalMatches,\n vulgarToPlainMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\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 * 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 = (qty, options) => {\n const dQty = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like\n if (isNaN(dQty) || dQty === null) {\n return null;\n }\n\n // Return an empty string if the value is zero\n if (dQty === 0) {\n return '';\n }\n\n const dQtyAbs = Math.abs(dQty);\n const iFloor = Math.floor(dQtyAbs);\n const sFloor = `${dQty < 0 ? '-' : ''}${iFloor === 0 ? '' : `${iFloor} `}`;\n const dDecimal = dQtyAbs - iFloor;\n\n // For integers just return the given value as a string\n if (dDecimal === 0) {\n return `${dQty}`;\n }\n\n const opts: FormatQuantityOptions =\n typeof options === 'boolean' ? { vulgarFractions: options } : options ?? {};\n\n const sFloorFinal = opts.vulgarFractions ? sFloor.trim() : sFloor;\n\n const getFraction = (vulgarFraction: VulgarFraction) =>\n opts.vulgarFractions\n ? vulgarFraction\n : opts.fractionSlash\n ? vulgarToPlainMap[vulgarFraction].replace('/', '')\n : vulgarToPlainMap[vulgarFraction];\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(dDecimal, num, opts.tolerance ?? defaultTolerance)) {\n return `${sFloorFinal}${getFraction(vf)}`;\n }\n }\n\n return `${dQty}`;\n};\n"],"names":[],"mappings":"4GAEO,KAAM,GAAmB,KAKnB,EAET,CACF,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,EAEa,EAAqD,CAChE,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,CACb,EClCM,EAAc,CAAC,EAAY,EAAY,IAC3C,KAAK,IAAI,EAAK,CAAE,EAAI,EAST,EAAiC,CAAC,EAAK,IAAY,OAC9D,KAAM,GAAO,MAAO,IAAQ,SAAW,WAAW,CAAG,EAAI,EAGzD,GAAI,MAAM,CAAI,GAAK,IAAS,KACnB,MAAA,MAIT,GAAI,IAAS,EACJ,MAAA,GAGH,KAAA,GAAU,KAAK,IAAI,CAAI,EACvB,EAAS,KAAK,MAAM,CAAO,EAC3B,EAAS,GAAG,EAAO,EAAI,IAAM,KAAK,IAAW,EAAI,GAAK,GAAG,OACzD,EAAW,EAAU,EAG3B,GAAI,IAAa,EACf,MAAO,GAAG,IAGN,KAAA,GACJ,MAAO,IAAY,UAAY,CAAE,gBAAiB,CAAA,EAAY,UAAW,GAErE,EAAc,EAAK,gBAAkB,EAAO,KAAS,EAAA,EAErD,EAAc,AAAC,GACnB,EAAK,gBACD,EACA,EAAK,cACL,EAAiB,GAAgB,QAAQ,IAAK,QAAG,EACjD,EAAiB,GAEZ,SAAA,CAAC,EAAK,IAAO,GACtB,GAAI,EAAY,EAAU,EAAK,KAAK,YAAL,OAAkB,CAAgB,EACxD,MAAA,GAAG,IAAc,EAAY,CAAE,IAI1C,MAAO,GAAG,GACZ"}
@@ -1,5 +1,47 @@
1
- const closeEnough = (a, b) => Math.abs(a - b) < 9e-3;
2
- function formatQuantity(qty, useVulgarFractions) {
1
+ const defaultTolerance = 9e-3;
2
+ const vulgarToPlainMap = {
3
+ "\xBC": "1/4",
4
+ "\xBD": "1/2",
5
+ "\xBE": "3/4",
6
+ "\u2150": "1/7",
7
+ "\u2151": "1/9",
8
+ "\u2152": "1/10",
9
+ "\u2153": "1/3",
10
+ "\u2154": "2/3",
11
+ "\u2155": "1/5",
12
+ "\u2156": "2/5",
13
+ "\u2157": "3/5",
14
+ "\u2158": "4/5",
15
+ "\u2159": "1/6",
16
+ "\u215A": "5/6",
17
+ "\u215B": "1/8",
18
+ "\u215C": "3/8",
19
+ "\u215D": "5/8",
20
+ "\u215E": "7/8"
21
+ };
22
+ const fractionDecimalMatches = [
23
+ [0.33, "\u2153"],
24
+ [0.66, "\u2154"],
25
+ [0.2, "\u2155"],
26
+ [0.4, "\u2156"],
27
+ [0.6, "\u2157"],
28
+ [0.8, "\u2158"],
29
+ [0.166, "\u2159"],
30
+ [0.833, "\u215A"],
31
+ [0.143, "\u2150"],
32
+ [0.111, "\u2151"],
33
+ [0.1, "\u2152"],
34
+ [0.125, "\u215B"],
35
+ [0.25, "\xBC"],
36
+ [0.375, "\u215C"],
37
+ [0.5, "\xBD"],
38
+ [0.625, "\u215D"],
39
+ [0.75, "\xBE"],
40
+ [0.875, "\u215E"]
41
+ ];
42
+ const closeEnough = (n1, n2, tolerance) => Math.abs(n1 - n2) < tolerance;
43
+ const formatQuantity = (qty, options) => {
44
+ var _a;
3
45
  const dQty = typeof qty === "string" ? parseFloat(qty) : qty;
4
46
  if (isNaN(dQty) || dQty === null) {
5
47
  return null;
@@ -9,43 +51,20 @@ function formatQuantity(qty, useVulgarFractions) {
9
51
  }
10
52
  const dQtyAbs = Math.abs(dQty);
11
53
  const iFloor = Math.floor(dQtyAbs);
12
- const sFloor = iFloor === 0 ? "" : `${dQty < 0 ? "-" : ""}${iFloor} `;
54
+ const sFloor = `${dQty < 0 ? "-" : ""}${iFloor === 0 ? "" : `${iFloor} `}`;
13
55
  const dDecimal = dQtyAbs - iFloor;
14
56
  if (dDecimal === 0) {
15
57
  return `${dQty}`;
16
58
  }
17
- const sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor;
18
- if (closeEnough(dDecimal, 0.33)) {
19
- return `${sFloorFinal}${useVulgarFractions ? "\u2153" : "1/3"}`;
20
- } else if (closeEnough(dDecimal, 0.66)) {
21
- return `${sFloorFinal}${useVulgarFractions ? "\u2154" : "2/3"}`;
22
- } else if (closeEnough(dDecimal, 0.2)) {
23
- return `${sFloorFinal}${useVulgarFractions ? "\u2155" : "1/5"}`;
24
- } else if (closeEnough(dDecimal, 0.4)) {
25
- return `${sFloorFinal}${useVulgarFractions ? "\u2156" : "2/5"}`;
26
- } else if (closeEnough(dDecimal, 0.6)) {
27
- return `${sFloorFinal}${useVulgarFractions ? "\u2157" : "3/5"}`;
28
- } else if (closeEnough(dDecimal, 0.8)) {
29
- return `${sFloorFinal}${useVulgarFractions ? "\u2158" : "4/5"}`;
30
- } else {
31
- switch (dDecimal) {
32
- case 0.125:
33
- return `${sFloorFinal}${useVulgarFractions ? "\u215B" : "1/8"}`;
34
- case 0.25:
35
- return `${sFloorFinal}${useVulgarFractions ? "\xBC" : "1/4"}`;
36
- case 0.375:
37
- return `${sFloorFinal}${useVulgarFractions ? "\u215C" : "3/8"}`;
38
- case 0.5:
39
- return `${sFloorFinal}${useVulgarFractions ? "\xBD" : "1/2"}`;
40
- case 0.625:
41
- return `${sFloorFinal}${useVulgarFractions ? "\u215D" : "5/8"}`;
42
- case 0.75:
43
- return `${sFloorFinal}${useVulgarFractions ? "\xBE" : "3/4"}`;
44
- case 0.875:
45
- return `${sFloorFinal}${useVulgarFractions ? "\u215E" : "7/8"}`;
59
+ const opts = typeof options === "boolean" ? { vulgarFractions: options } : options != null ? options : {};
60
+ const sFloorFinal = opts.vulgarFractions ? sFloor.trim() : sFloor;
61
+ const getFraction = (vulgarFraction) => opts.vulgarFractions ? vulgarFraction : opts.fractionSlash ? vulgarToPlainMap[vulgarFraction].replace("/", "\u2044") : vulgarToPlainMap[vulgarFraction];
62
+ for (const [num, vf] of fractionDecimalMatches) {
63
+ if (closeEnough(dDecimal, num, (_a = opts.tolerance) != null ? _a : defaultTolerance)) {
64
+ return `${sFloorFinal}${getFraction(vf)}`;
46
65
  }
47
66
  }
48
67
  return `${dQty}`;
49
- }
50
- export { formatQuantity as default };
68
+ };
69
+ export { formatQuantity as default, defaultTolerance, formatQuantity, fractionDecimalMatches, vulgarToPlainMap };
51
70
  //# sourceMappingURL=format-quantity.es.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"format-quantity.es.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Determines if two numbers are close enough to consider\n * them equal for our purposes\n */\nconst closeEnough = (a: number, b: number) => Math.abs(a - b) < 0.009;\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 fractions, e.g. \"½\",\n * pass `true` as the second argument.\n */\nfunction formatQuantity(qty: string | number, useVulgarFractions?: boolean) {\n const dQty = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Bomb out if not a number\n if (isNaN(dQty) || dQty === null) {\n return null;\n }\n\n // Return an empty string if the value is zero\n if (dQty === 0) {\n return '';\n }\n\n const dQtyAbs = Math.abs(dQty);\n const iFloor = Math.floor(dQtyAbs);\n const sFloor = iFloor === 0.0 ? '' : `${dQty < 0 ? '-' : ''}${iFloor} `;\n const dDecimal = dQtyAbs - iFloor;\n\n // Handle integers first. Just return the given value as a string.\n if (dDecimal === 0) {\n return `${dQty}`;\n }\n\n const sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor;\n\n // Handle infinitely repeating decimals next, since\n // we'll never get an exact match for a switch case:\n if (closeEnough(dDecimal, 0.33)) {\n return `${sFloorFinal}${useVulgarFractions ? '' : '1/3'}`;\n } else if (closeEnough(dDecimal, 0.66)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅔' : '2/3'}`;\n } else if (closeEnough(dDecimal, 0.2)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅕' : '1/5'}`;\n } else if (closeEnough(dDecimal, 0.4)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅖' : '2/5'}`;\n } else if (closeEnough(dDecimal, 0.6)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅗' : '3/5'}`;\n } else if (closeEnough(dDecimal, 0.8)) {\n return `${sFloorFinal}${useVulgarFractions ? '' : '4/5'}`;\n } else {\n switch (dDecimal) {\n case 0.125:\n return `${sFloorFinal}${useVulgarFractions ? '⅛' : '1/8'}`;\n case 0.25:\n return `${sFloorFinal}${useVulgarFractions ? '¼' : '1/4'}`;\n case 0.375:\n return `${sFloorFinal}${useVulgarFractions ? '⅜' : '3/8'}`;\n case 0.5:\n return `${sFloorFinal}${useVulgarFractions ? '½' : '1/2'}`;\n case 0.625:\n return `${sFloorFinal}${useVulgarFractions ? '⅝' : '5/8'}`;\n case 0.75:\n return `${sFloorFinal}${useVulgarFractions ? '¾' : '3/4'}`;\n case 0.875:\n return `${sFloorFinal}${useVulgarFractions ? '⅞' : '7/8'}`;\n }\n }\n\n return `${dQty}`;\n}\n\nexport default formatQuantity;\n"],"names":[],"mappings":"AAIA,MAAM,cAAc,CAAC,GAAW,MAAc,KAAK,IAAI,IAAI,CAAC,IAAI;AAQhE,wBAAwB,KAAsB,oBAA8B;AAC1E,QAAM,OAAO,OAAO,QAAQ,WAAW,WAAW,GAAG,IAAI;AAGzD,MAAI,MAAM,IAAI,KAAK,SAAS,MAAM;AACzB,WAAA;AAAA,EACT;AAGA,MAAI,SAAS,GAAG;AACP,WAAA;AAAA,EACT;AAEM,QAAA,UAAU,KAAK,IAAI,IAAI;AACvB,QAAA,SAAS,KAAK,MAAM,OAAO;AAC3B,QAAA,SAAS,WAAW,IAAM,KAAK,GAAG,OAAO,IAAI,MAAM,KAAK;AAC9D,QAAM,WAAW,UAAU;AAG3B,MAAI,aAAa,GAAG;AAClB,WAAO,GAAG;AAAA,EACZ;AAEA,QAAM,cAAc,qBAAqB,OAAO,KAAA,IAAS;AAIrD,MAAA,YAAY,UAAU,IAAI,GAAG;AACxB,WAAA,GAAG,cAAc,qBAAqB,WAAM;AAAA,EAC1C,WAAA,YAAY,UAAU,IAAI,GAAG;AAC/B,WAAA,GAAG,cAAc,qBAAqB,WAAM;AAAA,EAC1C,WAAA,YAAY,UAAU,GAAG,GAAG;AAC9B,WAAA,GAAG,cAAc,qBAAqB,WAAM;AAAA,EAC1C,WAAA,YAAY,UAAU,GAAG,GAAG;AAC9B,WAAA,GAAG,cAAc,qBAAqB,WAAM;AAAA,EAC1C,WAAA,YAAY,UAAU,GAAG,GAAG;AAC9B,WAAA,GAAG,cAAc,qBAAqB,WAAM;AAAA,EAC1C,WAAA,YAAY,UAAU,GAAG,GAAG;AAC9B,WAAA,GAAG,cAAc,qBAAqB,WAAM;AAAA,EAAA,OAC9C;AACG,YAAA;AAAA,WACD;AACI,eAAA,GAAG,cAAc,qBAAqB,WAAM;AAAA,WAChD;AACI,eAAA,GAAG,cAAc,qBAAqB,SAAM;AAAA,WAChD;AACI,eAAA,GAAG,cAAc,qBAAqB,WAAM;AAAA,WAChD;AACI,eAAA,GAAG,cAAc,qBAAqB,SAAM;AAAA,WAChD;AACI,eAAA,GAAG,cAAc,qBAAqB,WAAM;AAAA,WAChD;AACI,eAAA,GAAG,cAAc,qBAAqB,SAAM;AAAA,WAChD;AACI,eAAA,GAAG,cAAc,qBAAqB,WAAM;AAAA;AAAA,EAEzD;AAEA,SAAO,GAAG;AACZ;;"}
1
+ {"version":3,"file":"format-quantity.es.js","sources":["../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import type { VulgarFraction } from './types';\n\nexport const defaultTolerance = 0.009;\n\n/**\n * A map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToPlainMap: {\n [vf in VulgarFraction]: `${number}/${number}`;\n} = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n};\n\nexport const fractionDecimalMatches: [number, VulgarFraction][] = [\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];\n","import {\n defaultTolerance,\n fractionDecimalMatches,\n vulgarToPlainMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\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 * 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 = (qty, options) => {\n const dQty = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like\n if (isNaN(dQty) || dQty === null) {\n return null;\n }\n\n // Return an empty string if the value is zero\n if (dQty === 0) {\n return '';\n }\n\n const dQtyAbs = Math.abs(dQty);\n const iFloor = Math.floor(dQtyAbs);\n const sFloor = `${dQty < 0 ? '-' : ''}${iFloor === 0 ? '' : `${iFloor} `}`;\n const dDecimal = dQtyAbs - iFloor;\n\n // For integers just return the given value as a string\n if (dDecimal === 0) {\n return `${dQty}`;\n }\n\n const opts: FormatQuantityOptions =\n typeof options === 'boolean' ? { vulgarFractions: options } : options ?? {};\n\n const sFloorFinal = opts.vulgarFractions ? sFloor.trim() : sFloor;\n\n const getFraction = (vulgarFraction: VulgarFraction) =>\n opts.vulgarFractions\n ? vulgarFraction\n : opts.fractionSlash\n ? vulgarToPlainMap[vulgarFraction].replace('/', '')\n : vulgarToPlainMap[vulgarFraction];\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(dDecimal, num, opts.tolerance ?? defaultTolerance)) {\n return `${sFloorFinal}${getFraction(vf)}`;\n }\n }\n\n return `${dQty}`;\n};\n"],"names":[],"mappings":"AAEO,MAAM,mBAAmB;AAKzB,MAAM,mBAET;AAAA,EACF,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,MAAM,yBAAqD;AAAA,EAChE,CAAC,MAAM,QAAG;AAAA,EACV,CAAC,MAAM,QAAG;AAAA,EACV,CAAC,KAAK,QAAG;AAAA,EACT,CAAC,KAAK,QAAG;AAAA,EACT,CAAC,KAAK,QAAG;AAAA,EACT,CAAC,KAAK,QAAG;AAAA,EACT,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,KAAK,QAAG;AAAA,EACT,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,MAAM,MAAG;AAAA,EACV,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,KAAK,MAAG;AAAA,EACT,CAAC,OAAO,QAAG;AAAA,EACX,CAAC,MAAM,MAAG;AAAA,EACV,CAAC,OAAO,QAAG;AACb;AClCA,MAAM,cAAc,CAAC,IAAY,IAAY,cAC3C,KAAK,IAAI,KAAK,EAAE,IAAI;AAST,MAAA,iBAAiC,CAAC,KAAK,YAAY;ADvBzD;ACwBL,QAAM,OAAO,OAAO,QAAQ,WAAW,WAAW,GAAG,IAAI;AAGzD,MAAI,MAAM,IAAI,KAAK,SAAS,MAAM;AACzB,WAAA;AAAA,EACT;AAGA,MAAI,SAAS,GAAG;AACP,WAAA;AAAA,EACT;AAEM,QAAA,UAAU,KAAK,IAAI,IAAI;AACvB,QAAA,SAAS,KAAK,MAAM,OAAO;AAC3B,QAAA,SAAS,GAAG,OAAO,IAAI,MAAM,KAAK,WAAW,IAAI,KAAK,GAAG;AAC/D,QAAM,WAAW,UAAU;AAG3B,MAAI,aAAa,GAAG;AAClB,WAAO,GAAG;AAAA,EACZ;AAEM,QAAA,OACJ,OAAO,YAAY,YAAY,EAAE,iBAAiB,QAAA,IAAY,4BAAW;AAE3E,QAAM,cAAc,KAAK,kBAAkB,OAAO,KAAS,IAAA;AAE3D,QAAM,cAAc,CAAC,mBACnB,KAAK,kBACD,iBACA,KAAK,gBACL,iBAAiB,gBAAgB,QAAQ,KAAK,QAAG,IACjD,iBAAiB;AAEZ,aAAA,CAAC,KAAK,OAAO,wBAAwB;AAC9C,QAAI,YAAY,UAAU,KAAK,WAAK,cAAL,YAAkB,gBAAgB,GAAG;AAC3D,aAAA,GAAG,cAAc,YAAY,EAAE;AAAA,IACxC;AAAA,EACF;AAEA,SAAO,GAAG;AACZ;;"}
@@ -1,2 +1,2 @@
1
- (function(n,r){typeof exports=="object"&&typeof module!="undefined"?module.exports=r():typeof define=="function"&&define.amd?define(r):(n=typeof globalThis!="undefined"?globalThis:n||self,n.formatQuantity=r())})(this,function(){"use strict";const n=(o,e)=>Math.abs(o-e)<.009;function r(o,e){const f=typeof o=="string"?parseFloat(o):o;if(isNaN(f)||f===null)return null;if(f===0)return"";const u=Math.abs(f),i=Math.floor(u),d=i===0?"":`${f<0?"-":""}${i} `,$=u-i;if($===0)return`${f}`;const t=e?d.trim():d;if(n($,.33))return`${t}${e?"\u2153":"1/3"}`;if(n($,.66))return`${t}${e?"\u2154":"2/3"}`;if(n($,.2))return`${t}${e?"\u2155":"1/5"}`;if(n($,.4))return`${t}${e?"\u2156":"2/5"}`;if(n($,.6))return`${t}${e?"\u2157":"3/5"}`;if(n($,.8))return`${t}${e?"\u2158":"4/5"}`;switch($){case .125:return`${t}${e?"\u215B":"1/8"}`;case .25:return`${t}${e?"\xBC":"1/4"}`;case .375:return`${t}${e?"\u215C":"3/8"}`;case .5:return`${t}${e?"\xBD":"1/2"}`;case .625:return`${t}${e?"\u215D":"5/8"}`;case .75:return`${t}${e?"\xBE":"3/4"}`;case .875:return`${t}${e?"\u215E":"7/8"}`}return`${f}`}return r});
1
+ (function(u,o){typeof exports=="object"&&typeof module!="undefined"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(u=typeof globalThis!="undefined"?globalThis:u||self,o(u.FormatQuantity={}))})(this,function(u){"use strict";const r={"\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"},f=[[.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"]],M=(n,t,e)=>Math.abs(n-t)<e,i=(n,t)=>{var h;const e=typeof n=="string"?parseFloat(n):n;if(isNaN(e)||e===null)return null;if(e===0)return"";const s=Math.abs(e),c=Math.floor(s),d=`${e<0?"-":""}${c===0?"":`${c} `}`,m=s-c;if(m===0)return`${e}`;const l=typeof t=="boolean"?{vulgarFractions:t}:t!=null?t:{},T=l.vulgarFractions?d.trim():d,g=a=>l.vulgarFractions?a:l.fractionSlash?r[a].replace("/","\u2044"):r[a];for(const[a,y]of f)if(M(m,a,(h=l.tolerance)!=null?h:.009))return`${T}${g(y)}`;return`${e}`};u.default=i,u.defaultTolerance=.009,u.formatQuantity=i,u.fractionDecimalMatches=f,u.vulgarToPlainMap=r,Object.defineProperties(u,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
2
2
  //# sourceMappingURL=format-quantity.umd.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"format-quantity.umd.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Determines if two numbers are close enough to consider\n * them equal for our purposes\n */\nconst closeEnough = (a: number, b: number) => Math.abs(a - b) < 0.009;\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 fractions, e.g. \"½\",\n * pass `true` as the second argument.\n */\nfunction formatQuantity(qty: string | number, useVulgarFractions?: boolean) {\n const dQty = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Bomb out if not a number\n if (isNaN(dQty) || dQty === null) {\n return null;\n }\n\n // Return an empty string if the value is zero\n if (dQty === 0) {\n return '';\n }\n\n const dQtyAbs = Math.abs(dQty);\n const iFloor = Math.floor(dQtyAbs);\n const sFloor = iFloor === 0.0 ? '' : `${dQty < 0 ? '-' : ''}${iFloor} `;\n const dDecimal = dQtyAbs - iFloor;\n\n // Handle integers first. Just return the given value as a string.\n if (dDecimal === 0) {\n return `${dQty}`;\n }\n\n const sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor;\n\n // Handle infinitely repeating decimals next, since\n // we'll never get an exact match for a switch case:\n if (closeEnough(dDecimal, 0.33)) {\n return `${sFloorFinal}${useVulgarFractions ? '' : '1/3'}`;\n } else if (closeEnough(dDecimal, 0.66)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅔' : '2/3'}`;\n } else if (closeEnough(dDecimal, 0.2)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅕' : '1/5'}`;\n } else if (closeEnough(dDecimal, 0.4)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅖' : '2/5'}`;\n } else if (closeEnough(dDecimal, 0.6)) {\n return `${sFloorFinal}${useVulgarFractions ? '⅗' : '3/5'}`;\n } else if (closeEnough(dDecimal, 0.8)) {\n return `${sFloorFinal}${useVulgarFractions ? '' : '4/5'}`;\n } else {\n switch (dDecimal) {\n case 0.125:\n return `${sFloorFinal}${useVulgarFractions ? '⅛' : '1/8'}`;\n case 0.25:\n return `${sFloorFinal}${useVulgarFractions ? '¼' : '1/4'}`;\n case 0.375:\n return `${sFloorFinal}${useVulgarFractions ? '⅜' : '3/8'}`;\n case 0.5:\n return `${sFloorFinal}${useVulgarFractions ? '½' : '1/2'}`;\n case 0.625:\n return `${sFloorFinal}${useVulgarFractions ? '⅝' : '5/8'}`;\n case 0.75:\n return `${sFloorFinal}${useVulgarFractions ? '¾' : '3/4'}`;\n case 0.875:\n return `${sFloorFinal}${useVulgarFractions ? '⅞' : '7/8'}`;\n }\n }\n\n return `${dQty}`;\n}\n\nexport default formatQuantity;\n"],"names":[],"mappings":"iPAIA,KAAM,GAAc,CAAC,EAAW,IAAc,KAAK,IAAI,EAAI,CAAC,EAAI,KAQhE,WAAwB,EAAsB,EAA8B,CAC1E,KAAM,GAAO,MAAO,IAAQ,SAAW,WAAW,CAAG,EAAI,EAGzD,GAAI,MAAM,CAAI,GAAK,IAAS,KACnB,MAAA,MAIT,GAAI,IAAS,EACJ,MAAA,GAGH,KAAA,GAAU,KAAK,IAAI,CAAI,EACvB,EAAS,KAAK,MAAM,CAAO,EAC3B,EAAS,IAAW,EAAM,GAAK,GAAG,EAAO,EAAI,IAAM,KAAK,KACxD,EAAW,EAAU,EAG3B,GAAI,IAAa,EACf,MAAO,GAAG,IAGZ,KAAM,GAAc,EAAqB,EAAO,KAAA,EAAS,EAIrD,GAAA,EAAY,EAAU,GAAI,EACrB,MAAA,GAAG,IAAc,EAAqB,SAAM,QAC1C,GAAA,EAAY,EAAU,GAAI,EAC5B,MAAA,GAAG,IAAc,EAAqB,SAAM,QAC1C,GAAA,EAAY,EAAU,EAAG,EAC3B,MAAA,GAAG,IAAc,EAAqB,SAAM,QAC1C,GAAA,EAAY,EAAU,EAAG,EAC3B,MAAA,GAAG,IAAc,EAAqB,SAAM,QAC1C,GAAA,EAAY,EAAU,EAAG,EAC3B,MAAA,GAAG,IAAc,EAAqB,SAAM,QAC1C,GAAA,EAAY,EAAU,EAAG,EAC3B,MAAA,GAAG,IAAc,EAAqB,SAAM,QAE3C,OAAA,OACD,MACI,MAAA,GAAG,IAAc,EAAqB,SAAM,YAChD,KACI,MAAA,GAAG,IAAc,EAAqB,OAAM,YAChD,MACI,MAAA,GAAG,IAAc,EAAqB,SAAM,YAChD,IACI,MAAA,GAAG,IAAc,EAAqB,OAAM,YAChD,MACI,MAAA,GAAG,IAAc,EAAqB,SAAM,YAChD,KACI,MAAA,GAAG,IAAc,EAAqB,OAAM,YAChD,MACI,MAAA,GAAG,IAAc,EAAqB,SAAM,QAIzD,MAAO,GAAG,GACZ"}
1
+ {"version":3,"file":"format-quantity.umd.js","sources":["../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import type { VulgarFraction } from './types';\n\nexport const defaultTolerance = 0.009;\n\n/**\n * A map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToPlainMap: {\n [vf in VulgarFraction]: `${number}/${number}`;\n} = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n};\n\nexport const fractionDecimalMatches: [number, VulgarFraction][] = [\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];\n","import {\n defaultTolerance,\n fractionDecimalMatches,\n vulgarToPlainMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\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 * 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 = (qty, options) => {\n const dQty = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like\n if (isNaN(dQty) || dQty === null) {\n return null;\n }\n\n // Return an empty string if the value is zero\n if (dQty === 0) {\n return '';\n }\n\n const dQtyAbs = Math.abs(dQty);\n const iFloor = Math.floor(dQtyAbs);\n const sFloor = `${dQty < 0 ? '-' : ''}${iFloor === 0 ? '' : `${iFloor} `}`;\n const dDecimal = dQtyAbs - iFloor;\n\n // For integers just return the given value as a string\n if (dDecimal === 0) {\n return `${dQty}`;\n }\n\n const opts: FormatQuantityOptions =\n typeof options === 'boolean' ? { vulgarFractions: options } : options ?? {};\n\n const sFloorFinal = opts.vulgarFractions ? sFloor.trim() : sFloor;\n\n const getFraction = (vulgarFraction: VulgarFraction) =>\n opts.vulgarFractions\n ? vulgarFraction\n : opts.fractionSlash\n ? vulgarToPlainMap[vulgarFraction].replace('/', '')\n : vulgarToPlainMap[vulgarFraction];\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(dDecimal, num, opts.tolerance ?? defaultTolerance)) {\n return `${sFloorFinal}${getFraction(vf)}`;\n }\n }\n\n return `${dQty}`;\n};\n"],"names":[],"mappings":"wPAOO,KAAM,GAET,CACF,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,EAEa,EAAqD,CAChE,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,CACb,EClCM,EAAc,CAAC,EAAY,EAAY,IAC3C,KAAK,IAAI,EAAK,CAAE,EAAI,EAST,EAAiC,CAAC,EAAK,IAAY,OAC9D,KAAM,GAAO,MAAO,IAAQ,SAAW,WAAW,CAAG,EAAI,EAGzD,GAAI,MAAM,CAAI,GAAK,IAAS,KACnB,MAAA,MAIT,GAAI,IAAS,EACJ,MAAA,GAGH,KAAA,GAAU,KAAK,IAAI,CAAI,EACvB,EAAS,KAAK,MAAM,CAAO,EAC3B,EAAS,GAAG,EAAO,EAAI,IAAM,KAAK,IAAW,EAAI,GAAK,GAAG,OACzD,EAAW,EAAU,EAG3B,GAAI,IAAa,EACf,MAAO,GAAG,IAGN,KAAA,GACJ,MAAO,IAAY,UAAY,CAAE,gBAAiB,CAAA,EAAY,UAAW,GAErE,EAAc,EAAK,gBAAkB,EAAO,KAAS,EAAA,EAErD,EAAc,AAAC,GACnB,EAAK,gBACD,EACA,EAAK,cACL,EAAiB,GAAgB,QAAQ,IAAK,QAAG,EACjD,EAAiB,GAEZ,SAAA,CAAC,EAAK,IAAO,GACtB,GAAI,EAAY,EAAU,EAAK,KAAK,YAAL,OAAkB,IAAgB,EACxD,MAAA,GAAG,IAAc,EAAY,CAAE,IAI1C,MAAO,GAAG,GACZ"}
@@ -0,0 +1,9 @@
1
+ import type { FormatQuantity } from './types';
2
+ /**
3
+ * Formats a number (or string that appears to be a number)
4
+ * as one would see it written in imperial measurements, e.g.
5
+ * "1 1/2" instead of "1.5". To use vulgar fraction characters
6
+ * like "½", pass `true` as the second argument. For other options
7
+ * see the [documentation](https://jakeboone02.github.io/format-quantity/).
8
+ */
9
+ export declare const formatQuantity: FormatQuantity;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,5 @@
1
- /**
2
- * Formats a number (or string that appears to be a number)
3
- * as one would see it written in imperial measurements, e.g.
4
- * "1 1/2" instead of "1.5". To use vulgar fractions, e.g. "½",
5
- * pass `true` as the second argument.
6
- */
7
- declare function formatQuantity(qty: string | number, useVulgarFractions?: boolean): string | null;
1
+ import { formatQuantity } from './formatQuantity';
2
+ export * from './constants';
3
+ export * from './types';
4
+ export { formatQuantity };
8
5
  export default formatQuantity;
@@ -0,0 +1,33 @@
1
+ export 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.009
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
+ export declare type FormatQuantity = (qty: string | number, options?: boolean | FormatQuantityOptions) => string | null;
25
+ export declare type VulgarFraction = '¼' | '½' | '¾' | '⅐' | '⅑' | '⅒' | '⅓' | '⅔' | '⅕' | '⅖' | '⅗' | '⅘' | '⅙' | '⅚' | '⅛' | '⅜' | '⅝' | '⅞';
26
+ export declare type FormatQuantityTests = [
27
+ string,
28
+ ([Parameters<FormatQuantity>[0], ReturnType<FormatQuantity>] | [
29
+ Parameters<FormatQuantity>[0],
30
+ ReturnType<FormatQuantity>,
31
+ Parameters<FormatQuantity>[1]
32
+ ])[]
33
+ ][];
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.2",
2
+ "version": "1.1.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",
@@ -17,7 +17,9 @@
17
17
  "quantity",
18
18
  "number",
19
19
  "format",
20
- "string"
20
+ "string",
21
+ "fractions",
22
+ "imperial"
21
23
  ],
22
24
  "bugs": {
23
25
  "url": "https://github.com/jakeboone02/format-quantity/issues"
@@ -27,13 +29,13 @@
27
29
  "type": "git",
28
30
  "url": "https://github.com/jakeboone02/format-quantity"
29
31
  },
30
- "engines": {
31
- "node": ">=10"
32
- },
33
32
  "scripts": {
34
33
  "start": "vite",
35
- "build": "vite build && tsc",
34
+ "build": "yarn build:main && yarn build:types",
35
+ "build:main": "vite build",
36
+ "build:types": "tsc --project ./tsconfig.build.json",
36
37
  "test": "jest --coverage",
38
+ "watch": "jest --watch",
37
39
  "publish:npm": "np"
38
40
  },
39
41
  "devDependencies": {