Skip to content

Commit

Permalink
Don’t copy arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jul 30, 2019
1 parent fe2ed50 commit f13c41a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function(x) {
return Array.isArray(x) ? x : Array.from(x);
}
3 changes: 2 additions & 1 deletion src/selectAll.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import array from "./array.js";
import {Selection, root} from "./selection/index.js";

export default function(selector) {
return typeof selector === "string"
? new Selection([document.querySelectorAll(selector)], [document.documentElement])
: new Selection([selector == null ? [] : Array.from(selector)], root);
: new Selection([selector == null ? [] : array(selector)], root);
}
7 changes: 2 additions & 5 deletions src/selection/data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Selection} from "./index.js";
import {EnterNode} from "./enter.js";
import array from "../array.js";
import constant from "../constant.js";

var keyPrefix = "$"; // Protect against keys like “__proto__”.
Expand Down Expand Up @@ -78,10 +79,6 @@ 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 @@ -95,7 +92,7 @@ export default function(value, key) {
var parent = parents[j],
group = groups[j],
groupLength = group.length,
data = arrayify(value.call(parent, parent && parent.__data__, j, parents)),
data = array(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
2 changes: 1 addition & 1 deletion test/selection/each-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ tape("selection.each(function) skips missing elements", function(test) {
two = document.querySelector("#two"),
selection = d3.selectAll([, one,, two]).datum(function(d, i) { return "node-" + i; });
test.equal(selection.each(function(d, i, nodes) { result.push(this, d, i, nodes); }), selection);
test.deepEqual(result, [one, "node-1", 1, [undefined, one, undefined, two], two, "node-3", 3, [undefined, one, undefined, two]]);
test.deepEqual(result, [one, "node-1", 1, [, one,, two], two, "node-3", 3, [, one,, two]]);
test.end();
});

0 comments on commit f13c41a

Please sign in to comment.