Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collections): add dropLastWhile #1193

Merged
merged 4 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions collections/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,24 @@ const personWithMinAge = minBy(people, (i) => i.age);

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

### dropLastWhile

Returns all elements in the given collection until the last element that does
grian32 marked this conversation as resolved.
Show resolved Hide resolved
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 values = [22, 30, 44];
grian32 marked this conversation as resolved.
Show resolved Hide resolved

const notFourtyFour = dropLastWhile(values, (i) => i != 44);
grian32 marked this conversation as resolved.
Show resolved Hide resolved

assertEquals(
notFourtyFour,
grian32 marked this conversation as resolved.
Show resolved Hide resolved
[22, 30],
);
```
28 changes: 28 additions & 0 deletions collections/drop_last_while.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Returns 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 values = [22, 30, 44];
*
* const notFourtyFour = dropLastWhile(values, i => i != 44);
*
* assertEquals(
* notFourtyFour,
* [22, 30]
* );
* ```
grian32 marked this conversation as resolved.
Show resolved Hide resolved
*
*/
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 @@ -30,3 +30,4 @@ export * from "./unzip.ts";
export * from "./zip.ts";
export * from "./take_while.ts";
export * from "./first_not_nullish_of.ts";
export * from "./drop_last_while.ts";