Skip to content

Commit 2bf0fda

Browse files
committed
update README; add test; remove obsolete instanceof
1 parent 689a476 commit 2bf0fda

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ The returned filtered selection preserves the parents of this selection, but lik
198198

199199
<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)
200200

201-
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.)
201+
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.)
202202

203203
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:
204204

src/selection/merge.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {Selection} from "./index.js";
22

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

76
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) {
87
for (var group0 = groups0[j], group1 = groups1[j], n = group0.length, merge = merges[j] = new Array(n), node, i = 0; i < n; ++i) {

test/selection/merge-test.js

+17
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,20 @@ it("selection.merge(selection) reuses this selection’s parents", "<parent><chi
4444
assert.strictEqual(selection01._parents, selection0._parents);
4545
assert.strictEqual(selection10._parents, selection1._parents);
4646
});
47+
48+
it("selection.merge(transition) returns a new selection, merging the two selections", "<h1 id='one'>one</h1><h1 id='two'>two</h1>", () => {
49+
const one = document.querySelector("#one");
50+
const two = document.querySelector("#two");
51+
const selection0 = select(document.body).selectAll("h1");
52+
const selection1 = selection0.select(function(d, i) { return i & 1 ? this : null; });
53+
const selection2 = selection0.select(function(d, i) { return i & 1 ? null : this; });
54+
assertSelection(selection1.merge(mockTransition(selection2)), {groups: [[one, two]], parents: [document.body]});
55+
});
56+
57+
function mockTransition(selection) {
58+
return {
59+
selection() {
60+
return selection;
61+
}
62+
};
63+
}

0 commit comments

Comments
 (0)