-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(collections): Returns an array excluding single given value inst…
…ead of multi values
- Loading branch information
Showing
3 changed files
with
16 additions
and
38 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 |
---|---|---|
@@ -1,33 +1,21 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** Used as the size to enable large array optimizations. */ | ||
const LARGE_ARRAY_SIZE = 200; | ||
|
||
/** | ||
* Returns an array excluding all given values. | ||
* Returns an array excluding single given value. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* import { without } from "./without.ts"; | ||
* | ||
* const withoutList = without([2, 1, 2, 3], 1, 2); | ||
* const withoutList = without([1,2,3], 1); | ||
* | ||
* console.assert(withoutList === [ 3 ]) | ||
* console.assert(withoutList.length === 2) | ||
* ``` | ||
*/ | ||
export function without<T>( | ||
array: Array<T>, | ||
...values: Array<T> | ||
value: T, | ||
): Array<T> { | ||
if (array.length >= LARGE_ARRAY_SIZE) { | ||
const set = new Set<T>(); | ||
|
||
for (const element of array) { | ||
set.add(element); | ||
} | ||
|
||
array = Array.from(set); | ||
} | ||
return array.filter((value) => values.indexOf(value) === -1); | ||
return array.filter((element) => value !== element); | ||
} |
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