Skip to content

Commit

Permalink
Revert "Change selection.filter to preserve index."
Browse files Browse the repository at this point in the history
This reverts commit 0c252f1.
  • Loading branch information
mbostock committed Jun 14, 2016
1 parent 3820d48 commit f1c87ca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Unlike [*selection*.select](#selection_select), *selection*.selectAll does affec

<a name="selection_filter" href="#selection_filter">#</a> <i>selection</i>.<b>filter</b>(<i>filter</i>)

Filters the selection, returning a new selection that contains only the elements for which the specified *filter* is true. The returned filtered selection preserves the index and parents of this selection, using null to represent missing (filtered-out) elements. The *filter* may be specified either as a selector string or a function. If a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element.
Filters the selection, returning a new selection that contains only the elements for which the specified *filter* is true. The *filter* may be specified either as a selector string or a function. If a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element.

For example, to filter a selection of table rows to contain only even rows:

Expand Down Expand Up @@ -180,6 +180,8 @@ var even = d3.selectAll("tr").select(function(d, i) { return i & 1 ? this : null

Note that the `:nth-child` pseudo-class is a one-based index rather than a zero-based index. Also, the above filter functions do not have precisely the same meaning as `:nth-child`; they rely on the selection index rather than the number of preceeding sibling elements in the DOM.

The returned filtered selection preserves the parents of this selection, but like [*array*.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), it does not preserve indexes as some elements may be removed; use [*selection*.select](#selection_select) to preserve the index, if needed.

<a name="selection_merge" href="#selection_merge">#</a> <i>selection</i>.<b>merge</b>(<i>other</i>)

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.)
Expand All @@ -198,7 +200,7 @@ circle.enter().append("circle") // ENTER
.style("stroke", "black");
```

This method is also useful for merging [filtered](#selection_filter) selections because filtered selections retain the index structure of the originating selection. Note, however, that this method is not useful for concatenating arbitrary selections: if both this selection and the specified *other* selection have (non-null) elements at the same index, this selection’s element is returned in the merge and the *other* selection’s element is ignored.
This method is not intended for concatenating arbitrary selections, however: if both this selection and the specified *other* selection have (non-null) elements at the same index, this selection’s element is returned in the merge and the *other* selection’s element is ignored.

<a name="matcher" href="#matcher">#</a> d3.<b>matcher</b>(<i>selector</i>)

Expand Down
4 changes: 2 additions & 2 deletions src/selection/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export default function(match) {
if (typeof match !== "function") match = matcher(match);

for (var groups = this._groups, m = groups.length, subgroups = new Array(m), j = 0; j < m; ++j) {
for (var group = groups[j], n = group.length, subgroup = subgroups[j] = new Array(n), node, i = 0; i < n; ++i) {
for (var group = groups[j], n = group.length, subgroup = subgroups[j] = [], node, i = 0; i < n; ++i) {
if ((node = group[i]) && match.call(node, node.__data__, i, group)) {
subgroup[i] = node;
subgroup.push(node);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions test/selection/filter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tape("selection.filter(string) retains the selected elements that matches the se
var document = jsdom.jsdom("<h1><span id='one'></span><span id='two'></span></h1><h1><span id='three'></span><span id='four'></span></h1>"),
one = document.querySelector("#one"),
three = document.querySelector("#three");
test.deepEqual(d3.select(document).selectAll("span").filter("#one,#three"), {_groups: [[one,, three, ]], _parents: [document]});
test.deepEqual(d3.select(document).selectAll("span").filter("#one,#three"), {_groups: [[one, three]], _parents: [document]});
test.end();
});

Expand All @@ -22,7 +22,7 @@ tape("selection.filter(function) retains elements for which the given function r
two = document.querySelector("#two"),
three = document.querySelector("#three"),
four = document.querySelector("#four");
test.deepEqual(d3.selectAll([one, two, three, four]).filter(function(d, i) { return i & 1; }), {_groups: [[, two,, four]], _parents: [null]});
test.deepEqual(d3.selectAll([one, two, three, four]).filter(function(d, i) { return i & 1; }), {_groups: [[two, four]], _parents: [null]});
test.end();
});

Expand Down Expand Up @@ -69,12 +69,12 @@ tape("selection.filter(…) can filter elements when the originating selection i
test.end();
});

tape("selection.filter(…) skips missing originating elements and retains the original indexes", function(test) {
tape("selection.filter(…) skips missing originating elements and does not retain the original indexes", function(test) {
var document = jsdom.jsdom("<h1>hello</h1>"),
h1 = document.querySelector("h1");
test.deepEqual(d3.selectAll([, h1]).filter("*"), {_groups: [[, h1]], _parents: [null]});
test.deepEqual(d3.selectAll([null, h1]).filter("*"), {_groups: [[, h1]], _parents: [null]});
test.deepEqual(d3.selectAll([undefined, h1]).filter("*"), {_groups: [[, h1]], _parents: [null]});
test.deepEqual(d3.selectAll([, h1]).filter("*"), {_groups: [[h1]], _parents: [null]});
test.deepEqual(d3.selectAll([null, h1]).filter("*"), {_groups: [[h1]], _parents: [null]});
test.deepEqual(d3.selectAll([undefined, h1]).filter("*"), {_groups: [[h1]], _parents: [null]});
test.end();
});

Expand All @@ -84,6 +84,6 @@ tape("selection.filter(…) skips missing originating elements when the originat
two = document.querySelector("#two"),
three = document.querySelector("#three"),
four = document.querySelector("#four");
test.deepEqual(d3.selectAll([one, two]).selectAll("child").select(function(d, i) { return i & 1 ? this : null; }).filter("*"), {_groups: [[, three], [, four]], _parents: [one, two]});
test.deepEqual(d3.selectAll([one, two]).selectAll("child").select(function(d, i) { return i & 1 ? this : null; }).filter("*"), {_groups: [[three], [four]], _parents: [one, two]});
test.end();
});

0 comments on commit f1c87ca

Please sign in to comment.