-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathobject.ts
More file actions
24 lines (23 loc) · 820 Bytes
/
object.ts
File metadata and controls
24 lines (23 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Returns a new object with the given keys removed.
*
* @example
* omit({ a: 1, b: 2, c: 3 }, ['b', 'c']) // { a: 1 }
*/
export function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
const result = { ...obj }
for (const key of keys) {
delete result[key]
}
return result
}
/**
* Returns a shallow copy of `obj` with all `undefined`-valued keys removed.
* Useful for building query-param or request-body objects where undefined
* fields should not be serialized.
*
* Replaces the common `Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined))` pattern.
*/
export function filterUndefined<T extends Record<string, unknown>>(obj: T): Partial<T> {
return Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined)) as Partial<T>
}