Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collections): Add pick and omit #4218

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
},
});
Loading