format-quantity
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -11,5 +11,6 @@ 'use strict'; | ||
* as one would see it written in imperial measurements, e.g. | ||
* "1 1/2" instead of "1.5". | ||
* "1 1/2" instead of "1.5". To use vulgar fractions, e.g. "½", | ||
* pass `true` as the second argument. | ||
*/ | ||
function formatQuantity(qty) { | ||
function formatQuantity(qty, useVulgarFractions) { | ||
var dQty = typeof qty === 'string' ? parseFloat(qty) : qty; | ||
@@ -32,21 +33,22 @@ // Bomb out if not a number | ||
} | ||
var sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor; | ||
// Handle infinitely repeating decimals next, since | ||
// we'll never get an exact match for a switch case: | ||
if (closeEnough(dDecimal, 0.33)) { | ||
return sFloor + "1/3"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2153' : '1/3'); | ||
} | ||
else if (closeEnough(dDecimal, 0.66)) { | ||
return sFloor + "2/3"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2154' : '2/3'); | ||
} | ||
else if (closeEnough(dDecimal, 0.2)) { | ||
return sFloor + "1/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2155' : '1/5'); | ||
} | ||
else if (closeEnough(dDecimal, 0.4)) { | ||
return sFloor + "2/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2156' : '2/5'); | ||
} | ||
else if (closeEnough(dDecimal, 0.6)) { | ||
return sFloor + "3/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2157' : '3/5'); | ||
} | ||
else if (closeEnough(dDecimal, 0.8)) { | ||
return sFloor + "4/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2158' : '4/5'); | ||
} | ||
@@ -56,15 +58,15 @@ else { | ||
case 0.125: | ||
return sFloor + "1/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215B' : '1/8'); | ||
case 0.25: | ||
return sFloor + "1/4"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u00BC' : '1/4'); | ||
case 0.375: | ||
return sFloor + "3/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215C' : '3/8'); | ||
case 0.5: | ||
return sFloor + "1/2"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u00BD' : '1/2'); | ||
case 0.625: | ||
return sFloor + "5/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215D' : '5/8'); | ||
case 0.75: | ||
return sFloor + "3/4"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u00BE' : '3/4'); | ||
case 0.875: | ||
return sFloor + "7/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215E' : '7/8'); | ||
} | ||
@@ -71,0 +73,0 @@ } |
/** | ||
* 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". | ||
* "1 1/2" instead of "1.5". To use vulgar fractions, e.g. "½", | ||
* pass `true` as the second argument. | ||
*/ | ||
declare function formatQuantity(qty: string | number): string; | ||
declare function formatQuantity(qty: string | number, useVulgarFractions?: boolean): string; | ||
export default formatQuantity; |
@@ -9,5 +9,6 @@ /** | ||
* as one would see it written in imperial measurements, e.g. | ||
* "1 1/2" instead of "1.5". | ||
* "1 1/2" instead of "1.5". To use vulgar fractions, e.g. "½", | ||
* pass `true` as the second argument. | ||
*/ | ||
function formatQuantity(qty) { | ||
function formatQuantity(qty, useVulgarFractions) { | ||
var dQty = typeof qty === 'string' ? parseFloat(qty) : qty; | ||
@@ -30,21 +31,22 @@ // Bomb out if not a number | ||
} | ||
var sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor; | ||
// Handle infinitely repeating decimals next, since | ||
// we'll never get an exact match for a switch case: | ||
if (closeEnough(dDecimal, 0.33)) { | ||
return sFloor + "1/3"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2153' : '1/3'); | ||
} | ||
else if (closeEnough(dDecimal, 0.66)) { | ||
return sFloor + "2/3"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2154' : '2/3'); | ||
} | ||
else if (closeEnough(dDecimal, 0.2)) { | ||
return sFloor + "1/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2155' : '1/5'); | ||
} | ||
else if (closeEnough(dDecimal, 0.4)) { | ||
return sFloor + "2/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2156' : '2/5'); | ||
} | ||
else if (closeEnough(dDecimal, 0.6)) { | ||
return sFloor + "3/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2157' : '3/5'); | ||
} | ||
else if (closeEnough(dDecimal, 0.8)) { | ||
return sFloor + "4/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2158' : '4/5'); | ||
} | ||
@@ -54,15 +56,15 @@ else { | ||
case 0.125: | ||
return sFloor + "1/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215B' : '1/8'); | ||
case 0.25: | ||
return sFloor + "1/4"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u00BC' : '1/4'); | ||
case 0.375: | ||
return sFloor + "3/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215C' : '3/8'); | ||
case 0.5: | ||
return sFloor + "1/2"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u00BD' : '1/2'); | ||
case 0.625: | ||
return sFloor + "5/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215D' : '5/8'); | ||
case 0.75: | ||
return sFloor + "3/4"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u00BE' : '3/4'); | ||
case 0.875: | ||
return sFloor + "7/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215E' : '7/8'); | ||
} | ||
@@ -69,0 +71,0 @@ } |
@@ -15,5 +15,6 @@ (function (global, factory) { | ||
* as one would see it written in imperial measurements, e.g. | ||
* "1 1/2" instead of "1.5". | ||
* "1 1/2" instead of "1.5". To use vulgar fractions, e.g. "½", | ||
* pass `true` as the second argument. | ||
*/ | ||
function formatQuantity(qty) { | ||
function formatQuantity(qty, useVulgarFractions) { | ||
var dQty = typeof qty === 'string' ? parseFloat(qty) : qty; | ||
@@ -36,21 +37,22 @@ // Bomb out if not a number | ||
} | ||
var sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor; | ||
// Handle infinitely repeating decimals next, since | ||
// we'll never get an exact match for a switch case: | ||
if (closeEnough(dDecimal, 0.33)) { | ||
return sFloor + "1/3"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2153' : '1/3'); | ||
} | ||
else if (closeEnough(dDecimal, 0.66)) { | ||
return sFloor + "2/3"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2154' : '2/3'); | ||
} | ||
else if (closeEnough(dDecimal, 0.2)) { | ||
return sFloor + "1/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2155' : '1/5'); | ||
} | ||
else if (closeEnough(dDecimal, 0.4)) { | ||
return sFloor + "2/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2156' : '2/5'); | ||
} | ||
else if (closeEnough(dDecimal, 0.6)) { | ||
return sFloor + "3/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2157' : '3/5'); | ||
} | ||
else if (closeEnough(dDecimal, 0.8)) { | ||
return sFloor + "4/5"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u2158' : '4/5'); | ||
} | ||
@@ -60,15 +62,15 @@ else { | ||
case 0.125: | ||
return sFloor + "1/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215B' : '1/8'); | ||
case 0.25: | ||
return sFloor + "1/4"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u00BC' : '1/4'); | ||
case 0.375: | ||
return sFloor + "3/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215C' : '3/8'); | ||
case 0.5: | ||
return sFloor + "1/2"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u00BD' : '1/2'); | ||
case 0.625: | ||
return sFloor + "5/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215D' : '5/8'); | ||
case 0.75: | ||
return sFloor + "3/4"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u00BE' : '3/4'); | ||
case 0.875: | ||
return sFloor + "7/8"; | ||
return "" + sFloorFinal + (useVulgarFractions ? '\u215E' : '7/8'); | ||
} | ||
@@ -75,0 +77,0 @@ } |
{ | ||
"name": "format-quantity", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Format a number as an integer plus fraction, as seen in recipe ingredient lists", | ||
@@ -19,3 +19,3 @@ "repository": { | ||
"dev": "rollup -c -w", | ||
"test": "node ./tests/test.js" | ||
"test": "jest" | ||
}, | ||
@@ -37,2 +37,8 @@ "keywords": [ | ||
"devDependencies": { | ||
"@babel/core": "^7.5.5", | ||
"@babel/preset-env": "^7.5.5", | ||
"@babel/preset-typescript": "^7.3.3", | ||
"@types/jest": "^24.0.18", | ||
"babel-jest": "^24.9.0", | ||
"jest": "^24.9.0", | ||
"rollup": "^1.20.0", | ||
@@ -39,0 +45,0 @@ "rollup-plugin-typescript2": "^0.23.0", |
@@ -0,10 +1,11 @@ | ||
# format-quantity | ||
[![npm version](https://badge.fury.io/js/format-quantity.svg)](//npmjs.com/package/format-quantity) | ||
[![Travis (.org)](https://img.shields.io/travis/jakeboone02/format-quantity)](https://travis-ci.org/jakeboone02/format-quantity) | ||
[![codecov.io](https://codecov.io/github/jakeboone02/format-quantity/coverage.svg?branch=master)](https://codecov.io/github/jakeboone02/format-quantity?branch=master) | ||
[![downloads](https://img.shields.io/npm/dm/format-quantity.svg)](http://npm-stat.com/charts.html?package=format-quantity&from=2015-08-01) | ||
[![MIT License](https://img.shields.io/npm/l/format-quantity.svg)](http://opensource.org/licenses/MIT) | ||
# format-quantity | ||
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. | ||
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". | ||
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). | ||
@@ -44,4 +45,5 @@ | ||
console.log(formatQuantity(2.66)); // "2 2/3" | ||
console.log(formatQuantity(3.875, true)); // "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
13986
228
49
9