diff options
-rw-r--r-- | spec/spec/array.js | 2 | ||||
-rw-r--r-- | src/pointarray.js | 22 |
2 files changed, 8 insertions, 16 deletions
diff --git a/spec/spec/array.js b/spec/spec/array.js index 984c6e1..39a2d44 100644 --- a/spec/spec/array.js +++ b/spec/spec/array.js @@ -33,7 +33,7 @@ describe('PointArray', function () { expect(array + '').toBe('0,0.15 -100,-3.141592654 50,100') }) - it('parses Polygon points string correctly', function() { + it('parses points with space delimitered x/y coordinates', function() { var array = new SVG.PointArray('221.08 191.79 0.46 191.79 0.46 63.92 63.8 0.46 284.46 0.46 284.46 128.37 221.08 191.79') expect(array + '').toBe('221.08,191.79 0.46,191.79 0.46,63.92 63.8,0.46 284.46,0.46 284.46,128.37 221.08,191.79') diff --git a/src/pointarray.js b/src/pointarray.js index b935cb5..2911cd1 100644 --- a/src/pointarray.js +++ b/src/pointarray.js @@ -40,33 +40,25 @@ SVG.extend(SVG.PointArray, { } // Parse point string , parse: function(array) { + var points = [] + array = array.valueOf() // if already is an array, no need to parse it if (Array.isArray(array)) return array - // parse points - var points = array.split(/\s|,/) + array = array.split(/\s|,/) // validate points - https://svgwg.org/svg2-draft/shapes.html#DataTypePoints // Odd number of coordinates is an error. In such cases, drop the last odd coordinate. - if (points.length % 2 !== 0) points.pop() - - // parse points as floats - points = points.map(function(x) { return parseFloat(x) }) + if (array.length % 2 !== 0) array.pop() - // wrap points in two-tuples - points = points.map(wrapPoints()).filter(function(x) { return x }) + // wrap points in two-tuples and parse points as floats + for(var i = 0, len = points.length; i < len; i + 2) + point.push([ parseFloat(points[i]), parseFloat(points[i+1]) ]) return points - - function wrapPoints() { - var a - return function(b, i) { - return i % 2 === 0 ? (a = b, null) : [a,b] - } - } } // Move point string , move: function(x, y) { |