numeric-quantity
Advanced tools
Comparing version 0.1.1 to 0.1.2
91
index.js
@@ -6,47 +6,54 @@ module.exports = function(qty) { | ||
// Resolve any unicode vulgar fractions | ||
var sQty = (qty + "") | ||
.replace(/\u00BC/g, " 1/4") | ||
.replace(/\u00BD/g, " 1/2") | ||
.replace(/\u00BE/g, " 3/4") | ||
.replace(/\u2150/g, " 1/7") | ||
.replace(/\u2151/g, " 1/9") | ||
.replace(/\u2152/g, " 1/10") | ||
.replace(/\u2153/g, " 1/3") | ||
.replace(/\u2154/g, " 2/3") | ||
.replace(/\u2155/g, " 1/5") | ||
.replace(/\u2156/g, " 2/5") | ||
.replace(/\u2157/g, " 3/5") | ||
.replace(/\u2158/g, " 4/5") | ||
.replace(/\u2159/g, " 1/6") | ||
.replace(/\u215A/g, " 5/6") | ||
.replace(/\u215B/g, " 1/8") | ||
.replace(/\u215C/g, " 3/8") | ||
.replace(/\u215D/g, " 5/8") | ||
.replace(/\u215E/g, " 7/8"); | ||
var vulgarFractionsRegex = /(\u00BC|\u00BD|\u00BE|\u2150|\u2151|\u2152|\u2153|\u2154|\u2155|\u2156|\u2157|\u2158|\u2159|\u215A|\u215B|\u215C|\u215D|\u215E)/; | ||
/* | ||
Regex captures | ||
var vulgarFractionsCharMap = { | ||
"\u00BC": " 1/4", | ||
"\u00BD": " 1/2", | ||
"\u00BE": " 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" | ||
}; | ||
+=====+====================+========================+ | ||
| # | Description | Example | | ||
+=====+====================+========================+ | ||
| 0 | entire string | "2 2/3" from "2 2/3" | | ||
+-----+--------------------+------------------------+ | ||
| 1 | the whole number | "2" from "2 2/3" | | ||
| | - OR - | | | ||
| | the numerator | "2" from "2/3" | | ||
+-----+--------------------+------------------------+ | ||
| 2 | entire fraction | "2/3" from "2 2/3" | | ||
| | - OR - | | | ||
| | decimal portion | ".66" from "2.66" | | ||
| | - OR - | | | ||
| | denominator | "/3" from "2/3" | | ||
+=====+====================+========================+ | ||
var sQty = (qty + "").replace(vulgarFractionsRegex, function(m, vf) { | ||
return vulgarFractionsCharMap[vf]; | ||
}); | ||
re.exec("1") // [ "1", "1", null, null ] | ||
re.exec("1.23") // [ "1.23", "1", ".23", null ] | ||
re.exec("1 2/3") // [ "1 2/3", "1", " 2/3", " 2" ] | ||
re.exec("2/3") // [ "2/3", "2", "/3", null ] | ||
re.exec("2 / 3") // [ "2 / 3", "2", "/ 3", null ] | ||
*/ | ||
/** | ||
* Regex captures | ||
* | ||
* +=====+====================+========================+ | ||
* | # | Description | Example | | ||
* +=====+====================+========================+ | ||
* | 0 | entire string | "2 2/3" from "2 2/3" | | ||
* +-----+--------------------+------------------------+ | ||
* | 1 | the whole number | "2" from "2 2/3" | | ||
* | | - OR - | | | ||
* | | the numerator | "2" from "2/3" | | ||
* +-----+--------------------+------------------------+ | ||
* | 2 | entire fraction | "2/3" from "2 2/3" | | ||
* | | - OR - | | | ||
* | | decimal portion | ".66" from "2.66" | | ||
* | | - OR - | | | ||
* | | denominator | "/3" from "2/3" | | ||
* +=====+====================+========================+ | ||
* | ||
* re.exec("1") // [ "1", "1", null, null ] | ||
* re.exec("1.23") // [ "1.23", "1", ".23", null ] | ||
* re.exec("1 2/3") // [ "1 2/3", "1", " 2/3", " 2" ] | ||
* re.exec("2/3") // [ "2/3", "2", "/3", null ] | ||
* re.exec("2 / 3") // [ "2 / 3", "2", "/ 3", null ] | ||
*/ | ||
var re = /^\s*(\d+)(\.\d+|(\s+\d*\s*)?\s*\/\s*\d+)?\s*$/; | ||
@@ -53,0 +60,0 @@ |
{ | ||
"name": "numeric-quantity", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Convert integer plus fraction into a decimal value", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,0 +0,0 @@ # numeric-quantity |
25
test.js
@@ -39,3 +39,3 @@ /** | ||
assert(nq("1")).is(1); | ||
// TODO: don't allow leading zeroes on whole numbers | ||
// TODO?: don't allow leading zeroes on whole numbers | ||
// assert(nq("010")).is(-1); | ||
@@ -67,2 +67,25 @@ assert(nq("100")).is(100); | ||
assert(nq("1 4/5")).is(1.8); | ||
// Unicode vulgar fractions | ||
assert(nq("\u00BC")).is(0.25); // 1/4 | ||
assert(nq("\u00BD")).is(0.5); // 1/2 | ||
assert(nq("\u00BE")).is(0.75); // 3/4 | ||
assert(nq("\u2150")).is(0.143); // 1/7 | ||
assert(nq("\u2151")).is(0.111); // 1/9 | ||
assert(nq("\u2152")).is(0.1); // 1/10 | ||
assert(nq("\u2153")).is(0.333); // 1/3 | ||
assert(nq("\u2154")).is(0.667); // 2/3 | ||
assert(nq("\u2155")).is(0.2); // 1/5 | ||
assert(nq("\u2156")).is(0.4); // 2/5 | ||
assert(nq("\u2157")).is(0.6); // 3/5 | ||
assert(nq("\u2158")).is(0.8); // 4/5 | ||
assert(nq("\u2159")).is(0.167); // 1/6 | ||
assert(nq("\u215A")).is(0.833); // 5/6 | ||
assert(nq("\u215B")).is(0.125); // 1/8 | ||
assert(nq("\u215C")).is(0.375); // 3/8 | ||
assert(nq("\u215D")).is(0.625); // 5/8 | ||
assert(nq("\u215E")).is(0.875); // 7/8 | ||
// Mixed unicode vulgar fraction | ||
assert(nq("2 \u2155")).is(2.2); // 2 1/5 | ||
// Mixed unicode vulgar fraction - no space | ||
assert(nq("2\u2155")).is(2.2); // 2 1/5 | ||
@@ -69,0 +92,0 @@ // Report results |
Sorry, the diff of this file is not supported yet
7710
169