diff --git a/modules/actions/flip.js b/modules/actions/flip.js index cfb83279a..6b4ef7ffb 100644 --- a/modules/actions/flip.js +++ b/modules/actions/flip.js @@ -27,8 +27,10 @@ export function actionFlip(wayId, isVertical, projection) { .map(function (nodeId) { return graph.entity(nodeId); }) + // Only process each node once, as the first node will be listed twice in the way + .uniqBy(function (node) { return node.id; }) + // Get distance from midPoint and produce a translated node .map(function (node) { - // Get distance from midPoint const delta = isVertical ? node.loc[1] - midPoint : node.loc[0] - midPoint; diff --git a/test/spec/actions/flip.js b/test/spec/actions/flip.js index 26cbdbf38..925267fbb 100644 --- a/test/spec/actions/flip.js +++ b/test/spec/actions/flip.js @@ -21,14 +21,57 @@ describe('iD.actionFlip', function() { iD.Node({id: 'b', loc: [2, 0]}), iD.Node({id: 'c', loc: [2, 2]}), iD.Node({id: 'd', loc: [0, 2]}), - iD.Way({id: '-', nodes: ['a', 'b', 'c', 'd', 'a'], tags: { area: 'yes'}}) + iD.Way({id: '-', nodes: ['a', 'b', 'c', 'd', 'a'], tags: { area: 'yes'}}) ]); graph = iD.actionFlip('-', false, projection)(graph); - - expect(graph.entity('a').loc[0]).to.equal(2); // A should be 2,0 now - expect(graph.entity('b').loc[0]).to.equal(0); // B should be 0,0 now - expect(graph.entity('c').loc[0]).to.equal(0); // C should be 0,2 now - expect(graph.entity('d').loc[0]).to.equal(2); // D should be 2,0 now + expect(graph.entity('a').loc[0]).to.equal(2); // A should be 2,0 now + expect(graph.entity('b').loc[0]).to.equal(0); // B should be 0,0 now + expect(graph.entity('c').loc[0]).to.equal(0); // C should be 0,2 now + expect(graph.entity('d').loc[0]).to.equal(2); // D should be 2,2 now }); + it('flips horizontally - does not alter y value', function () { + var graph = iD.Graph([ + iD.Node({id: 'a', loc: [0, 0]}), + iD.Node({id: 'b', loc: [2, 0]}), + iD.Node({id: 'c', loc: [2, 2]}), + iD.Node({id: 'd', loc: [0, 2]}), + iD.Way({id: '-', nodes: ['a', 'b', 'c', 'd', 'a'], tags: { area: 'yes'}}) + ]); + graph = iD.actionFlip('-', false, projection)(graph); + expect(graph.entity('a').loc[1]).to.equal(0); // A should be 2,0 now + expect(graph.entity('b').loc[1]).to.equal(0); // B should be 0,0 now + expect(graph.entity('c').loc[1]).to.equal(2); // C should be 0,2 now + expect(graph.entity('d').loc[1]).to.equal(2); // D should be 2,2 now + }); + + it('flips vertically - alters y value', function () { + var graph = iD.Graph([ + iD.Node({id: 'a', loc: [0, 0]}), + iD.Node({id: 'b', loc: [2, 0]}), + iD.Node({id: 'c', loc: [2, 2]}), + iD.Node({id: 'd', loc: [0, 2]}), + iD.Way({id: '-', nodes: ['a', 'b', 'c', 'd', 'a'], tags: { area: 'yes'}}) + ]); + graph = iD.actionFlip('-', true, projection)(graph); + expect(graph.entity('a').loc[1]).to.equal(2); // A should be 0,2 now + expect(graph.entity('b').loc[1]).to.equal(2); // B should be 2,2 now + expect(graph.entity('c').loc[1]).to.equal(0); // C should be 2,0 now + expect(graph.entity('d').loc[1]).to.equal(0); // D should be 0,0 now + }); + + it('flips vertically - does not alter x value', function () { + var graph = iD.Graph([ + iD.Node({id: 'a', loc: [0, 0]}), + iD.Node({id: 'b', loc: [2, 0]}), + iD.Node({id: 'c', loc: [2, 2]}), + iD.Node({id: 'd', loc: [0, 2]}), + iD.Way({id: '-', nodes: ['a', 'b', 'c', 'd', 'a'], tags: { area: 'yes'}}) + ]); + graph = iD.actionFlip('-', true, projection)(graph); + expect(graph.entity('a').loc[0]).to.equal(0); // A should be 0,2 now + expect(graph.entity('b').loc[0]).to.equal(2); // B should be 2,2 now + expect(graph.entity('c').loc[0]).to.equal(2); // C should be 2,0 now + expect(graph.entity('d').loc[0]).to.equal(0); // D should be 0,0 now + }); });