Skip to content

Commit

Permalink
Fix #204 - accept iterable in selection.data.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jul 29, 2019
1 parent 54f6e6b commit 263a801
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/selection/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ function datum(node) {
return node.__data__;
}

function arrayify(data) {
return Array.isArray(data) ? data : Array.from(data);
}

export default function(value, key) {
if (!value) return Array.from(this, datum);

Expand All @@ -91,7 +95,7 @@ export default function(value, key) {
var parent = parents[j],
group = groups[j],
groupLength = group.length,
data = value.call(parent, parent && parent.__data__, j, parents),
data = arrayify(value.call(parent, parent && parent.__data__, j, parents)),
dataLength = data.length,
enterGroup = enter[j] = new Array(dataLength),
updateGroup = update[j] = new Array(dataLength),
Expand Down
7 changes: 7 additions & 0 deletions test/selection/data-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ tape("selection.data(values) binds the specified values to the selected elements
test.end();
});

tape("selection.data(values) accepts an iterable", function(test) {
var body = jsdom("<div id='one'></div><div id='two'></div><div id='three'></div>").body,
selection = d3.select(body).selectAll("div").data(new Set(["foo", "bar", "baz"]));
test.deepEqual(selection.data(), ["foo", "bar", "baz"]);
test.end();
});

tape("selection.data() returns the bound data", function(test) {
var body = jsdom("<div id='one'></div><div id='two'></div><div id='three'></div>").body,
selection = d3.select(body).selectAll("div").data(["foo", "bar", "baz"]);
Expand Down

0 comments on commit 263a801

Please sign in to comment.