Don't mutate entity in place

This commit is contained in:
John Firebaugh
2012-12-04 13:30:29 -05:00
parent 103678b36d
commit 60e7698f1a
2 changed files with 11 additions and 3 deletions

View File

@@ -88,13 +88,12 @@ iD.Graph.prototype = {
// Resolve the id references in a way, replacing them with actual objects.
fetch: function(id) {
var entity = iD.Entity(this.entities[id]), nodes = [];
var entity = this.entities[id], nodes = [];
if (!entity.nodes || !entity.nodes.length) return entity;
for (var i = 0, l = entity.nodes.length; i < l; i++) {
nodes[i] = this.fetch(entity.nodes[i]);
}
entity.nodes = nodes;
return entity;
return iD.Entity(entity, {nodes: nodes});
},
modifications: function() {

View File

@@ -54,6 +54,15 @@ describe('Graph', function() {
});
});
describe("#fetch", function () {
it("replaces node ids with references", function () {
var node = iD.Node({id: "n1"}),
way = iD.Way({id: "w1", nodes: ["n1"]}),
graph = iD.Graph({n1: node, w1: way});
expect(graph.fetch("w1").nodes).to.eql([node]);
});
});
describe("#modifications", function () {
it("filters entities by modified", function () {
var a = {id: 'a', modified: function () { return true; }},