diff --git a/src/toPoints.js b/src/toPoints.js index f1f0bf7..34159fa 100644 --- a/src/toPoints.js +++ b/src/toPoints.js @@ -100,14 +100,13 @@ const getPointsFromPath = ({ d }) => { const upperCaseCommand = command.toUpperCase() const commandLength = commandLengths[ upperCaseCommand ] const relative = isRelative(command) + const prevPoint = i === 0 ? null : points[ points.length - 1 ] if (commandLength > 0) { const commandParams = params.shift() const iterations = commandParams.length / commandLength for (let j = 0; j < iterations; j++) { - const prevPoint = i === 0 ? null : points[ points.length - 1 ] - switch (upperCaseCommand) { case 'M': const x = (relative && prevPoint ? prevPoint.x : 0) + commandParams.shift() @@ -246,7 +245,9 @@ const getPointsFromPath = ({ d }) => { } } } else { - points.push({ x: moveTo.x, y: moveTo.y }) + if (prevPoint.x !== moveTo.x || prevPoint.y !== moveTo.y) { + points.push({ x: moveTo.x, y: moveTo.y }) + } } } diff --git a/test/toPoints.js b/test/toPoints.js index c72eda2..b1ab38e 100644 --- a/test/toPoints.js +++ b/test/toPoints.js @@ -366,3 +366,20 @@ test('should return correct points of a g', () => { expect(points).toEqual(expectedPoints) }) + +test('should not add an additional point for z if last point equals first', () => { + const shape = { + type: 'path', + d: 'M30,45A5,5,0,0,1,30,55A5,5,0,0,1,30,45Z' + } + + const expectedPoints = [ + { x: 30, y: 45, moveTo: true }, + { x: 30, y: 55, curve: { type: 'arc', rx: 5, ry: 5, sweepFlag: 1 } }, + { x: 30, y: 45, curve: { type: 'arc', rx: 5, ry: 5, sweepFlag: 1 } } + ] + + const points = toPoints(shape) + + expect(points).toEqual(expectedPoints) +})