From e24b22b518b35778c0e9344d6470bc0858637e9a Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 17 Jan 2013 09:57:22 -0800 Subject: [PATCH] Extract iD.svg.LineString --- js/id/svg.js | 16 ++++++++++++++++ js/id/svg/areas.js | 12 +----------- js/id/svg/lines.js | 10 +--------- test/index.html | 1 + test/index_packaged.html | 1 + test/spec/svg.js | 10 ++++++++++ 6 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 test/spec/svg.js diff --git a/js/id/svg.js b/js/id/svg.js index e1d26e058..bf73f32b8 100644 --- a/js/id/svg.js +++ b/js/id/svg.js @@ -9,5 +9,21 @@ iD.svg = { return function (entity) { return 'translate(' + projection(entity.loc) + ')'; }; + }, + + LineString: function (projection) { + var cache = {}; + return function (entity) { + if (cache[entity.id] !== undefined) { + return cache[entity.id]; + } + + if (entity.nodes.length === 0) { + return (cache[entity.id] = ''); + } + + return (cache[entity.id] = + 'M' + entity.nodes.map(function (n) { return projection(n.loc); }).join('L')); + } } }; diff --git a/js/id/svg/areas.js b/js/id/svg/areas.js index 4fe06ba92..3862aca0c 100644 --- a/js/id/svg/areas.js +++ b/js/id/svg/areas.js @@ -38,17 +38,7 @@ iD.svg.Areas = function(projection) { areas.sort(areastack); - var lineStrings = {}; - - function lineString(entity) { - if (lineStrings[entity.id] !== undefined) { - return lineStrings[entity.id]; - } - var nodes = _.pluck(entity.nodes, 'loc'); - if (nodes.length === 0) return (lineStrings[entity.id] = ''); - else return (lineStrings[entity.id] = - 'M' + nodes.map(projection).join('L')); - } + var lineString = iD.svg.LineString(projection); function drawPaths(group, areas, filter, classes) { var paths = group.selectAll('path') diff --git a/js/id/svg/lines.js b/js/id/svg/lines.js index 4a2acfe38..167a9e75e 100644 --- a/js/id/svg/lines.js +++ b/js/id/svg/lines.js @@ -73,15 +73,7 @@ iD.svg.Lines = function(projection) { lines.sort(waystack); - function lineString(entity) { - if (lineStrings[entity.id] !== undefined) { - return lineStrings[entity.id]; - } - var nodes = _.pluck(entity.nodes, 'loc'); - if (nodes.length === 0) return (lineStrings[entity.id] = ''); - else return (lineStrings[entity.id] = - 'M' + nodes.map(projection).join('L')); - } + var lineString = iD.svg.LineString(projection); var casing = surface.select('.layer-casing'), stroke = surface.select('.layer-stroke'), diff --git a/test/index.html b/test/index.html index 195b459f6..96095a244 100644 --- a/test/index.html +++ b/test/index.html @@ -151,6 +151,7 @@ + diff --git a/test/index_packaged.html b/test/index_packaged.html index 92c61df79..cf333b416 100644 --- a/test/index_packaged.html +++ b/test/index_packaged.html @@ -60,6 +60,7 @@ + diff --git a/test/spec/svg.js b/test/spec/svg.js new file mode 100644 index 000000000..bbb78c990 --- /dev/null +++ b/test/spec/svg.js @@ -0,0 +1,10 @@ +describe("iD.svg.LineString", function () { + it("returns an SVG path description for the entity's nodes", function () { + var a = iD.Node({loc: [0, 0]}), + b = iD.Node({loc: [2, 3]}), + way = iD.Way({nodes: [a, b]}), + projection = Object; + + expect(iD.svg.LineString(projection)(way)).to.equal("M0,0L2,3"); + }); +});