Comparing version 0.1.2 to 0.1.3
30
index.js
@@ -1,6 +0,6 @@ | ||
var util = require('util'); | ||
module.exports = v; | ||
v.Vec3 = Vec3; | ||
var re = /\((-?[.\d]+), (-?[.\d]+), (-?[.\d]+)\)/; | ||
function Vec3(x, y, z) { | ||
@@ -13,9 +13,21 @@ this.x = x; | ||
function v(x, y, z) { | ||
return x == null ? | ||
new Vec3(0, 0, 0) : | ||
util.isArray(x) ? | ||
new Vec3(x[0], x[1], x[2]) : | ||
(typeof x === 'object') ? | ||
new Vec3(x.x, x.y, x.z) : | ||
new Vec3(x, y, z); | ||
if (x == null) { | ||
return new Vec3(0, 0, 0); | ||
} else if (Array.isArray(x)) { | ||
return new Vec3(parseFloat(x[0], 10), parseFloat(x[1], 10), parseFloat(x[2], 10)); | ||
} else if (typeof x === 'object') { | ||
return new Vec3(parseFloat(x.x, 10), parseFloat(x.y, 10), parseFloat(x.z, 10)); | ||
} else if (typeof x === 'string' && y == null) { | ||
var match = x.match(re); | ||
if (match) { | ||
return new Vec3( | ||
parseFloat(match[1], 10), | ||
parseFloat(match[2], 10), | ||
parseFloat(match[3], 10)); | ||
} else { | ||
throw new Error("vec3: cannot parse: " + x); | ||
} | ||
} else { | ||
return new Vec3(parseFloat(x, 10), parseFloat(y, 10), parseFloat(z, 10)); | ||
} | ||
} | ||
@@ -22,0 +34,0 @@ |
{ | ||
"name": "vec3", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "3d vector math with good unit tests", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,2 +30,23 @@ var v = require('../') | ||
}); | ||
it("string coords", function() { | ||
var v1 = v("1", "1.5", "-30.2"); | ||
assert.strictEqual(v1.x, 1); | ||
assert.strictEqual(v1.y, 1.5); | ||
assert.strictEqual(v1.z, -30.2); | ||
}); | ||
it("deserialize", function() { | ||
var v1 = v(v(1, -3.5, 0).toString()); | ||
assert.strictEqual(v1.x, 1); | ||
assert.strictEqual(v1.y, -3.5); | ||
assert.strictEqual(v1.z, 0); | ||
var v2 = v(v(-111, 222, 9876543210.123456789).toString()); | ||
assert.strictEqual(v2.x, -111); | ||
assert.strictEqual(v2.y, 222); | ||
assert.strictEqual(v2.z, 9876543210.123456789); | ||
}); | ||
it("invalid deserialize", function() { | ||
assert.throws(function() { | ||
return v("lol hax"); | ||
}, /cannot parse/); | ||
}); | ||
}); | ||
@@ -32,0 +53,0 @@ describe("vec3", function() { |
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
14941
6
348