mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 19:04:46 +00:00
refactor: change separate tools by category
This commit is contained in:
38
core/stdlib/src/objects/omit/index.ts
Normal file
38
core/stdlib/src/objects/omit/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { isArray, type Arrayable } from '../../types';
|
||||
|
||||
/**
|
||||
* @name omit
|
||||
* @category Objects
|
||||
* @description Returns a new object with the specified keys omitted
|
||||
*
|
||||
* @param {object} target - The object to omit keys from
|
||||
* @param {Arrayable<keyof Target>} keys - The keys to omit
|
||||
* @returns {Omit<Target, Key>} The new object with the specified keys omitted
|
||||
*
|
||||
* @example
|
||||
* omit({ a: 1, b: 2, c: 3 }, 'a') // => { b: 2, c: 3 }
|
||||
*
|
||||
* @example
|
||||
* omit({ a: 1, b: 2, c: 3 }, ['a', 'b']) // => { c: 3 }
|
||||
*
|
||||
* @since 0.0.3
|
||||
*/
|
||||
export function omit<Target extends object, Key extends keyof Target>(
|
||||
target: Target,
|
||||
keys: Arrayable<Key>
|
||||
): Omit<Target, Key> {
|
||||
const result = { ...target };
|
||||
|
||||
if (!target || !keys)
|
||||
return result;
|
||||
|
||||
if (isArray(keys)) {
|
||||
for (const key of keys) {
|
||||
delete result[key];
|
||||
}
|
||||
} else {
|
||||
delete result[keys];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user