-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(collections): add dropLastWhile (#1193)
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, []); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters