From 91b4bbde6d3d8eb579e7f8c7f6b615e8640b79ea Mon Sep 17 00:00:00 2001 From: Wenlu Wang Date: Wed, 11 Nov 2020 16:01:51 +0800 Subject: [PATCH 1/2] Add find index right --- README.md | 44 +++++++++++++++++++++++++++++++------------- index.html | 9 +++++++-- spec.emu | 18 +++++++++++++++++- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2fcacc8..3650f73 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # [proposal-array-find-right](https://kingwl.github.io/proposal-array-find-right/index.html) -Proposal for a `.findRight()` method on array. +Proposal for `.findRight()` and `.findIndex` methods on array. ## Motivation @@ -8,28 +8,34 @@ Find something from array is a very common pattern in development. Now we have `Array.prototype.indexOf`, `Array.prototype.lastIndexOf` to find index of some `value` in the array. -And we also has `Array.prototype.find` to find some element who in the array in out customized way. +And we also has `Array.prototype.find`, `Array.prototype.findIndex` to find some element or index who in the array in out customized way. But There’s not a way to allow us find something `from the end to the start ` of array in customized way. `[].reverse().find()` is work. But there’s two issues: -1. unnecessary reverse. -2. `Array.prototype.reverse` is not immutable. +1. **unnecessary reverse.** +2. **`Array.prototype.reverse` is not immutable.** -You have to write the `findRight` in your codebase or `[...[]].reverse().find()`. +You have to write `[...[]].reverse().find()`. -As the result the third issue: +As the result, the third issue: -3. unnecessary spread +3. **unnecessary spread** -So, perhaps we need `Array.prototype.findRight`. +For `.findIndex()`, you must have much additional operations (re-calculate index and handle the `-1` ) for the result of `[...arr].reverse().findIndex()`. + +As the result, the fourth issue: + + 4. **complex index calculate** + +So, perhaps we need `Array.prototype.findRight` and `Array.prototype.findIndexRight`. ## Core features -Add `Array.prototype.findRight`. +Add `Array.prototype.findRight` and `Array.prototype.findIndexRight`. -And we could use that like the [(Array.prototype.find)](https://www.ecma-international.org/ecma-262/11.0/index.html#sec-array.prototype.find) but from the end to be start. +And we could use that like the [Array.prototype.find](https://www.ecma-international.org/ecma-262/11.0/index.html#sec-array.prototype.find) and [Array.prototype.findIndex](https://www.ecma-international.org/ecma-262/11.0/index.html#sec-array.prototype.findindex) but from the end to be start. eg: @@ -37,13 +43,24 @@ eg: const array = [{ value: 1 }, { value: 2 }, { value: 3 }, { value: 4 }]; array.find(n => n.value % 2 === 1); // { value: 1 } +array.findIndex(n => n.value % 2 === 1); // 0 -// Before the proposal +// ======== Before the proposal =========== + +// find [...array].reverse().find(n => n.value % 2 === 1); // { value: 3 } -// In the proposal +// findIndex +array.length - 1 - [...array].reverse().findIndex(n => n.value % 2 === 1); // 2 +array.length - 1 - [...array].reverse().findIndex(n => n.value === 42); // should be -1, but 4 + +// ======== In the proposal =========== +// find array.findRight(n => n.value % 2 === 1); // { value: 3 } +// findIndex +array.findIndexRight(n => n.value % 2 === 1); // 2 +array.findIndexRight(n => n.value === 42); // -1 ``` @@ -51,7 +68,8 @@ array.findRight(n => n.value % 2 === 1); // { value: 3 } - [Array.prototype.reduceRight](https://www.ecma-international.org/ecma-262/11.0/index.html#sec-array.prototype.reduceright) - [lodash.findLast](https://lodash.com/docs/4.17.15#findLast) +- [lodash.findLastIndex](https://lodash.com/docs/4.17.15#findLastIndex) - [ramda.findLast](https://ramdajs.com/docs/#findLast) - +- [ramda.findLastIndex](https://ramdajs.com/docs/#findLastIndex) - [@extra-array/find-right](https://www.npmjs.com/package/@extra-array/find-right) diff --git a/index.html b/index.html index 761dfe6..7b6b251 100644 --- a/index.html +++ b/index.html @@ -1845,12 +1845,17 @@ display: none; } } -

Stage 0 Draft / November 10, 2020

Proposal Array.prototype.findRight

+

Stage 0 Draft / November 11, 2020

Proposal Array.prototype.findRight

1 Array.prototype.findRight

-

The find method is called with one or two arguments, predicate and thisArg.

+

The findRight method is called with one or two arguments, predicate and thisArg.

+

When the findRight method is called, the following steps are taken:

  1. Let O be ? ToObject(this value).
  2. Let idx be ? LengthOfArrayLike(O).
  3. If IsCallable(predicate) is false, throw a TypeError exception.
  4. Repeat, while idx >= 0
    1. Let Pi be ! ToString(idx).
    2. Let iValue be ? Get(O, Pi).
    3. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « iValue, idx, O »)).
    4. If testResult is true, return iValue.
    5. Set idx to idx - 1
  5. Return undefined
+

Array.prototype.findIndexRight

+

The findIndexRight method is called with one or two arguments, predicate and thisArg.

+

When the findIndexRight method is called with one or two arguments, the following steps are taken:

+
  1. Let O be ? ToObject(this value).
  2. Let idx be ? LengthOfArrayLike(O).
  3. If IsCallable(predicate) is false, throw a TypeError exception.
  4. Repeat, while idx >= 0
    1. Let Pi be ! ToString(idx).
    2. Let iValue be ? Get(O, Pi).
    3. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « iValue, idx, O »)).
    4. If testResult is true, return idx.
    5. Set idx to idx - 1
  5. Return -1

A Copyright & Software License

diff --git a/spec.emu b/spec.emu index 9527893..3a45472 100644 --- a/spec.emu +++ b/spec.emu @@ -11,7 +11,8 @@ contributors: Wenlu Wang

Array.prototype.findRight

-

The find method is called with one or two arguments, predicate and thisArg.

+

The findRight method is called with one or two arguments, predicate and thisArg.

+

When the findRight method is called, the following steps are taken:

1. Let O be ? ToObject(this value). 2. Let idx be ? LengthOfArrayLike(O). @@ -24,4 +25,19 @@ contributors: Wenlu Wang 5. Set idx to idx - 1 5. Return undefined +

Array.prototype.findIndexRight

+

The findIndexRight method is called with one or two arguments, predicate and thisArg.

+

When the findIndexRight method is called with one or two arguments, the following steps are taken:

+ + 1. Let O be ? ToObject(this value). + 2. Let idx be ? LengthOfArrayLike(O). + 3. If IsCallable(predicate) is false, throw a TypeError exception. + 4. Repeat, while idx >= 0 + 1. Let Pi be ! ToString(idx). + 2. Let iValue be ? Get(O, Pi). + 3. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « iValue, idx, O »)). + 4. If testResult is true, return idx. + 5. Set idx to idx - 1 + 5. Return -1 +
From 3b952d40457861d3ca12d68755cbe594adb4a674 Mon Sep 17 00:00:00 2001 From: Wenlu Wang Date: Wed, 11 Nov 2020 16:03:01 +0800 Subject: [PATCH 2/2] Fix ident --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3650f73..877f047 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ For `.findIndex()`, you must have much additional operations (re-calculate inde As the result, the fourth issue: - 4. **complex index calculate** +4. **complex index calculate** So, perhaps we need `Array.prototype.findRight` and `Array.prototype.findIndexRight`.