Skip to content

Commit

Permalink
update README; add test; remove obsolete instanceof
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 7, 2021
1 parent 689a476 commit 2bf0fda
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ The returned filtered selection preserves the parents of this selection, but lik

<a name="selection_merge" href="#selection_merge">#</a> <i>selection</i>.<b>merge</b>(<i>other</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/merge.js)

Returns a new selection merging this selection with the specified *other* selection. The returned selection has the same number of groups and the same parents as this selection. Any missing (null) elements in this selection are filled with the corresponding element, if present (not null), from the specified *selection*. (If the *other* selection has additional groups or parents, they are ignored.)
Returns a new selection merging this selection with the specified *other* selection or transition. The returned selection has the same number of groups and the same parents as this selection. Any missing (null) elements in this selection are filled with the corresponding element, if present (not null), from the specified *selection*. (If the *other* selection has additional groups or parents, they are ignored.)

This method is used internally by [*selection*.join](#selection_join) to merge the [enter](#selection_enter) and [update](#selection_data) selections after [binding data](#joining-data). You can also merge explicitly, although note that since merging is based on element index, you should use operations that preserve index, such as [*selection*.select](#selection_select) instead of [*selection*.filter](#selection_filter). For example:

Expand Down
1 change: 0 additions & 1 deletion src/selection/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {Selection} from "./index.js";

export default function(context) {
var selection = context.selection ? context.selection() : context;
if (!(selection instanceof Selection)) throw new Error("invalid merge");

for (var groups0 = this._groups, groups1 = selection._groups, m0 = groups0.length, m1 = groups1.length, m = Math.min(m0, m1), merges = new Array(m0), j = 0; j < m; ++j) {
for (var group0 = groups0[j], group1 = groups1[j], n = group0.length, merge = merges[j] = new Array(n), node, i = 0; i < n; ++i) {
Expand Down
17 changes: 17 additions & 0 deletions test/selection/merge-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,20 @@ it("selection.merge(selection) reuses this selection’s parents", "<parent><chi
assert.strictEqual(selection01._parents, selection0._parents);
assert.strictEqual(selection10._parents, selection1._parents);
});

it("selection.merge(transition) returns a new selection, merging the two selections", "<h1 id='one'>one</h1><h1 id='two'>two</h1>", () => {
const one = document.querySelector("#one");
const two = document.querySelector("#two");
const selection0 = select(document.body).selectAll("h1");
const selection1 = selection0.select(function(d, i) { return i & 1 ? this : null; });
const selection2 = selection0.select(function(d, i) { return i & 1 ? null : this; });
assertSelection(selection1.merge(mockTransition(selection2)), {groups: [[one, two]], parents: [document.body]});
});

function mockTransition(selection) {
return {
selection() {
return selection;
}
};
}

0 comments on commit 2bf0fda

Please sign in to comment.