Skip to content

Commit

Permalink
feat(collections): Returns an array excluding single given value inst…
Browse files Browse the repository at this point in the history
…ead of multi values
  • Loading branch information
getspooky committed Aug 3, 2021
1 parent 2fdee07 commit 2e285cb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
8 changes: 4 additions & 4 deletions collections/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,13 @@ console.assert(

## without

Returns an array excluding all given values.
Returns an array excluding single given value.

```ts
import { without } from "./without.ts";

const numbers = [];
const withoutList = without([2, 1, 2, 3], 1, 2);
const numbers = [1, 2, 3];
const withoutList = without(numbers, 1);

console.assert(withoutList === [3]);
console.assert(withoutList.length === 2);
```
22 changes: 5 additions & 17 deletions collections/without.ts
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);
}
24 changes: 7 additions & 17 deletions collections/without_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { without } from "./without.ts";

function withoutTest<I>(
input: Array<I>,
excluded: Array<I>,
excluded: I,
expected: Array<I>,
message?: string,
) {
const actual = without(input, ...excluded);
const actual = without(input, excluded);
assertEquals(actual, expected, message);
}

Expand All @@ -23,32 +23,22 @@ Deno.test({
Deno.test({
name: "[collections/without] no matches",
fn() {
withoutTest([1, 2, 3, 4], [], [1, 2, 3, 4]);
withoutTest([1, 2, 3, 4], 0, [1, 2, 3, 4]);
},
});

Deno.test({
name: "[collections/without] single matche",
fn() {
withoutTest([1, 2, 3, 4], [1], [2, 3, 4]);
withoutTest([1, 2, 3, 2], [2], [1, 3]);
withoutTest([1, 2, 3, 4], 1, [2, 3, 4]);
withoutTest([1, 2, 3, 2], 2, [1, 3]);
},
});

Deno.test({
name: "[collections/without] multiple matches",
fn() {
withoutTest([1, 2, 3, 4, 6], [1, 3, 6], [2, 4]);
withoutTest([2, 9, 8, 7, 6, 5], [7, 5], [2, 9, 8, 6]);
withoutTest([2, 9, 8, 7, 6, 5], [7, 5], [2, 9, 8, 6]);
},
});

Deno.test({
name: "[collections/without] large array size",
fn() {
const array = Array(200).fill(0);
array[0] = 1;
withoutTest(array, [0], [1]);
withoutTest([1, 2, 3, 4, 6, 3], 1, [2, 3, 4, 6, 3]);
withoutTest([7, 2, 9, 8, 7, 6, 5, 7], 7, [2, 9, 8, 6, 5]);
},
});

0 comments on commit 2e285cb

Please sign in to comment.