transformation-matrix
Advanced tools
Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "transformation-matrix", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "2d transformation matrix functions written in ES6 syntax. Tree shaking ready!", | ||
@@ -5,0 +5,0 @@ "main": "./lib/2d-transformation.js", |
@@ -67,2 +67,9 @@ # transformation-matrix | ||
</dd> | ||
<dt><a href="#isAffineMatrix">isAffineMatrix(object)</a> ⇒ <code>boolean</code></dt> | ||
<dd><p>Check if the object contain an affine matrix</p> | ||
</dd> | ||
<dt><a href="#fromObject">fromObject(object)</a> ⇒ <code>Object</code></dt> | ||
<dd><p>Extract an affine matrix from an object that contains a,b,c,d,e,f keys | ||
Each value could be a float or a string that contains a float</p> | ||
</dd> | ||
</dl> | ||
@@ -238,1 +245,25 @@ | ||
<a name="isAffineMatrix"></a> | ||
## isAffineMatrix(object) ⇒ <code>boolean</code> | ||
Check if the object contain an affine matrix | ||
**Kind**: global function | ||
| Param | | ||
| --- | | ||
| object | | ||
<a name="fromObject"></a> | ||
## fromObject(object) ⇒ <code>Object</code> | ||
Extract an affine matrix from an object that contains a,b,c,d,e,f keys | ||
Each value could be a float or a string that contains a float | ||
**Kind**: global function | ||
**Returns**: <code>Object</code> - } | ||
| Param | | ||
| --- | | ||
| object | | ||
@@ -191,1 +191,40 @@ const {cos, sin, PI} = Math; | ||
} | ||
/** | ||
* Check if the object contain an affine matrix | ||
* @param object | ||
* @return {boolean} | ||
*/ | ||
export function isAffineMatrix(object) { | ||
let isNumeric = n => typeof n === 'number' && !isNaN(n) && isFinite(n); | ||
return typeof object === 'object' | ||
&& object.hasOwnProperty('a') | ||
&& isNumeric(object.a) | ||
&& object.hasOwnProperty('b') | ||
&& isNumeric(object.b) | ||
&& object.hasOwnProperty('c') | ||
&& isNumeric(object.c) | ||
&& object.hasOwnProperty('d') | ||
&& isNumeric(object.d) | ||
&& object.hasOwnProperty('e') | ||
&& isNumeric(object.e) | ||
&& object.hasOwnProperty('f') | ||
&& isNumeric(object.f); | ||
} | ||
/** | ||
* Extract an affine matrix from an object that contains a,b,c,d,e,f keys | ||
* Each value could be a float or a string that contains a float | ||
* @param object | ||
* @return {{a: *, b: *, c: *, e: *, d: *, f: *}}} | ||
*/ | ||
export function fromObject(object) { | ||
return { | ||
a: parseFloat(object.a), | ||
b: parseFloat(object.b), | ||
c: parseFloat(object.c), | ||
d: parseFloat(object.d), | ||
e: parseFloat(object.e), | ||
f: parseFloat(object.f), | ||
}; | ||
} |
@@ -11,3 +11,5 @@ import { | ||
inverse, | ||
fromString | ||
fromString, | ||
isAffineMatrix, | ||
fromObject | ||
} from '../src/2d-transformation'; | ||
@@ -211,4 +213,43 @@ import chai from 'chai'; | ||
}) | ||
}); | ||
describe('isAffineMatrix', () => { | ||
it('should return true', () => { | ||
let o1 = {a: 0, b: 0, c: 0, d: 0, e: 0, f: 0}; | ||
let o2 = {a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, x: 100}; | ||
let o3 = {y: 200, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, x: 100}; | ||
assert.isTrue(isAffineMatrix(o1)); | ||
assert.isTrue(isAffineMatrix(o2)); | ||
assert.isTrue(isAffineMatrix(o3)); | ||
}); | ||
it('should return false', () => { | ||
let o1 = {a: 0, b: 0, c: 0, d: 0, e: 0}; | ||
let o2 = {a: 0, b: 0, c: 0, d: 0, e: 0, f: Number.NaN}; | ||
let o3 = {a: 0, b: 0, c: 0, d: 0, e: Number.POSITIVE_INFINITY, f: 0}; | ||
let o4 = {a: "0", b: 0, c: 0, d: 0, e: 0, f: 0}; | ||
let o5 = "{a: 0, b: 0, c: 0, d: 0, e: 0, f: 0}"; | ||
let o6 = {}; | ||
let o7 = 42; | ||
assert.isFalse(isAffineMatrix(o1)); | ||
assert.isFalse(isAffineMatrix(o2)); | ||
assert.isFalse(isAffineMatrix(o3)); | ||
assert.isFalse(isAffineMatrix(o4)); | ||
assert.isFalse(isAffineMatrix(o5)); | ||
assert.isFalse(isAffineMatrix(o6)); | ||
assert.isFalse(isAffineMatrix(o7)); | ||
}) | ||
}); | ||
describe('fromObject', () => { | ||
it('should return an affine matrix', () => { | ||
let expected = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}; | ||
let o1 = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}; | ||
let o2 = {a: 1, b: 2, z: 500, c: 3, d: 4, e: 5, f: 6, x: 100, y: 200}; | ||
let o3 = {a: "1", b: "2", c: "3", d: "4", e: "5", f: "6"}; | ||
assert.deepEqual(fromObject(o1), expected); | ||
assert.deepEqual(fromObject(o2), expected); | ||
assert.deepEqual(fromObject(o3), expected); | ||
}); | ||
}) | ||
}); | ||
139191
436
268