Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua committed Sep 19, 2024
1 parent ec6804b commit b36d8ec
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions collections/take_while.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Returns all elements in the given collection until the first element that
* does not match the given predicate.
*
* Note: If you want to process any iterable, use the new version of
* `takeWhile` from `@std/collections/unstable-take-while`.
*
* @typeParam T The type of the array elements.
*
* @param array The array to take elements from.
Expand Down
53 changes: 53 additions & 0 deletions collections/take_while_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,59 @@ Deno.test("takeWhile() returns the same array when all elements match the predic
assertEquals(actual, [1, 2, 3, 4]);
});

Deno.test("(unstable) takeWhile() handles num array", () => {
const arr = [1, 2, 3, 4, 5, 6];
const actual = unstableTakeWhile(arr, (i) => i !== 4);

assertEquals(actual, [1, 2, 3]);
});

Deno.test("(unstable) takeWhile() adds two to each num in predicate", () => {
const arr = [1, 2, 3, 4, 5, 6];
const actual = unstableTakeWhile(arr, (i) => i + 2 !== 6);

assertEquals(actual, [1, 2, 3]);
});

Deno.test("(unstable) takeWhile() handles negatives", () => {
const arr = [-1, -2, -3, -4, -5, -6];

const actual = unstableTakeWhile(arr, (i) => i > -4);
assertEquals(actual, [-1, -2, -3]);
});

Deno.test("(unstable) takeWhile() handles no mutation", () => {
const arr = [-1, -2, -3, -4, -5, -6];

const actual = unstableTakeWhile(arr, (i) => i > -4);
assertEquals(actual, [-1, -2, -3]);
assertEquals(arr, [-1, -2, -3, -4, -5, -6]);
});

Deno.test("(unstable) takeWhile() handles empty input array returns empty array", () => {
const arr: number[] = [];

const actual = unstableTakeWhile(arr, (i) => i > 4);

assertEquals(actual, []);
});

Deno.test("(unstable) takeWhile() returns empty array when the first element doesn't match the predicate", () => {
const arr = [1, 2, 3, 4];

const actual = unstableTakeWhile(arr, (i) => i !== 1);

assertEquals(actual, []);
});

Deno.test("(unstable) takeWhile() returns the same array when all elements match the predicate", () => {
const arr = [1, 2, 3, 4];

const actual = unstableTakeWhile(arr, (i) => i !== 400);

assertEquals(actual, [1, 2, 3, 4]);
});

Deno.test("(unstable) takeWhile() handles a generator", () => {
function* infiniteCount() {
let count = 0;
Expand Down
2 changes: 2 additions & 0 deletions collections/unstable_take_while.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Returns all elements in the given collection 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 iterable.
*
* @param iterable The iterable to take elements from.
Expand Down

0 comments on commit b36d8ec

Please sign in to comment.