imperial-metric
Advanced tools
Comparing version 1.0.0 to 1.1.0
89
index.js
var ImperialMetric = function(val) { | ||
this.val = val; | ||
this.map = { | ||
inch: { | ||
belong: "imperial", | ||
toMain: 1, | ||
toOthers: (2.54 / 100) | ||
}, | ||
foot: { | ||
belong: "imperial", | ||
toMain: 12, | ||
toOthers: (2.54 / 100) | ||
}, | ||
m: { | ||
belong: "metric", | ||
toMain: 1, | ||
toOthers: (1 / 2.54) * 100 | ||
}, | ||
cm: { | ||
belong: "metric", | ||
toMain: 1 / 100, | ||
toOthers: (1 / 2.54) * 100 | ||
}, | ||
mm: { | ||
belong: "metric", | ||
toMain: 1 / 1000, | ||
toOthers: (1 / 2.54) * 100 | ||
}, | ||
km: { | ||
belong: "metric", | ||
toMain: 1000, | ||
toOthers: (1 / 2.54) * 100 | ||
} | ||
} | ||
} | ||
@@ -10,61 +42,18 @@ | ||
ImperialMetric.prototype.from = function(unit) { | ||
this.unit = unit; | ||
this.unit = this.map[unit]; | ||
return this; | ||
} | ||
ImperialMetric.prototype.toInch = function() { | ||
switch(this.unit) { | ||
case "inch": | ||
return this.val; | ||
case "cm": | ||
return this.val * (1 / 2.54); | ||
case "m": | ||
return this.val * (1 / 2.54) * 100; | ||
case "foot": | ||
return this.val * 12; | ||
ImperialMetric.prototype.to = function(goal) { | ||
this.goal = this.map[goal]; | ||
if (this.unit.belong !== this.goal.belong) { | ||
return this.val * this.unit.toMain * this.unit.toOthers / this.goal.toMain; | ||
} | ||
} | ||
ImperialMetric.prototype.toFoot = function() { | ||
switch(this.unit) { | ||
case "inch": | ||
return this.val * (1 / 12); | ||
case "cm": | ||
return this.val * (1 / 30.48); | ||
case "m": | ||
return this.val * (1 / 30.48) * 100; | ||
case "foot": | ||
return this.val; | ||
else { | ||
return this.val * this.unit.toMain / this.goal.toMain; | ||
} | ||
} | ||
ImperialMetric.prototype.toCm = function() { | ||
switch(this.unit) { | ||
case "inch": | ||
return this.val * 2.54; | ||
case "cm": | ||
return this.val; | ||
case "m": | ||
return this.val * (1 / 100); | ||
case "foot": | ||
return this.val * 30.48; | ||
} | ||
} | ||
ImperialMetric.prototype.toM = function() { | ||
switch(this.unit) { | ||
case "inch": | ||
return this.val * (2.54 / 100); | ||
case "cm": | ||
return this.val * (1 / 100); | ||
case "m": | ||
return this.val; | ||
case "foot": | ||
return this.val * (30.48 / 100); | ||
} | ||
} | ||
module.exports = function (value) { | ||
return new ImperialMetric(value); | ||
} |
{ | ||
"name": "imperial-metric", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,6 +7,6 @@ # ImperialMetric | ||
var IM = require("imperial-metric"); | ||
IM(1).from('inch').toCm(); // 2.54 | ||
IM(1).from('m').toInch(); // 1 / 30.48 | ||
IM(1).from('cm').toInch(); // 1 / 2.54 | ||
IM(1).from('foot').toM(); // 0.3048 | ||
IM(1).from('inch').to("cm"); // 2.54 | ||
IM(1).from('m').to("inch"); // 39.37007874 | ||
IM(1).from('cm').to("foot"); // 0.03280839895 | ||
IM(1).from('foot').to("m"); // 0.3048 | ||
``` | ||
@@ -18,5 +18,1 @@ | ||
``` | ||
### TODO | ||
* add `mm`, `km` to the conversion |
34
test.js
@@ -5,6 +5,6 @@ var IM = require("./index.js"); | ||
test("1 foot", function(t) { | ||
t.equal(IM(1).from("foot").toInch(), 12, "equal 12 inches"); | ||
t.equal(IM(1).from("foot").toFoot(), 1, "equal 1 foot"); | ||
t.equal(IM(1).from("foot").toCm(), 30.48, "equal 30.48 cm"); | ||
t.equal(IM(1).from("foot").toM(), 0.3048, "equal 0.3048 m"); | ||
t.equal(IM(1).from("foot").to("inch"), 12, "equal 12 inches"); | ||
t.equal(IM(1).from("foot").to("foot"), 1, "equal 1 foot"); | ||
t.equal(Number(IM(1).from("foot").to("cm").toFixed(2)), 30.48, "equal 30.48 cm"); | ||
t.equal(Number(IM(1).from("foot").to("m").toFixed(4)), 0.3048, "equal 0.3048 m"); | ||
t.end(); | ||
@@ -14,6 +14,6 @@ }); | ||
test("1 inch", function(t) { | ||
t.equal(IM(1).from("inch").toInch(), 1, "equal 1 inch"); | ||
t.equal(IM(1).from("inch").toFoot(), 1 / 12, "equal 1 / 12 foot"); | ||
t.equal(IM(1).from("inch").toCm(), 2.54, "equal 2.54 cm"); | ||
t.equal(IM(1).from("inch").toM(), 2.54 / 100, "equal 2.54 / 100 m"); | ||
t.equal(IM(1).from("inch").to("inch"), 1, "equal 1 inch"); | ||
t.equal(IM(1).from("inch").to("foot"), 1 / 12, "equal 1 / 12 foot"); | ||
t.equal(IM(1).from("inch").to("cm"), 2.54, "equal 2.54 cm"); | ||
t.equal(IM(1).from("inch").to("m"), 2.54 / 100, "equal 2.54 / 100 m"); | ||
t.end(); | ||
@@ -24,6 +24,7 @@ }); | ||
test("1 cm", function(t) { | ||
t.equal(IM(1).from("cm").toInch(), 1 / 2.54, "equal 1 / 2.54 inch"); | ||
t.equal(IM(1).from("cm").toFoot(), 1 / 30.48, "equal 1 / 30.48 foot"); | ||
t.equal(IM(1).from("cm").toCm(), 1, "equal 1 cm"); | ||
t.equal(IM(1).from("cm").toM(), 1 / 100, "equal 1 / 100 m"); | ||
t.equal(IM(1).from("cm").to("inch"), 1 / 2.54, "equal 1 / 2.54 inch"); | ||
t.equal(IM(1).from("cm").to("foot"), 1 / 30.48, "equal 1 / 30.48 foot"); | ||
t.equal(IM(1).from("cm").to("cm"), 1, "equal 1 cm"); | ||
t.equal(IM(1).from("cm").to("m"), 1 / 100, "equal 1 / 100 m"); | ||
t.equal(IM(1).from("cm").to("mm"), 10, "equal 10 mm"); | ||
t.end(); | ||
@@ -34,7 +35,8 @@ }); | ||
test("1 m", function(t) { | ||
t.equal(IM(1).from("m").toInch(), (1 / 2.54) * 100, "equal (1 / 2.54) * 100 inch"); | ||
t.equal(IM(1).from("m").toFoot(), (1 / 30.48) * 100, "equal (1 / 30.48) * 100 foot"); | ||
t.equal(IM(1).from("m").toCm(), 0.01, "equal 0.01 cm"); | ||
t.equal(IM(1).from("m").toM(), 1, "equal 1 m"); | ||
t.equal(IM(1).from("m").to("inch"), (1 / 2.54) * 100, "equal (1 / 2.54) * 100 inch"); | ||
t.equal(IM(1).from("m").to("foot").toFixed(2), ((1 / 30.48) * 100).toFixed(2), "equal (1 / 30.48) * 100 foot"); | ||
t.equal(IM(1).from("m").to("cm"), 100, "equal 100 cm"); | ||
t.equal(IM(1).from("m").to("m"), 1, "equal 1 m"); | ||
t.equal(IM(1000).from("m").to("km"), 1, "equal 1 km"); | ||
t.end(); | ||
}); |
5
3149
86
17