format-quantity
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -0,0 +0,0 @@ { |
132
index.js
(function (global, factory) { | ||
if (typeof exports === "object" && typeof module !== "undefined" && module.exports) { | ||
module.exports = factory(); | ||
} else if (typeof define === "function" && define.amd) { | ||
define(factory); | ||
} else { | ||
global.formatQuantity = factory(); | ||
} | ||
}(this, function() { | ||
if (typeof exports === "object" && typeof module !== "undefined" && module.exports) { | ||
module.exports = factory(); | ||
} else if (typeof define === "function" && define.amd) { | ||
define(factory); | ||
} else { | ||
global.formatQuantity = factory(); | ||
} | ||
}(this, function () { | ||
var closeEnough = function(a, b) { | ||
return Math.abs(a - b) < 0.009; | ||
}; | ||
var closeEnough = function (a, b) { | ||
return Math.abs(a - b) < 0.009; | ||
}; | ||
var formatQuantity = function(sInQty) { | ||
var formatQuantity = function (sInQty) { | ||
sQty = sInQty; | ||
sQty = sInQty; | ||
// bomb out if not a number | ||
if (isNaN(sQty)) { | ||
// bomb out if not a number | ||
if (isNaN(sQty)) { | ||
return "-1"; | ||
} | ||
return "-1"; | ||
} | ||
var dQty = sQty - 0; | ||
var dQty = sQty - 0; | ||
if (dQty === 0) { | ||
if (dQty === 0) { | ||
return ""; | ||
} | ||
return ""; | ||
} | ||
var iFloor = Math.floor(dQty); | ||
var sFloor = (iFloor === 0.0 ? "" : iFloor + " "); | ||
var dDecimal = dQty - iFloor; | ||
var iFloor = Math.floor(dQty); | ||
var sFloor = (iFloor === 0.0 ? "" : iFloor + " "); | ||
var dDecimal = dQty - iFloor; | ||
// Handle integers first. Just return the given value. | ||
if (dDecimal === 0) { | ||
// Handle integers first. Just return the given value. | ||
if (dDecimal === 0) { | ||
return sInQty + ""; | ||
} | ||
return sInQty + ""; | ||
} | ||
// Handle infinitely repeating decimals next, since | ||
// we'll never get an exact match for a switch case: | ||
if (closeEnough(dDecimal, 0.33)) { | ||
// Handle infinitely repeating decimals next, since | ||
// we'll never get an exact match for a switch case: | ||
if (closeEnough(dDecimal, 0.33)) { | ||
sQty = sFloor + "1/3"; | ||
sQty = sFloor + "1/3"; | ||
} else if (closeEnough(dDecimal, 0.66)) { | ||
} else if (closeEnough(dDecimal, 0.66)) { | ||
sQty = sFloor + "2/3"; | ||
sQty = sFloor + "2/3"; | ||
} else if (closeEnough(dDecimal, 0.2)) { | ||
} else if (closeEnough(dDecimal, 0.2)) { | ||
sQty = sFloor + "1/5"; | ||
sQty = sFloor + "1/5"; | ||
} else if (closeEnough(dDecimal, 0.4)) { | ||
} else if (closeEnough(dDecimal, 0.4)) { | ||
sQty = sFloor + "2/5"; | ||
sQty = sFloor + "2/5"; | ||
} else if (closeEnough(dDecimal, 0.6)) { | ||
} else if (closeEnough(dDecimal, 0.6)) { | ||
sQty = sFloor + "3/5"; | ||
sQty = sFloor + "3/5"; | ||
} else if (closeEnough(dDecimal, 0.8)) { | ||
} else if (closeEnough(dDecimal, 0.8)) { | ||
sQty = sFloor + "4/5"; | ||
sQty = sFloor + "4/5"; | ||
} else { | ||
} else { | ||
switch (dDecimal) { | ||
case 0.125: sQty = sFloor + "1/8"; break; | ||
// case 0.2: sQty = sFloor + "1/5"; break; | ||
case 0.25: sQty = sFloor + "1/4"; break; | ||
case 0.375: sQty = sFloor + "3/8"; break; | ||
// case 0.4: sQty = sFloor + "2/5"; break; | ||
case 0.5: sQty = sFloor + "1/2"; break; | ||
// case 0.6: sQty = sFloor + "3/5"; break; | ||
case 0.625: sQty = sFloor + "5/8"; break; | ||
case 0.75: sQty = sFloor + "3/4"; break; | ||
// case 0.8: sQty = sFloor + "4/5"; break; | ||
case 0.875: sQty = sFloor + "7/8"; break; | ||
switch (dDecimal) { | ||
case 0.125: | ||
sQty = sFloor + "1/8"; | ||
break; | ||
// case 0.2: sQty = sFloor + "1/5"; break; | ||
case 0.25: | ||
sQty = sFloor + "1/4"; | ||
break; | ||
case 0.375: | ||
sQty = sFloor + "3/8"; | ||
break; | ||
// case 0.4: sQty = sFloor + "2/5"; break; | ||
case 0.5: | ||
sQty = sFloor + "1/2"; | ||
break; | ||
// case 0.6: sQty = sFloor + "3/5"; break; | ||
case 0.625: | ||
sQty = sFloor + "5/8"; | ||
break; | ||
case 0.75: | ||
sQty = sFloor + "3/4"; | ||
break; | ||
// case 0.8: sQty = sFloor + "4/5"; break; | ||
case 0.875: | ||
sQty = sFloor + "7/8"; | ||
break; | ||
} | ||
} | ||
} | ||
return sQty + ""; | ||
}; | ||
return sQty + ""; | ||
}; | ||
return formatQuantity; | ||
return formatQuantity; | ||
})); |
{ | ||
"name": "format-quantity", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Format a number as an integer plus fraction, as seen in recipe ingredient lists", | ||
@@ -10,4 +10,5 @@ "repository": { | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node ./test.js" | ||
}, | ||
@@ -14,0 +15,0 @@ "keywords": [ |
@@ -7,3 +7,3 @@ # format-quantity | ||
https://www.npmjs.com/package/format-quantity | ||
[format-quantity](https://www.npmjs.com/package/format-quantity) | ||
@@ -10,0 +10,0 @@ ``` |
@@ -0,0 +0,0 @@ /** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
9034
10
213
2