format-quantity 0.6.1 → 1.0.2

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,11 @@
1
+ ## 1.0.1 (2021-02-15)
2
+
3
+ - Added description to package.json
4
+
5
+ ## 1.0.0 (2021-02-11)
6
+
7
+ - New build system ([tsdx](https://tsdx.io/))
8
+
1
9
  ## 0.6.0 (2019-08-31)
2
10
 
3
11
  - Added ability to produce unicode vulgar fractions (pass `true` as the second argument)
package/LICENSE CHANGED
@@ -1,4 +1,6 @@
1
- Copyright (c) 2015 Jake Boone
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Jake Boone
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
6
  of this software and associated documentation files (the "Software"), to deal
@@ -7,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
9
  copies of the Software, and to permit persons to whom the Software is
8
10
  furnished to do so, subject to the following conditions:
9
11
 
10
- The above copyright notice and this permission notice shall be included in
11
- all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
12
14
 
13
15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
16
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
17
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- THE SOFTWARE.
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -8,19 +8,17 @@
8
8
 
9
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.
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).
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
 
15
15
  ### npm
16
16
 
17
- ```
17
+ ```shell
18
+ # npm
18
19
  npm i format-quantity
19
- ```
20
20
 
21
- or
22
-
23
- ```
21
+ # yarn
24
22
  yarn add format-quantity
25
23
  ```
26
24
 
@@ -29,7 +27,7 @@ yarn add format-quantity
29
27
  In the browser, available as a global function `formatQuantity`.
30
28
 
31
29
  ```html
32
- <script src="path/to/format-quantity.umd.js"></script>
30
+ <script src="https://unpkg.com/format-quantity"></script>
33
31
  <script>
34
32
  console.log(formatQuantity(10.5)); // "10 1/2"
35
33
  </script>
@@ -1,74 +1,2 @@
1
- 'use strict';
2
-
3
- /**
4
- * Determines if two numbers are close enough to consider
5
- * them equal for our purposes
6
- */
7
- var closeEnough = function (a, b) { return Math.abs(a - b) < 0.009; };
8
- /**
9
- * Formats a number (or string that appears to be a number)
10
- * as one would see it written in imperial measurements, e.g.
11
- * "1 1/2" instead of "1.5". To use vulgar fractions, e.g. "½",
12
- * pass `true` as the second argument.
13
- */
14
- function formatQuantity(qty, useVulgarFractions) {
15
- var dQty = typeof qty === 'string' ? parseFloat(qty) : qty;
16
- // Bomb out if not a number
17
- if (isNaN(dQty) || dQty === null) {
18
- return null;
19
- }
20
- // Return an empty string if the value is zero
21
- if (dQty === 0) {
22
- return '';
23
- }
24
- var dQtyAbs = Math.abs(dQty);
25
- var iFloor = Math.floor(dQtyAbs);
26
- var sFloor = iFloor === 0.0 ? '' : "" + (dQty < 0 ? '-' : '') + iFloor + " ";
27
- var dDecimal = dQtyAbs - iFloor;
28
- // Handle integers first. Just return the given value as a string.
29
- if (dDecimal === 0) {
30
- return "" + dQty;
31
- }
32
- var sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor;
33
- // Handle infinitely repeating decimals next, since
34
- // we'll never get an exact match for a switch case:
35
- if (closeEnough(dDecimal, 0.33)) {
36
- return "" + sFloorFinal + (useVulgarFractions ? '⅓' : '1/3');
37
- }
38
- else if (closeEnough(dDecimal, 0.66)) {
39
- return "" + sFloorFinal + (useVulgarFractions ? '⅔' : '2/3');
40
- }
41
- else if (closeEnough(dDecimal, 0.2)) {
42
- return "" + sFloorFinal + (useVulgarFractions ? '⅕' : '1/5');
43
- }
44
- else if (closeEnough(dDecimal, 0.4)) {
45
- return "" + sFloorFinal + (useVulgarFractions ? '⅖' : '2/5');
46
- }
47
- else if (closeEnough(dDecimal, 0.6)) {
48
- return "" + sFloorFinal + (useVulgarFractions ? '⅗' : '3/5');
49
- }
50
- else if (closeEnough(dDecimal, 0.8)) {
51
- return "" + sFloorFinal + (useVulgarFractions ? '⅘' : '4/5');
52
- }
53
- else {
54
- switch (dDecimal) {
55
- case 0.125:
56
- return "" + sFloorFinal + (useVulgarFractions ? '⅛' : '1/8');
57
- case 0.25:
58
- return "" + sFloorFinal + (useVulgarFractions ? '¼' : '1/4');
59
- case 0.375:
60
- return "" + sFloorFinal + (useVulgarFractions ? '⅜' : '3/8');
61
- case 0.5:
62
- return "" + sFloorFinal + (useVulgarFractions ? '½' : '1/2');
63
- case 0.625:
64
- return "" + sFloorFinal + (useVulgarFractions ? '⅝' : '5/8');
65
- case 0.75:
66
- return "" + sFloorFinal + (useVulgarFractions ? '¾' : '3/4');
67
- case 0.875:
68
- return "" + sFloorFinal + (useVulgarFractions ? '⅞' : '7/8');
69
- }
70
- }
71
- return "" + dQty;
72
- }
73
-
74
- module.exports = formatQuantity;
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;
2
+ //# sourceMappingURL=format-quantity.cjs.js.map
@@ -0,0 +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"}
@@ -0,0 +1,51 @@
1
+ const closeEnough = (a, b) => Math.abs(a - b) < 9e-3;
2
+ function formatQuantity(qty, useVulgarFractions) {
3
+ const dQty = typeof qty === "string" ? parseFloat(qty) : qty;
4
+ if (isNaN(dQty) || dQty === null) {
5
+ return null;
6
+ }
7
+ if (dQty === 0) {
8
+ return "";
9
+ }
10
+ const dQtyAbs = Math.abs(dQty);
11
+ const iFloor = Math.floor(dQtyAbs);
12
+ const sFloor = iFloor === 0 ? "" : `${dQty < 0 ? "-" : ""}${iFloor} `;
13
+ const dDecimal = dQtyAbs - iFloor;
14
+ if (dDecimal === 0) {
15
+ return `${dQty}`;
16
+ }
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"}`;
46
+ }
47
+ }
48
+ return `${dQty}`;
49
+ }
50
+ export { formatQuantity as default };
51
+ //# sourceMappingURL=format-quantity.es.js.map
@@ -0,0 +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,80 +1,2 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.formatQuantity = factory());
5
- }(this, (function () { 'use strict';
6
-
7
- /**
8
- * Determines if two numbers are close enough to consider
9
- * them equal for our purposes
10
- */
11
- var closeEnough = function (a, b) { return Math.abs(a - b) < 0.009; };
12
- /**
13
- * Formats a number (or string that appears to be a number)
14
- * as one would see it written in imperial measurements, e.g.
15
- * "1 1/2" instead of "1.5". To use vulgar fractions, e.g. "½",
16
- * pass `true` as the second argument.
17
- */
18
- function formatQuantity(qty, useVulgarFractions) {
19
- var dQty = typeof qty === 'string' ? parseFloat(qty) : qty;
20
- // Bomb out if not a number
21
- if (isNaN(dQty) || dQty === null) {
22
- return null;
23
- }
24
- // Return an empty string if the value is zero
25
- if (dQty === 0) {
26
- return '';
27
- }
28
- var dQtyAbs = Math.abs(dQty);
29
- var iFloor = Math.floor(dQtyAbs);
30
- var sFloor = iFloor === 0.0 ? '' : "" + (dQty < 0 ? '-' : '') + iFloor + " ";
31
- var dDecimal = dQtyAbs - iFloor;
32
- // Handle integers first. Just return the given value as a string.
33
- if (dDecimal === 0) {
34
- return "" + dQty;
35
- }
36
- var sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor;
37
- // Handle infinitely repeating decimals next, since
38
- // we'll never get an exact match for a switch case:
39
- if (closeEnough(dDecimal, 0.33)) {
40
- return "" + sFloorFinal + (useVulgarFractions ? '⅓' : '1/3');
41
- }
42
- else if (closeEnough(dDecimal, 0.66)) {
43
- return "" + sFloorFinal + (useVulgarFractions ? '⅔' : '2/3');
44
- }
45
- else if (closeEnough(dDecimal, 0.2)) {
46
- return "" + sFloorFinal + (useVulgarFractions ? '⅕' : '1/5');
47
- }
48
- else if (closeEnough(dDecimal, 0.4)) {
49
- return "" + sFloorFinal + (useVulgarFractions ? '⅖' : '2/5');
50
- }
51
- else if (closeEnough(dDecimal, 0.6)) {
52
- return "" + sFloorFinal + (useVulgarFractions ? '⅗' : '3/5');
53
- }
54
- else if (closeEnough(dDecimal, 0.8)) {
55
- return "" + sFloorFinal + (useVulgarFractions ? '⅘' : '4/5');
56
- }
57
- else {
58
- switch (dDecimal) {
59
- case 0.125:
60
- return "" + sFloorFinal + (useVulgarFractions ? '⅛' : '1/8');
61
- case 0.25:
62
- return "" + sFloorFinal + (useVulgarFractions ? '¼' : '1/4');
63
- case 0.375:
64
- return "" + sFloorFinal + (useVulgarFractions ? '⅜' : '3/8');
65
- case 0.5:
66
- return "" + sFloorFinal + (useVulgarFractions ? '½' : '1/2');
67
- case 0.625:
68
- return "" + sFloorFinal + (useVulgarFractions ? '⅝' : '5/8');
69
- case 0.75:
70
- return "" + sFloorFinal + (useVulgarFractions ? '¾' : '3/4');
71
- case 0.875:
72
- return "" + sFloorFinal + (useVulgarFractions ? '⅞' : '7/8');
73
- }
74
- }
75
- return "" + dQty;
76
- }
77
-
78
- return formatQuantity;
79
-
80
- })));
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});
2
+ //# sourceMappingURL=format-quantity.umd.js.map
@@ -0,0 +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,8 +1,8 @@
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;
8
- export default formatQuantity;
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;
8
+ export default formatQuantity;
package/package.json CHANGED
@@ -1,25 +1,16 @@
1
1
  {
2
+ "version": "1.0.2",
2
3
  "name": "format-quantity",
3
- "version": "0.6.1",
4
- "description": "Format a number as an integer plus fraction, as seen in recipe ingredient lists",
5
- "repository": {
6
- "type": "git",
7
- "url": "https://github.com/jakeboone02/format-quantity"
8
- },
9
- "main": "dist/format-quantity.cjs.js",
10
- "module": "dist/format-quantity.esm.js",
11
- "browser": "dist/format-quantity.umd.js",
12
- "types": "dist/format-quantity.d.ts",
4
+ "author": "Jake Boone <jakeboone02@gmail.com>",
5
+ "description": "Number formatter for imperial measurements with support for vulgar fractions",
13
6
  "files": [
14
7
  "dist"
15
8
  ],
16
- "scripts": {
17
- "build": "rollup -c",
18
- "dev": "rollup -c -w",
19
- "pretty-print": "npx prettier --write ./src/**/*",
20
- "publish:npm": "np",
21
- "test": "jest --coverage"
22
- },
9
+ "main": "./dist/format-quantity.cjs.js",
10
+ "module": "./dist/format-quantity.es.js",
11
+ "unpkg": "./dist/format-quantity.umd.js",
12
+ "typings": "dist/index.d.ts",
13
+ "license": "MIT",
23
14
  "keywords": [
24
15
  "recipe",
25
16
  "ingredient",
@@ -28,23 +19,33 @@
28
19
  "format",
29
20
  "string"
30
21
  ],
31
- "author": "Jake Boone <jakeboone02@gmail.com>",
32
- "license": "MIT",
33
22
  "bugs": {
34
23
  "url": "https://github.com/jakeboone02/format-quantity/issues"
35
24
  },
36
25
  "homepage": "https://github.com/jakeboone02/format-quantity",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/jakeboone02/format-quantity"
29
+ },
30
+ "engines": {
31
+ "node": ">=10"
32
+ },
33
+ "scripts": {
34
+ "start": "vite",
35
+ "build": "vite build && tsc",
36
+ "test": "jest --coverage",
37
+ "publish:npm": "np"
38
+ },
37
39
  "devDependencies": {
38
- "@babel/core": "^7.5.5",
39
- "@babel/preset-env": "^7.5.5",
40
- "@babel/preset-typescript": "^7.3.3",
41
- "@types/jest": "^26.0.15",
42
- "babel-jest": "^26.0.1",
43
- "jest": "^26.0.1",
44
- "np": "^7.0.0",
45
- "prettier": "2.2.1",
46
- "rollup": "^2.13.1",
47
- "rollup-plugin-typescript2": "^0.29.0",
48
- "typescript": "^4.0.5"
40
+ "@babel/core": "^7.17.8",
41
+ "@babel/preset-env": "^7.16.11",
42
+ "@babel/preset-typescript": "^7.16.7",
43
+ "@types/jest": "^27.4.1",
44
+ "gh-pages": "^3.1.0",
45
+ "jest": "^27.5.1",
46
+ "np": "^7.3.0",
47
+ "prettier": "^2.6.0",
48
+ "typescript": "^4.1.5",
49
+ "vite": "^2.8.6"
49
50
  }
50
51
  }
@@ -1,72 +0,0 @@
1
- /**
2
- * Determines if two numbers are close enough to consider
3
- * them equal for our purposes
4
- */
5
- var closeEnough = function (a, b) { return Math.abs(a - b) < 0.009; };
6
- /**
7
- * Formats a number (or string that appears to be a number)
8
- * as one would see it written in imperial measurements, e.g.
9
- * "1 1/2" instead of "1.5". To use vulgar fractions, e.g. "½",
10
- * pass `true` as the second argument.
11
- */
12
- function formatQuantity(qty, useVulgarFractions) {
13
- var dQty = typeof qty === 'string' ? parseFloat(qty) : qty;
14
- // Bomb out if not a number
15
- if (isNaN(dQty) || dQty === null) {
16
- return null;
17
- }
18
- // Return an empty string if the value is zero
19
- if (dQty === 0) {
20
- return '';
21
- }
22
- var dQtyAbs = Math.abs(dQty);
23
- var iFloor = Math.floor(dQtyAbs);
24
- var sFloor = iFloor === 0.0 ? '' : "" + (dQty < 0 ? '-' : '') + iFloor + " ";
25
- var dDecimal = dQtyAbs - iFloor;
26
- // Handle integers first. Just return the given value as a string.
27
- if (dDecimal === 0) {
28
- return "" + dQty;
29
- }
30
- var sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor;
31
- // Handle infinitely repeating decimals next, since
32
- // we'll never get an exact match for a switch case:
33
- if (closeEnough(dDecimal, 0.33)) {
34
- return "" + sFloorFinal + (useVulgarFractions ? '⅓' : '1/3');
35
- }
36
- else if (closeEnough(dDecimal, 0.66)) {
37
- return "" + sFloorFinal + (useVulgarFractions ? '⅔' : '2/3');
38
- }
39
- else if (closeEnough(dDecimal, 0.2)) {
40
- return "" + sFloorFinal + (useVulgarFractions ? '⅕' : '1/5');
41
- }
42
- else if (closeEnough(dDecimal, 0.4)) {
43
- return "" + sFloorFinal + (useVulgarFractions ? '⅖' : '2/5');
44
- }
45
- else if (closeEnough(dDecimal, 0.6)) {
46
- return "" + sFloorFinal + (useVulgarFractions ? '⅗' : '3/5');
47
- }
48
- else if (closeEnough(dDecimal, 0.8)) {
49
- return "" + sFloorFinal + (useVulgarFractions ? '⅘' : '4/5');
50
- }
51
- else {
52
- switch (dDecimal) {
53
- case 0.125:
54
- return "" + sFloorFinal + (useVulgarFractions ? '⅛' : '1/8');
55
- case 0.25:
56
- return "" + sFloorFinal + (useVulgarFractions ? '¼' : '1/4');
57
- case 0.375:
58
- return "" + sFloorFinal + (useVulgarFractions ? '⅜' : '3/8');
59
- case 0.5:
60
- return "" + sFloorFinal + (useVulgarFractions ? '½' : '1/2');
61
- case 0.625:
62
- return "" + sFloorFinal + (useVulgarFractions ? '⅝' : '5/8');
63
- case 0.75:
64
- return "" + sFloorFinal + (useVulgarFractions ? '¾' : '3/4');
65
- case 0.875:
66
- return "" + sFloorFinal + (useVulgarFractions ? '⅞' : '7/8');
67
- }
68
- }
69
- return "" + dQty;
70
- }
71
-
72
- export default formatQuantity;
@@ -1 +0,0 @@
1
- export {};