format-quantity
Advanced tools
Comparing version 0.4.2 to 0.5.0
@@ -15,13 +15,15 @@ 'use strict'; | ||
var dQty = typeof qty === 'string' ? parseFloat(qty) : qty; | ||
// bomb out if not a number | ||
// Bomb out if not a number | ||
if (isNaN(dQty) || dQty === null) { | ||
return '-1'; | ||
return null; | ||
} | ||
// Return an empty string if the value is zero | ||
if (dQty === 0) { | ||
return ''; | ||
} | ||
var iFloor = Math.floor(dQty); | ||
var sFloor = iFloor === 0.0 ? '' : iFloor + " "; | ||
var dDecimal = dQty - iFloor; | ||
// Handle integers first. Just return the given value. | ||
var dQtyAbs = Math.abs(dQty); | ||
var iFloor = Math.floor(dQtyAbs); | ||
var sFloor = iFloor === 0.0 ? '' : "" + (dQty < 0 ? '-' : '') + iFloor + " "; | ||
var dDecimal = dQtyAbs - iFloor; | ||
// Handle integers first. Just return the given value as a string. | ||
if (dDecimal === 0) { | ||
@@ -33,18 +35,18 @@ return "" + dQty; | ||
if (closeEnough(dDecimal, 0.33)) { | ||
return sFloor + '1/3'; | ||
return sFloor + "1/3"; | ||
} | ||
else if (closeEnough(dDecimal, 0.66)) { | ||
return sFloor + '2/3'; | ||
return sFloor + "2/3"; | ||
} | ||
else if (closeEnough(dDecimal, 0.2)) { | ||
return sFloor + '1/5'; | ||
return sFloor + "1/5"; | ||
} | ||
else if (closeEnough(dDecimal, 0.4)) { | ||
return sFloor + '2/5'; | ||
return sFloor + "2/5"; | ||
} | ||
else if (closeEnough(dDecimal, 0.6)) { | ||
return sFloor + '3/5'; | ||
return sFloor + "3/5"; | ||
} | ||
else if (closeEnough(dDecimal, 0.8)) { | ||
return sFloor + '4/5'; | ||
return sFloor + "4/5"; | ||
} | ||
@@ -54,15 +56,15 @@ else { | ||
case 0.125: | ||
return sFloor + '1/8'; | ||
return sFloor + "1/8"; | ||
case 0.25: | ||
return sFloor + '1/4'; | ||
return sFloor + "1/4"; | ||
case 0.375: | ||
return sFloor + '3/8'; | ||
return sFloor + "3/8"; | ||
case 0.5: | ||
return sFloor + '1/2'; | ||
return sFloor + "1/2"; | ||
case 0.625: | ||
return sFloor + '5/8'; | ||
return sFloor + "5/8"; | ||
case 0.75: | ||
return sFloor + '3/4'; | ||
return sFloor + "3/4"; | ||
case 0.875: | ||
return sFloor + '7/8'; | ||
return sFloor + "7/8"; | ||
} | ||
@@ -69,0 +71,0 @@ } |
@@ -13,13 +13,15 @@ /** | ||
var dQty = typeof qty === 'string' ? parseFloat(qty) : qty; | ||
// bomb out if not a number | ||
// Bomb out if not a number | ||
if (isNaN(dQty) || dQty === null) { | ||
return '-1'; | ||
return null; | ||
} | ||
// Return an empty string if the value is zero | ||
if (dQty === 0) { | ||
return ''; | ||
} | ||
var iFloor = Math.floor(dQty); | ||
var sFloor = iFloor === 0.0 ? '' : iFloor + " "; | ||
var dDecimal = dQty - iFloor; | ||
// Handle integers first. Just return the given value. | ||
var dQtyAbs = Math.abs(dQty); | ||
var iFloor = Math.floor(dQtyAbs); | ||
var sFloor = iFloor === 0.0 ? '' : "" + (dQty < 0 ? '-' : '') + iFloor + " "; | ||
var dDecimal = dQtyAbs - iFloor; | ||
// Handle integers first. Just return the given value as a string. | ||
if (dDecimal === 0) { | ||
@@ -31,18 +33,18 @@ return "" + dQty; | ||
if (closeEnough(dDecimal, 0.33)) { | ||
return sFloor + '1/3'; | ||
return sFloor + "1/3"; | ||
} | ||
else if (closeEnough(dDecimal, 0.66)) { | ||
return sFloor + '2/3'; | ||
return sFloor + "2/3"; | ||
} | ||
else if (closeEnough(dDecimal, 0.2)) { | ||
return sFloor + '1/5'; | ||
return sFloor + "1/5"; | ||
} | ||
else if (closeEnough(dDecimal, 0.4)) { | ||
return sFloor + '2/5'; | ||
return sFloor + "2/5"; | ||
} | ||
else if (closeEnough(dDecimal, 0.6)) { | ||
return sFloor + '3/5'; | ||
return sFloor + "3/5"; | ||
} | ||
else if (closeEnough(dDecimal, 0.8)) { | ||
return sFloor + '4/5'; | ||
return sFloor + "4/5"; | ||
} | ||
@@ -52,15 +54,15 @@ else { | ||
case 0.125: | ||
return sFloor + '1/8'; | ||
return sFloor + "1/8"; | ||
case 0.25: | ||
return sFloor + '1/4'; | ||
return sFloor + "1/4"; | ||
case 0.375: | ||
return sFloor + '3/8'; | ||
return sFloor + "3/8"; | ||
case 0.5: | ||
return sFloor + '1/2'; | ||
return sFloor + "1/2"; | ||
case 0.625: | ||
return sFloor + '5/8'; | ||
return sFloor + "5/8"; | ||
case 0.75: | ||
return sFloor + '3/4'; | ||
return sFloor + "3/4"; | ||
case 0.875: | ||
return sFloor + '7/8'; | ||
return sFloor + "7/8"; | ||
} | ||
@@ -67,0 +69,0 @@ } |
@@ -19,13 +19,15 @@ (function (global, factory) { | ||
var dQty = typeof qty === 'string' ? parseFloat(qty) : qty; | ||
// bomb out if not a number | ||
// Bomb out if not a number | ||
if (isNaN(dQty) || dQty === null) { | ||
return '-1'; | ||
return null; | ||
} | ||
// Return an empty string if the value is zero | ||
if (dQty === 0) { | ||
return ''; | ||
} | ||
var iFloor = Math.floor(dQty); | ||
var sFloor = iFloor === 0.0 ? '' : iFloor + " "; | ||
var dDecimal = dQty - iFloor; | ||
// Handle integers first. Just return the given value. | ||
var dQtyAbs = Math.abs(dQty); | ||
var iFloor = Math.floor(dQtyAbs); | ||
var sFloor = iFloor === 0.0 ? '' : "" + (dQty < 0 ? '-' : '') + iFloor + " "; | ||
var dDecimal = dQtyAbs - iFloor; | ||
// Handle integers first. Just return the given value as a string. | ||
if (dDecimal === 0) { | ||
@@ -37,18 +39,18 @@ return "" + dQty; | ||
if (closeEnough(dDecimal, 0.33)) { | ||
return sFloor + '1/3'; | ||
return sFloor + "1/3"; | ||
} | ||
else if (closeEnough(dDecimal, 0.66)) { | ||
return sFloor + '2/3'; | ||
return sFloor + "2/3"; | ||
} | ||
else if (closeEnough(dDecimal, 0.2)) { | ||
return sFloor + '1/5'; | ||
return sFloor + "1/5"; | ||
} | ||
else if (closeEnough(dDecimal, 0.4)) { | ||
return sFloor + '2/5'; | ||
return sFloor + "2/5"; | ||
} | ||
else if (closeEnough(dDecimal, 0.6)) { | ||
return sFloor + '3/5'; | ||
return sFloor + "3/5"; | ||
} | ||
else if (closeEnough(dDecimal, 0.8)) { | ||
return sFloor + '4/5'; | ||
return sFloor + "4/5"; | ||
} | ||
@@ -58,15 +60,15 @@ else { | ||
case 0.125: | ||
return sFloor + '1/8'; | ||
return sFloor + "1/8"; | ||
case 0.25: | ||
return sFloor + '1/4'; | ||
return sFloor + "1/4"; | ||
case 0.375: | ||
return sFloor + '3/8'; | ||
return sFloor + "3/8"; | ||
case 0.5: | ||
return sFloor + '1/2'; | ||
return sFloor + "1/2"; | ||
case 0.625: | ||
return sFloor + '5/8'; | ||
return sFloor + "5/8"; | ||
case 0.75: | ||
return sFloor + '3/4'; | ||
return sFloor + "3/4"; | ||
case 0.875: | ||
return sFloor + '7/8'; | ||
return sFloor + "7/8"; | ||
} | ||
@@ -73,0 +75,0 @@ } |
{ | ||
"name": "format-quantity", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "Format a number as an integer plus fraction, as seen in recipe ingredient lists", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -12,8 +12,8 @@ [![npm version](https://badge.fury.io/js/format-quantity.svg)](//npmjs.com/package/format-quantity) | ||
### Installation | ||
## Installation | ||
##### npm | ||
### npm | ||
``` | ||
npm i --save format-quantity | ||
npm i format-quantity | ||
``` | ||
@@ -27,3 +27,3 @@ | ||
##### Browser | ||
### Browser | ||
@@ -35,13 +35,15 @@ In the browser, available as a global function `formatQuantity`. | ||
<script> | ||
console.log(formatQuantity(10.5)); // "10 1/2" | ||
console.log(formatQuantity(10.5)); // "10 1/2" | ||
</script> | ||
``` | ||
### Usage | ||
## Usage | ||
```js | ||
import formatQuantity from "format-quantity"; | ||
import formatQuantity from 'format-quantity'; | ||
console.log( formatQuantity(1.5) ); // "1 1/2" | ||
console.log( formatQuantity(2.66) ); // "2 2/3" | ||
console.log(formatQuantity(1.5)); // "1 1/2" | ||
console.log(formatQuantity(2.66)); // "2 2/3" | ||
``` | ||
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). |
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
11184
8
221
47