mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
revert(collection): Remove findLastIndex from std/collections (#1528)
This commit is contained in:
parent
251ad20639
commit
68f093fb71
@ -279,21 +279,6 @@ assertEquals(
|
||||
);
|
||||
```
|
||||
|
||||
### findLastIndex
|
||||
|
||||
Returns the index of the last element in the given array matching the given
|
||||
predicate.
|
||||
|
||||
```ts
|
||||
import { findLastIndex } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";
|
||||
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
|
||||
|
||||
const numbers = [0, 1, 2, 3, 4, 5, 6];
|
||||
const lastIndexEvenNumber = findLastIndex(numbers, (it) => it % 2 === 0);
|
||||
|
||||
assertEquals(lastIndexEvenNumber, 6);
|
||||
```
|
||||
|
||||
### findSingle
|
||||
|
||||
Returns an element if and only if that element is the only one matching the
|
||||
|
@ -1,34 +0,0 @@
|
||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
// This module is browser compatible.
|
||||
|
||||
/**
|
||||
* @deprecated Use `Array.prototype.findLastIndex`. This function will be removed in std@0.117.0.
|
||||
*
|
||||
* Returns the index of the last element in the given array matching the given predicate
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```ts
|
||||
* import { findLastIndex } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";
|
||||
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
|
||||
*
|
||||
* const numbers = [ 4, 2, 7 ]
|
||||
* const lastIndexNumber = findLastIndex(numbers, it => it % 2 === 0)
|
||||
*
|
||||
* assertEquals(lastIndexNumber, 1)
|
||||
* ```
|
||||
*/
|
||||
export function findLastIndex<T>(
|
||||
array: readonly T[],
|
||||
predicate: (el: T) => boolean,
|
||||
): number | undefined {
|
||||
for (let i = array.length - 1; i >= 0; i -= 1) {
|
||||
const element = array[i];
|
||||
|
||||
if (predicate(element)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import { assertEquals } from "../testing/asserts.ts";
|
||||
import { findLastIndex } from "./find_last_index.ts";
|
||||
|
||||
function findLastIndexTest<I>(
|
||||
input: [Array<I>, (element: I) => boolean],
|
||||
expected: number | undefined,
|
||||
message?: string,
|
||||
) {
|
||||
const actual = findLastIndex(...input);
|
||||
assertEquals(actual, expected, message);
|
||||
}
|
||||
|
||||
Deno.test({
|
||||
name: "[collections/findLastIndex] empty input",
|
||||
fn() {
|
||||
findLastIndexTest([[], (_) => true], undefined);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[collections/findLastIndex] no matches",
|
||||
fn() {
|
||||
findLastIndexTest([[9, 11, 13], (it) => it % 2 === 0], undefined);
|
||||
findLastIndexTest([["foo", "bar"], (it) => it.startsWith("z")], undefined);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[collections/findLastIndex] only match",
|
||||
fn() {
|
||||
findLastIndexTest([[9, 12, 13], (it) => it % 2 === 0], 1);
|
||||
findLastIndexTest([["zap", "foo", "bar"], (it) => it.startsWith("z")], 0);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[collections/findLastIndex] multiple matches",
|
||||
fn() {
|
||||
findLastIndexTest([[0, 1, 2, 3, 4, 5, 6], (it) => it % 2 === 0], 6);
|
||||
findLastIndexTest(
|
||||
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], (it) => it % 2 === 0],
|
||||
8,
|
||||
);
|
||||
},
|
||||
});
|
@ -11,7 +11,6 @@ export * from "./drop_while.ts";
|
||||
export * from "./filter_entries.ts";
|
||||
export * from "./filter_keys.ts";
|
||||
export * from "./filter_values.ts";
|
||||
export * from "./find_last_index.ts";
|
||||
export * from "./group_by.ts";
|
||||
export * from "./intersect.ts";
|
||||
export * from "./map_entries.ts";
|
||||
|
Loading…
Reference in New Issue
Block a user