Skip to content

Commit

Permalink
feat(collections): add pick and omit (#4218)
Browse files Browse the repository at this point in the history
Co-authored-by: Kenta Moriuchi <moriken@kimamass.com>
  • Loading branch information
lambdalisue and petamoriken authored Feb 25, 2024
1 parent 9882fbf commit 987fc72
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions collections/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ export * from "./drop_last_while.ts";
export * from "./reduce_groups.ts";
export * from "./sample.ts";
export * from "./running_reduce.ts";
export * from "./pick.ts";
export * from "./omit.ts";
26 changes: 26 additions & 0 deletions collections/omit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/**
* Creates a new object by excluding the specified keys from the provided object.
*
* @example
* ```ts
* import { omit } from "https://deno.land/std@$STD_VERSION/collections/omit.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts";
*
* const obj = { a: 5, b: 6, c: 7, d: 8 };
* const omitted = omit(obj, ["a", "c"]);
*
* assertEquals(omitted, { b: 6, d: 8 });
* ```
*/
export function omit<T extends object, K extends keyof T>(
obj: Readonly<T>,
keys: readonly K[],
): Omit<T, K> {
const excludes = new Set(keys);
const has = excludes.has.bind(excludes);
return Object.fromEntries(
Object.entries(obj).filter(([k, _]) => !has(k as K)),
) as Omit<T, K>;
}
38 changes: 38 additions & 0 deletions collections/omit_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals, assertNotStrictEquals } from "../assert/mod.ts";
import { omit } from "./omit.ts";

Deno.test({
name: "omit() returns a new object from the provided object",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const omitted = omit(obj, []);

assertEquals(omitted, { a: 5, b: 6, c: 7, d: 8 });
assertNotStrictEquals(omitted, obj);
},
});

Deno.test({
name:
"omit() returns a new object from the provided object without the provided keys",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const omitted = omit(obj, ["a", "c"]);

assertEquals(omitted, { b: 6, d: 8 });
assertNotStrictEquals(omitted, obj);
},
});

Deno.test({
name: "omit() returns an empty object when the provided keys is empty",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const omitted = omit(obj, ["a", "b", "c", "d"]);

assertEquals(omitted, {});
assertNotStrictEquals(omitted, obj);
},
});
22 changes: 22 additions & 0 deletions collections/pick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/**
* Creates a new object by including the specified keys from the provided object.
*
* @example
* ```ts
* import { pick } from "https://deno.land/std@$STD_VERSION/collections/pick.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts";
*
* const obj = { a: 5, b: 6, c: 7, d: 8 };
* const picked = pick(obj, ["a", "c"]);
*
* assertEquals(picked, { a: 5, c: 7 });
* ```
*/
export function pick<T extends object, K extends keyof T>(
obj: Readonly<T>,
keys: readonly K[],
): Pick<T, K> {
return Object.fromEntries(keys.map((k) => [k, obj[k]])) as Pick<T, K>;
}
39 changes: 39 additions & 0 deletions collections/pick_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals, assertNotStrictEquals } from "../assert/mod.ts";
import { pick } from "./pick.ts";

Deno.test({
name: "pick() returns a new empty object when no keys are provided",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const picked = pick(obj, []);

assertEquals(picked, {});
assertNotStrictEquals(picked, obj);
},
});

Deno.test({
name:
"pick() returns a new object from the provided object with the provided keys",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const picked = pick(obj, ["a", "c"]);

assertEquals(picked, { a: 5, c: 7 });
assertNotStrictEquals(picked, obj);
},
});

Deno.test({
name:
"pick() returns a new object from the provided object with the provided keys (all keys are provided)",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const picked = pick(obj, ["a", "b", "c", "d"]);

assertEquals(picked, { a: 5, b: 6, c: 7, d: 8 });
assertNotStrictEquals(picked, obj);
},
});

0 comments on commit 987fc72

Please sign in to comment.