1- proposal-array-select-reject
2- ============================
1+ # proposal-array-filtering
32
4- A Stage 1 proposal to add ` Array.prototype.select ` and
5- ` Array.prototype.reject ` .
3+ A proposal to add ` Array.prototype.filterOut ` .
64
75``` js
86const array = [1 , 2 , 3 , 4 , 5 ];
97
10- // Select is just an alias for Array.p.filter
11- array .select (i => (i < 3 )); // => [1, 2];
8+ // filter keeps the items that return true.
9+ array .filter (i => (i < 3 )); // => [1, 2];
1210
13- // Reject does the opposite of select .
14- array .reject (i => (i < 3 )); // => [3, 4, 5];
11+ // filterOut removes the items that return true .
12+ array .filterOut (i => (i < 3 )); // => [3, 4, 5];
1513```
1614
17- Motivation
18- ----------
15+ ## Champions
1916
20- ` Array.prototype.filter ` is confusing. I constantly have to ask myself
21- "am I filtering in, or filtering out the current item?".
17+ - Justin Ridgewell ([ @jridgewell ] ( https://github.com/jridgewell/ ) )
18+
19+ ## Status
20+
21+ Current [ Stage] ( https://tc39.es/process-document/ ) : 1
22+
23+ ## Motivation
24+
25+ ` Array.p.filter ` is confusing. I constantly have to ask myself "am I
26+ keeping, or filtering out the current item?".
2227
2328<dl >
24- <dt >"Filtering in "</dt >
29+ <dt >"Keeping "</dt >
2530 <dd >
2631
2732 Implies that returning ` true ` would keep the current item.
@@ -36,55 +41,32 @@ Motivation
3641 </dd >
3742</dl >
3843
39- ` Array.p.filter ` acts as "filtering in ". But when I think of the word
44+ ` Array.p.filter ` acts as "keeping ". But when I think of the word
4045"filter", I think of "filtering out". So every time that I attempt to
4146write an array filter, I end up writing the opposite of what I intended.
4247
43- ` Array.p.select ` fixes this confusion. You are _ selecting_ the current
44- item. Similarly, ` Array.p.reject ` means you are _ rejecting_ the current
45- item.
46-
47-
48- Semantics
49- ---------
50-
51- ### ` Array.prototype.select(callbackfn[, thisArg ]) `
52-
53- The initial value of the ` select ` property is the same function object
54- as the initial value of the ` Array.prototype.filter ` property.
55-
56- ### ` Array.prototype.reject(callbackfn[, thisArg ]) `
48+ ` Array.p.filterOut ` attempts to fix this confusion. By providing a
49+ clearly named filtering function that matches my intuition, I'm able
50+ what will happen when calling ` filterOut ` . And because it exists, I'm
51+ able to assume that ` filter ` does something different, so it must be
52+ "keep" version.
5753
58- When the ` reject ` method is called with one or two arguments, the
59- following steps are taken:
54+ ## Ongoing Discussions
6055
61- 1 . Let ` O ` be ` ? ToObject(this value) ` .
62- 1 . Let ` len ` be ` ? LengthOfArrayLike(O) ` .
63- 1 . If ` IsCallable(callbackfn) ` is ` false ` , throw a ` TypeError ` exception.
64- 1 . If ` thisArg ` is present, let ` T ` be ` thisArg ` ; else let ` T ` be ` undefined ` .
65- 1 . Let ` A ` be ` ? ArraySpeciesCreate(O, 0) ` .
66- 1 . Let ` k ` be 0.
67- 1 . Let ` to ` be 0.
68- 1 . Repeat, while ` k ` < ; ` len `
69- 1 . Let ` Pk ` be ` ! ToString(k) ` .
70- 1 . Let ` kPresent ` be ` ? HasProperty(O, Pk) ` .
71- 1 . If ` kPresent ` is ` true ` , then
72- 1 . Let ` kValue ` be ` ? Get(O, Pk) ` .
73- 1 . Let ` rejected ` be ` ! ToBoolean(? Call(callbackfn, T, « kValue, k, O »)) ` .
74- 1 . If ` rejected ` is ` false ` , then
75- 1 . Perform ` ? CreateDataPropertyOrThrow(A, ! ToString(to), kValue) ` .
76- 1 . Set ` to ` to ` to + 1 ` .
77- 1 . Set ` k ` to ` k + 1 ` .
78- 1 . Return ` A ` .
56+ - [ Supporting Data from HTTP Archive and GitHub Archive] ( https://github.com/jridgewell/proposal-array-filtering/issues/4 )
57+ - [ What should ` filterOut ` be called?] ( https://github.com/jridgewell/proposal-array-filtering/issues/6 )
58+ - [ Should ` partition ` be included?] ( https://github.com/jridgewell/proposal-array-filtering/issues/2 )
7959
80- Related
81- -------
60+ ## Related
8261
8362- Ruby
8463 - [ ` Array#select ` ] ( https://ruby-doc.org/core-2.4.1/Array.html#method-i-select )
8564 - [ ` Array#reject ` ] ( https://ruby-doc.org/core-2.4.1/Array.html#method-i-reject )
65+ - [ ` Array#partition ` ] ( https://ruby-doc.org/core-2.5.1/Enumerable.html#method-i-partition )
8666- Underscore
8767 - [ ` _.select ` ] ( https://underscorejs.org/#filter ) (aliased to _ .filter)
8868 - [ ` _.reject ` ] ( https://underscorejs.org/#reject )
69+ - [ ` _.partition ` ] ( https://underscorejs.org/#partition )
8970- Lodash
90- - [ ` _.reject ` ] ( https://lodash.com/docs/4.17.15#reject ) ([ 700,000 downloads/week] ( https://www.npmjs.com/package/lodash.reject ) )
71+ - [ ` _.reject ` ] ( https://lodash.com/docs/4.17.15#reject ) ([ 700k downloads/week] ( https://www.npmjs.com/package/lodash.reject ) )
72+ - [ ` _.partition ` ] ( https://lodash.com/docs/4.17.15#partition ) ([ 18k downloads/week] ( https://www.npmjs.com/package/lodash.partition ) )
0 commit comments