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')
}
// 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) {