-
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 withoutAll (#1100)
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com> Co-authored-by: Leon Strauss <me@lionc.de> Co-authored-by: Zhangyuan Nie <yuan@znie.org>
- Loading branch information
1 parent
d7cbbf3
commit 6c407de
Showing
2 changed files
with
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* Returns an array excluding all given values. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* import { withoutAll } from "./without_all.ts"; | ||
* import { assertEquals } from "../testing/asserts.ts"; | ||
* | ||
* const withoutList = withoutAll([2, 1, 2, 3], [1, 2]); | ||
* | ||
* assertEquals(withoutList, [3]); | ||
* ``` | ||
*/ | ||
export function withoutAll<T>(array: readonly T[], values: readonly T[]): T[] { | ||
const toExclude = new Set(values); | ||
return array.filter((it) => !toExclude.has(it)); | ||
} |
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,60 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "../testing/asserts.ts"; | ||
import { withoutAll } from "./without_all.ts"; | ||
|
||
function withoutAllTest<I>( | ||
input: Array<I>, | ||
excluded: Array<I>, | ||
expected: Array<I>, | ||
message?: string, | ||
) { | ||
const actual = withoutAll(input, excluded); | ||
assertEquals(actual, expected, message); | ||
} | ||
|
||
Deno.test({ | ||
name: "[collections/withoutAll] no mutation", | ||
fn() { | ||
const array = [1, 2, 3, 4]; | ||
withoutAll(array, [2, 3]); | ||
assertEquals(array, [1, 2, 3, 4]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/withoutAll] empty input", | ||
fn() { | ||
withoutAllTest([], [], []); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/withoutAll] no matches", | ||
fn() { | ||
withoutAllTest([1, 2, 3, 4], [0, 7, 9], [1, 2, 3, 4]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/withoutAll] single matche", | ||
fn() { | ||
withoutAllTest([1, 2, 3, 4], [1], [2, 3, 4]); | ||
withoutAllTest([1, 2, 3, 2], [2], [1, 3]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/withoutAll] multiple matches", | ||
fn() { | ||
withoutAllTest([1, 2, 3, 4, 6, 3], [1, 2], [3, 4, 6, 3]); | ||
withoutAllTest([7, 2, 9, 8, 7, 6, 5, 7], [7, 9], [2, 8, 6, 5]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collection/withoutAll] leaves duplicate elements", | ||
fn() { | ||
withoutAllTest(new Array(110).fill(3), [1], new Array(110).fill(3)); | ||
}, | ||
}); |