Skip to content

Commit

Permalink
introduces fromDefinition function
Browse files Browse the repository at this point in the history
  • Loading branch information
chrvadala committed Apr 5, 2019
1 parent bb4c889 commit ce314fe
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 147 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Each value could be a float or a string that contains a float</p>
<dt><a href="#fromString">fromString(string)</a> ⇒ <code>Object</code></dt>
<dd><p>Parse a string matrix formatted as matrix(a,b,c,d,e,f)</p>
</dd>
<dt><a href="#fromTransformAttribute">fromTransformAttribute(transformString)</a> ⇒ <code>Object</code></dt>
<dt><a href="#fromTransformAttribute">fromTransformAttribute(transformString)</a> ⇒</dt>
<dd><p>Parser for SVG Trasform Attribute <a href="http://www.w3.org/TR/SVG/coords.html#TransformAttribute">http://www.w3.org/TR/SVG/coords.html#TransformAttribute</a> <br/>
Warning: This should be considered BETA until it is released a stable version of pegjs.</p>
</dd>
Expand Down Expand Up @@ -168,7 +168,7 @@ Warning: This should be considered BETA until it is released a stable version of
- **1.13**- Adds `compose` function, Upgrades deps, Exposes skew operation [#37](https://github.com/chrvadala/transformation-matrix/pull/37)
- **1.14**- Adds support for points defined as `Array` in the form `[x, y]` [#38](https://github.com/chrvadala/transformation-matrix/pull/38)
- **1.15**- Adds `fromTriangle` and `smoothMatrix` functions [#41](https://github.com/chrvadala/transformation-matrix/issues/41)
- **2.0**- Migrates to Babel 7 and updates dependencies
- **2.0**- Migrates to Babel 7 and updates dependencies; introduces `fromDefinition` function, changes fromTransformAttribute return object

## Some projects using transformation-matrix
- [**React Planner**](https://github.com/cvdlab/react-planner)
Expand Down Expand Up @@ -239,12 +239,12 @@ Parse a string matrix formatted as matrix(a,b,c,d,e,f)

<a name="fromTransformAttribute"></a>

## fromTransformAttribute(transformString) ⇒ <code>Object</code>
## fromTransformAttribute(transformString) ⇒
Parser for SVG Trasform Attribute http://www.w3.org/TR/SVG/coords.html#TransformAttribute <br/>
Warning: This should be considered BETA until it is released a stable version of pegjs.

**Kind**: global function
**Returns**: <code>Object</code> - Parsed matrices
**Returns**: Array Parsed matrix descriptor

| Param | Description |
| --- | --- |
Expand Down
2 changes: 1 addition & 1 deletion README.template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ available at [http://chrvadala.github.io/transformation-matrix/](http://chrvadal
- **1.13**- Adds `compose` function, Upgrades deps, Exposes skew operation [#37](https://github.com/chrvadala/transformation-matrix/pull/37)
- **1.14**- Adds support for points defined as `Array` in the form `[x, y]` [#38](https://github.com/chrvadala/transformation-matrix/pull/38)
- **1.15**- Adds `fromTriangle` and `smoothMatrix` functions [#41](https://github.com/chrvadala/transformation-matrix/issues/41)
- **2.0**- Migrates to Babel 7 and updates dependencies
- **2.0**- Migrates to Babel 7 and updates dependencies; introduces `fromDefinition` function, changes fromTransformAttribute return object

## Some projects using transformation-matrix
- [**React Planner**](https://github.com/cvdlab/react-planner)
Expand Down
66 changes: 66 additions & 0 deletions src/fromDefinition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { fromObject } from './fromObject'
import { translate } from './translate'
import { scale } from './scale'
import { rotateDEG } from './rotate'
import { skewDEG } from './skew'
import { shear } from './shear'

export function fromDefinition (definitionOrArrayOfDefinition) {
return Array.isArray(definitionOrArrayOfDefinition)
? definitionOrArrayOfDefinition.map(mapper)
: mapper(definitionOrArrayOfDefinition)

function mapper (descriptor) {
switch (descriptor.type) {
case 'matrix':
if ('a' in descriptor &&
'b' in descriptor &&
'c' in descriptor &&
'd' in descriptor &&
'e' in descriptor &&
'f' in descriptor
) {
return fromObject(descriptor)
} else {
throw new Error('MISSING_MANDATORY_PARAM')
}

case 'translate':
if (!('tx' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM')

if ('ty' in descriptor) return translate(descriptor.tx, descriptor.ty)

return translate(descriptor.tx)

case 'scale':
if (!('sx' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM')

if ('sy' in descriptor) return scale(descriptor.sx, descriptor.sy)

return scale(descriptor.sx)

case 'rotate':
if (!('angle' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM')

if ('cx' in descriptor && 'cy' in descriptor) {
return rotateDEG(descriptor.angle, descriptor.cx, descriptor.cy)
}
return rotateDEG(descriptor.angle)

case 'skewX':
if (!('angle' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM')
return skewDEG(descriptor.angle, 0)

case 'skewY':
if (!('angle' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM')
return skewDEG(0, descriptor.angle)

case 'shear':
if (!('shx' in descriptor && 'shy' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM')
return shear(descriptor.shx, descriptor.shy)

default:
throw new Error('UNSUPPORTED_DESCRIPTOR')
}
}
}
46 changes: 2 additions & 44 deletions src/fromTransformAttribute.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,11 @@
import { parse } from './fromTransformAttribute.autogenerated'
import { translate } from './translate'
import { rotateDEG } from './rotate'
import { fromObject } from './fromObject'
import { scale } from './scale'
import { skewDEG } from './skew'

/**
* Parser for SVG Trasform Attribute http://www.w3.org/TR/SVG/coords.html#TransformAttribute <br/>
* Warning: This should be considered BETA until it is released a stable version of pegjs.
* @param transformString string
* @returns {{descriptors: *, matrices: *}} Parsed matrices
* @returns Array Parsed matrix descriptor
*/
export function fromTransformAttribute (transformString) {
let descriptors = parse(transformString)
let matrices = descriptors.map(convertMatrixDescriptorToMatrix)
return { descriptors, matrices }
}

function convertMatrixDescriptorToMatrix (matrixDescriptor) {
let hasParam = key => matrixDescriptor.hasOwnProperty(key)

let { type, ...params } = matrixDescriptor
switch (type) {
case 'matrix':
return fromObject(params)

case 'translate':
if (hasParam('ty')) { return translate(params.tx, params.ty) }

return translate(params.tx)

case 'scale':
if (hasParam('sy')) { return scale(params.sx, params.sy) }

return scale(params.sx)

case 'rotate':
if (hasParam('cx') && hasParam('cy')) { return rotateDEG(params.angle, params.cx, params.cy) }

return rotateDEG(params.angle)

case 'skewX':
return skewDEG(params.angle, 0)

case 'skewY':
return skewDEG(0, params.angle)

default:
/* istanbul ignore next */
throw new Error('Unsupported descriptor')
}
return parse(transformString)
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './transform'
export * from './translate'
export * from './fromTriangles'
export * from './smoothMatrix'
export * from './fromDefinition'
16 changes: 10 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ export function isUndefined (val) {
}

export function isNumeric (n) {
return typeof n === 'number'
&& !Number.isNaN(n)
&& Number.isFinite(n)
return typeof n === 'number' &&
!Number.isNaN(n) &&
Number.isFinite(n)
}

export function isObject (obj) {
return typeof obj === 'object'
&& obj !== null
&& !Array.isArray(obj)
return typeof obj === 'object' &&
obj !== null &&
!Array.isArray(obj)
}

export function matchesShape (obj, keys) {
return keys.every(key => key in obj)
}
80 changes: 80 additions & 0 deletions test/fromDefinition.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* global describe, test, expect */
import { translate } from '../src/translate'
import { rotateDEG } from '../src/rotate'
import { scale } from '../src/scale'
import { skewDEG } from '../src/skew'
import { fromDefinition, shear } from '../src'

describe.only('fromDefinition', () => {
test('atomic transformations', () => {
expect(fromDefinition({ type: 'matrix', a: 1.1, b: 2.2, c: 3.3, d: 4.4, e: 5.5, f: 6.6 })).toEqual(
{ a: 1.1, b: 2.2, c: 3.3, d: 4.4, e: 5.5, f: 6.6 }
)
expect(() => fromDefinition({ type: 'matrix' })).toThrow()

// translate
expect(fromDefinition({ type: 'translate', tx: 1.1 })).toEqual(
translate(1.1, 0)
)
expect(fromDefinition({ type: 'translate', tx: 1.1, ty: 2.2 })).toEqual(
translate(1.1, 2.2)
)
expect(() => fromDefinition({ type: 'translate' })).toThrow()

// scale
expect(fromDefinition({ type: 'scale', sx: 1.1 })).toEqual(
scale(1.1)
)
expect(fromDefinition({ type: 'scale', sx: 1.1, sy: 2.2 })).toEqual(
scale(1.1, 2.2)
)
expect(() => fromDefinition({ type: 'scale' })).toThrow()

// rotate
expect(fromDefinition({ type: 'rotate', angle: 45 })).toEqual(
rotateDEG(45)
)
expect(fromDefinition({ type: 'rotate', angle: 45, cx: 100, cy: 200 })).toEqual(
rotateDEG(45, 100, 200)
)
expect(() => fromDefinition({ type: 'rotate' })).toThrow()

// skew
expect(fromDefinition({ type: 'skewX', angle: 45 })).toEqual(
skewDEG(45, 0)
)
expect(fromDefinition({ type: 'skewY', angle: 45 })).toEqual(
skewDEG(0, 45)
)
expect(() => fromDefinition({ type: 'skewX' })).toThrow()
expect(() => fromDefinition({ type: 'skewY' })).toThrow()

// shear
expect(fromDefinition({ type: 'shear', shx: 5, shy: 10 })).toEqual(
shear(5, 10)
)
expect(() => fromDefinition({ type: 'shear' })).toThrow()

// common
expect(() => fromDefinition('M_A_T_R_I_X(1,2,3,4,5,6)')).toThrow()
})

test('multiple matrices', () => {
expect(
fromDefinition([
{ type: 'translate', tx: 1, ty: -1 },
{ type: 'matrix', a: 6, b: 5, c: 4, d: 3, e: 2, f: 1 },
{ type: 'translate', tx: 1, ty: -1 },
{ type: 'translate', tx: 1, ty: -1 },
{ type: 'translate', tx: 1, ty: -1 }
])
).toEqual([
translate(1, -1),
{ a: 6, b: 5, c: 4, d: 3, e: 2, f: 1 },
translate(1, -1),
translate(1, -1),
translate(1, -1)
]
)
})
})

0 comments on commit ce314fe

Please sign in to comment.