From bbd1dd2d99e25318e2cf8971ee289eb210be0cfd Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Mon, 14 Jul 2014 09:59:03 -0500 Subject: [PATCH] Simplify point parsing logic The SVG spec basically allows delimiting numbers and pairs of numbers with either whitespace or commas, so convert all commas to whitespace, split on whitespace and remove the `asPairs` logic --- src/parser.js | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/src/parser.js b/src/parser.js index 307a5e21..e808052f 100644 --- a/src/parser.js +++ b/src/parser.js @@ -713,38 +713,19 @@ // points attribute is required and must not be empty if (!points) return null; - points = points.trim(); - var asPairs = points.indexOf(',') > -1; + // replace commas with whitespace and remove bookending whitespace + points = points.replace(/,/g, ' ').trim(); - // remove possible whitespace around commas - if (asPairs) { - points = points.replace(/\s*,\s*/g, ',') - } - points = points.split(/\s+/); var parsedPoints = [ ], i, len; - // points could look like "10,20 30,40" or "10 20 30 40" - if (asPairs) { - i = 0; - len = points.length; - for (; i < len; i++) { - var pair = points[i].split(','); - parsedPoints.push({ - x: parseFloat(pair[0]), - y: parseFloat(pair[1]) - }); - } - } - else { - i = 0; - len = points.length; - for (; i < len; i+=2) { - parsedPoints.push({ - x: parseFloat(points[i]), - y: parseFloat(points[i + 1]) - }); - } + i = 0; + len = points.length; + for (; i < len; i+=2) { + parsedPoints.push({ + x: parseFloat(points[i]), + y: parseFloat(points[i + 1]) + }); } // odd number of points is an error