diff --git a/src/toPath.js b/src/toPath.js index 1ff25db..8096550 100644 --- a/src/toPath.js +++ b/src/toPath.js @@ -3,8 +3,7 @@ import toPoints from './toPoints' const pointsToD = p => { let d = '' let i = 0 - - const firstPoint = p[ i ] + let firstPointInCurrentLine for (let point of p) { const isFirstPoint = i === 0 @@ -14,6 +13,7 @@ const pointsToD = p => { if (moveTo || isFirstPoint) { if (!isLastPoint) { + firstPointInCurrentLine = point d += `M${x},${y}` } } else if (curve) { @@ -32,10 +32,10 @@ const pointsToD = p => { break } - if (isLastPoint && x === firstPoint.x && y === firstPoint.y) { + if (isLastPoint && x === firstPointInCurrentLine.x && y === firstPointInCurrentLine.y) { d += 'Z' } - } else if (isLastPoint && x === firstPoint.x && y === firstPoint.y) { + } else if (isLastPoint && x === firstPointInCurrentLine.x && y === firstPointInCurrentLine.y) { d += 'Z' } else if (x !== prevPoint.x && y !== prevPoint.y) { d += `L${x},${y}` diff --git a/test/toPath.js b/test/toPath.js index 289e4d1..4059542 100644 --- a/test/toPath.js +++ b/test/toPath.js @@ -309,3 +309,18 @@ test('should return correct paths from a group of points', () => { expect(paths).toEqual(expectedPaths) }) + +test('does not close path when last point does not match corresponding moveTo point', () => { + const points = [ + { x: 40, y: 80, moveTo: true }, + { x: 50, y: 60 }, + { x: 30, y: 40, moveTo: true }, + { x: 40, y: 80 } + ] + + const expectedPath = 'M40,80L50,60M30,40L40,80' + + const path = toPath(points) + + expect(path).toEqual(expectedPath) +})