Skip to content

Commit

Permalink
Merge branch 'map-copy-constructor' into 3.2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Aug 1, 2013
2 parents b563495 + 4b839b6 commit 4c6b28d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 3 additions & 1 deletion d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ d3 = function() {
}
d3.map = function(object) {
var map = new d3_Map();
for (var key in object) map.set(key, object[key]);
if (object instanceof d3_Map) object.forEach(function(key, value) {
map.set(key, value);
}); else for (var key in object) map.set(key, object[key]);
return map;
};
function d3_Map() {}
Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/arrays/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import "../core/class";

d3.map = function(object) {
var map = new d3_Map;
for (var key in object) map.set(key, object[key]);
if (object instanceof d3_Map) object.forEach(function(key, value) { map.set(key, value); });
else for (var key in object) map.set(key, object[key]);
return map;
};

Expand Down
8 changes: 8 additions & 0 deletions test/arrays/map-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ suite.addBatch({
var m = map(Object.create(null, {foo: {value: 42, enumerable: false}}));
assert.isFalse(m.has("foo"));
assert.isUndefined(m.get("foo"));
},
"map(map) copies the given map": function(map) {
var a = map({foo: 42}),
b = map(a);
assert.isTrue(b.has("foo"));
assert.equal(b.get("foo"), 42);
a.set("bar", true);
assert.isFalse(b.has("bar"));
}
},
"forEach": {
Expand Down

0 comments on commit 4c6b28d

Please sign in to comment.