Skip to content

Commit

Permalink
feat(collections): add dropLastWhile (#1193)
Browse files Browse the repository at this point in the history
  • Loading branch information
grian32 committed Sep 1, 2021
1 parent 7691861 commit 2369fd6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
21 changes: 21 additions & 0 deletions collections/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,24 @@ const personWithMinAge = minBy(people, (i) => i.age);

assertEquals(personWithMinAge, { name: "John", age: 23 });
```

### dropLastWhile

Returns a new array that drops all elements in the given collection until the
last element that does not match the given predicate

Example:

```ts
import { dropLastWhile } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";

const numbers = [22, 30, 44];

const notFourtyFour = dropLastWhile(numbers, (i) => i != 44);

assertEquals(
notFourtyFour,
[22, 30],
);
```
29 changes: 29 additions & 0 deletions collections/drop_last_while.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Returns a new array that drops all elements in the given collection until the
* last element that does not match the given predicate
*
* Example:
* ```ts
* import { dropLastWhile } from "./drop_last_while.ts";
* import { assertEquals } from "../testing/asserts.ts";
*
* const numbers = [22, 30, 44];
*
* const notFourtyFour = dropLastWhile(numbers, i => i != 44);
*
* assertEquals(
* notFourtyFour,
* [22, 30]
* );
* ```
*
*/
export function dropLastWhile<T>(
array: readonly T[],
predicate: (el: T) => boolean,
): T[] {
let offset = array.length;
while (0 < offset && predicate(array[offset - 1])) offset--;

return array.slice(0, offset);
}
51 changes: 51 additions & 0 deletions collections/drop_last_while_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { dropLastWhile } from "./drop_last_while.ts";
import { assertEquals } from "../testing/asserts.ts";

Deno.test("[collections/dropLastWhile] Num array", () => {
const values = [20, 33, 44];

const actual = dropLastWhile(values, (i) => i > 30);

assertEquals(actual, [20]);
});

Deno.test("[collections/dropLastWhile] No mutation", () => {
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("[collections/dropLastWhile] Negatives", () => {
const array = [-1, -2, -3, -4, -5, -6];

const actual = dropLastWhile(array, (i) => i < -4);

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

Deno.test("[collections/dropLastWhile] Empty input returns empty array", () => {
const array: number[] = [];

const actual = dropLastWhile(array, (i) => i > 4);

assertEquals(actual, []);
});

Deno.test("[collections/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("[collections/dropLastWhile] Returns empty array when all elements get dropped", () => {
const array = [20, 30, 20];

const actual = dropLastWhile(array, (i) => i < 40);

assertEquals(actual, []);
});
1 change: 1 addition & 0 deletions collections/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ export * from "./zip.ts";
export * from "./take_last_while.ts";
export * from "./take_while.ts";
export * from "./first_not_nullish_of.ts";
export * from "./drop_last_while.ts";

0 comments on commit 2369fd6

Please sign in to comment.