feat(collections/unstable): support Iterable argument in dropWhile() (#6088)

This commit is contained in:
Liam Tait 2024-10-09 03:05:25 +13:00 committed by GitHub
parent 0312678936
commit ce4d7c5e87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 152 additions and 0 deletions

View File

@ -47,6 +47,7 @@
"./take-while": "./take_while.ts",
"./union": "./union.ts",
"./unstable-chunk": "./unstable_chunk.ts",
"./unstable-drop-while": "./unstable_drop_while.ts",
"./unstable-drop-last-while": "./unstable_drop_last_while.ts",
"./unstable-intersect": "./unstable_intersect.ts",
"./unstable-sample": "./unstable_sample.ts",

View File

@ -0,0 +1,49 @@
// 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
* first element that does not match the given predicate.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @typeParam T The type of the elements in the input array.
*
* @param array 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 first element that
* does not match the given predicate.
*
* @example Basic usage
* ```ts
* import { dropWhile } from "@std/collections/unstable-drop-while";
* import { assertEquals } from "@std/assert";
*
* const numbers = [3, 2, 5, 2, 5];
* const dropWhileNumbers = dropWhile(numbers, (number) => number !== 2);
*
* assertEquals(dropWhileNumbers, [2, 5, 2, 5]);
* ```
*/
export function dropWhile<T>(
iterable: Iterable<T>,
predicate: (el: T) => boolean,
): T[] {
if (Array.isArray(iterable)) {
const idx = iterable.findIndex((el) => !predicate(el));
if (idx === -1) {
return [];
}
return iterable.slice(idx);
}
const array: T[] = [];
let found = false;
for (const item of iterable) {
if (found || !predicate(item)) {
found = true;
array.push(item);
}
}
return array;
}

View File

@ -0,0 +1,102 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "@std/assert";
import { dropWhile } from "./unstable_drop_while.ts";
Deno.test("(unstable) dropWhile matches first element", () => {
const arr = [1, 2, 3, 4, 5, 6];
const actual = dropWhile(arr, (i) => i !== 1);
assertEquals(actual, [1, 2, 3, 4, 5, 6]);
});
Deno.test("(unstable) dropWhile() handles Array", () => {
const arr = [1, 2, 3, 4, 5, 6];
const actual = dropWhile(arr, (i) => i !== 2);
assertEquals(actual, [2, 3, 4, 5, 6]);
});
Deno.test("(unstable) dropWhile() adds two to each num in predicate", () => {
const arr = [1, 2, 3, 4, 5, 6];
const actual = dropWhile(arr, (i) => i + 2 !== 6);
assertEquals(actual, [4, 5, 6]);
});
Deno.test("(unstable) dropWhile() handles negatives", () => {
const arr = [-5, -6];
const actual = dropWhile(arr, (i) => i < -4);
assertEquals(actual, []);
});
Deno.test("(unstable) dropWhile() handles no mutation", () => {
const arr = [1, 2, 3, 4, 5, 6];
const actual = dropWhile(arr, (i) => i !== 4);
assertEquals(actual, [4, 5, 6]);
assertEquals(arr, [1, 2, 3, 4, 5, 6]);
});
Deno.test("(unstable) dropWhile() handles empty input array returns empty array", () => {
const arr: number[] = [];
const actual = dropWhile(arr, (i) => i > 4);
assertEquals(actual, []);
});
Deno.test("(unstable) dropWhile() returns empty array when the last element doesn't match the predicate", () => {
const arr = [1, 2, 3, 4];
const actual = dropWhile(arr, (i) => i !== 4);
assertEquals(actual, [4]);
});
Deno.test("(unstable) dropWhile() returns the same array when all elements match the predicate", () => {
const arr = [1, 2, 3, 4];
const actual = dropWhile(arr, (i) => i !== 400);
assertEquals(actual, []);
});
Deno.test("(unstable) dropWhile() handles a generator", () => {
function* gen() {
yield 1;
yield 2;
yield 3;
yield 4;
yield 5;
yield 6;
}
const actual = dropWhile(gen(), (i) => i !== 4);
assertEquals(actual, [4, 5, 6]);
});
Deno.test("(unstable) dropWhile() handles a Set", () => {
const set = new Set([1, 2, 3, 4, 5, 6]);
const actual = dropWhile(set, (i) => i !== 4);
assertEquals(actual, [4, 5, 6]);
});
Deno.test("(unstable) dropWhile() handles a Map", () => {
const map = new Map([
["a", 1],
["b", 2],
["c", 3],
["d", 4],
["e", 5],
["f", 6],
]);
const actual = dropWhile(map, ([_k, v]) => v !== 4);
assertEquals(actual, [
["d", 4],
["e", 5],
["f", 6],
]);
});