mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
d102a10235
* refactor: import from `@std/assert` * update
35 lines
990 B
TypeScript
35 lines
990 B
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
/**
|
|
* Creates a new object by excluding the specified keys from the provided object.
|
|
*
|
|
* @typeParam T The type of the object.
|
|
* @typeParam K The type of the keys to omit.
|
|
*
|
|
* @param obj The object to omit keys from.
|
|
* @param keys The keys to omit from the object.
|
|
*
|
|
* @returns A new object with the specified keys omitted.
|
|
*
|
|
* @example Basic usage
|
|
* ```ts
|
|
* import { omit } from "@std/collections/omit";
|
|
* import { assertEquals } from "@std/assert";
|
|
*
|
|
* 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);
|
|
return Object.fromEntries(
|
|
Object.entries(obj).filter(([k, _]) => !excludes.has(k as K)),
|
|
) as Omit<T, K>;
|
|
}
|