forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/expect-custom-equality-case
- Loading branch information
Showing
17 changed files
with
185 additions
and
22 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
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
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,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>; | ||
} |
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,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); | ||
}, | ||
}); |
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,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>; | ||
} |
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,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); | ||
}, | ||
}); |
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
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
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
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
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