mirror of
https://github.com/denoland/std.git
synced 2024-11-21 12:40:03 +00:00
feat(collections/unstable): support Iterable
argument in dropLastWhile()
(#6076)
This commit is contained in:
parent
bfbc53e2ca
commit
8abe412340
@ -47,6 +47,7 @@
|
||||
"./take-while": "./take_while.ts",
|
||||
"./union": "./union.ts",
|
||||
"./unstable-chunk": "./unstable_chunk.ts",
|
||||
"./unstable-drop-last-while": "./unstable_drop_last_while.ts",
|
||||
"./unstable-sample": "./unstable_sample.ts",
|
||||
"./unstable-sort-by": "./unstable_sort_by.ts",
|
||||
"./unstable-take-while": "./unstable_take_while.ts",
|
||||
|
38
collections/unstable_drop_last_while.ts
Normal file
38
collections/unstable_drop_last_while.ts
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
// This module is browser compatible.
|
||||
|
||||
/**
|
||||
* Returns an array that drops all elements in the given iterable until the
|
||||
* last element that does not match the given predicate.
|
||||
*
|
||||
* @typeParam T The type of the elements in the input array.
|
||||
*
|
||||
* @param iterable The iterable to drop elements from.
|
||||
* @param predicate The function to test each element for a condition.
|
||||
*
|
||||
* @returns An array that drops all elements until the last element that does
|
||||
* not match the given predicate.
|
||||
*
|
||||
* @example Basic usage
|
||||
* ```ts
|
||||
* import { dropLastWhile } from "@std/collections/unstable-drop-last-while";
|
||||
* import { assertEquals } from "@std/assert";
|
||||
*
|
||||
* const numbers = [11, 42, 55, 20, 33, 44];
|
||||
*
|
||||
* const notFortyFour = dropLastWhile(numbers, (number) => number > 30);
|
||||
*
|
||||
* assertEquals(notFortyFour, [11, 42, 55, 20]);
|
||||
* ```
|
||||
*/
|
||||
export function dropLastWhile<T>(
|
||||
iterable: Iterable<T>,
|
||||
predicate: (el: T) => boolean,
|
||||
): T[] {
|
||||
const array = Array.isArray(iterable) ? iterable : Array.from(iterable);
|
||||
let offset = array.length - 1;
|
||||
while (offset >= 0 && predicate(array[offset]!)) {
|
||||
offset--;
|
||||
}
|
||||
return array.slice(0, offset + 1);
|
||||
}
|
72
collections/unstable_drop_last_while_test.ts
Normal file
72
collections/unstable_drop_last_while_test.ts
Normal file
@ -0,0 +1,72 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
import { dropLastWhile } from "./unstable_drop_last_while.ts";
|
||||
import { assertEquals } from "@std/assert";
|
||||
|
||||
Deno.test("(unstable) dropLastWhile() handles num array", () => {
|
||||
const values = [20, 33, 44];
|
||||
const actual = dropLastWhile(values, (i) => i > 30);
|
||||
assertEquals(actual, [20]);
|
||||
});
|
||||
|
||||
Deno.test("(unstable) dropLastWhile() does not mutate the input array", () => {
|
||||
const array = [1, 2, 3, 4, 5, 6];
|
||||
const actual = dropLastWhile(array, (i) => i > 4);
|
||||
assertEquals(actual, [1, 2, 3, 4]);
|
||||
assertEquals(array, [1, 2, 3, 4, 5, 6]);
|
||||
});
|
||||
|
||||
Deno.test("(unstable) dropLastWhile() handles negatives", () => {
|
||||
const array = [-1, -2, -3, -4, -5, -6];
|
||||
const actual = dropLastWhile(array, (i) => i < -4);
|
||||
assertEquals(actual, [-1, -2, -3, -4]);
|
||||
});
|
||||
|
||||
Deno.test("(unstable) dropLastWhile() handles empty input returns empty array", () => {
|
||||
const array: number[] = [];
|
||||
const actual = dropLastWhile(array, (i) => i > 4);
|
||||
assertEquals(actual, []);
|
||||
});
|
||||
|
||||
Deno.test("(unstable) dropLastWhile() returns same array when the last element doesn't get dropped", () => {
|
||||
const array = [40, 30, 20];
|
||||
const actual = dropLastWhile(array, (i) => i > 40);
|
||||
assertEquals(actual, [40, 30, 20]);
|
||||
});
|
||||
|
||||
Deno.test("(unstable) dropLastWhile() returns empty array when all elements get dropped", () => {
|
||||
const array = [20, 30, 20];
|
||||
const actual = dropLastWhile(array, (i) => i < 40);
|
||||
assertEquals(actual, []);
|
||||
});
|
||||
|
||||
Deno.test("(unstable) dropLastWhile() handles a string", () => {
|
||||
const values = "hello there world";
|
||||
const actual = dropLastWhile(values, (i) => i !== " ");
|
||||
assertEquals(actual, "hello there ".split(""));
|
||||
});
|
||||
|
||||
Deno.test("(unstable) dropLastWhile() handles a Set", () => {
|
||||
const values = new Set([20, 33, 44]);
|
||||
const actual = dropLastWhile(values, (i) => i > 30);
|
||||
assertEquals(actual, [20]);
|
||||
});
|
||||
|
||||
Deno.test("(unstable) dropLastWhile() handles a Map", () => {
|
||||
const values = new Map([
|
||||
["a", 20],
|
||||
["b", 33],
|
||||
["c", 44],
|
||||
]);
|
||||
const actual = dropLastWhile(values, ([_k, v]) => v > 30);
|
||||
assertEquals(actual, [["a", 20]]);
|
||||
});
|
||||
|
||||
Deno.test("(unstable) dropLastWhile() handles a generator", () => {
|
||||
function* gen() {
|
||||
yield 20;
|
||||
yield 33;
|
||||
yield 44;
|
||||
}
|
||||
const actual = dropLastWhile(gen(), (i) => i > 30);
|
||||
assertEquals(actual, [20]);
|
||||
});
|
Loading…
Reference in New Issue
Block a user