add pick + exclude object util

This commit is contained in:
axolotle 2023-08-28 19:12:46 +02:00
parent 8d6b1aa791
commit e78c97f926

18
utils/common.ts Normal file
View file

@ -0,0 +1,18 @@
export function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> {
const filtered = {} as Pick<T, K>
keys.forEach((key) => {
filtered[key] = obj[key]
})
return filtered
}
export function exclude<T, K extends keyof T>(
obj: T,
...keys: K[]
): Omit<T, K> {
const filtered = { ...obj }
keys.forEach((key) => {
delete filtered[key]
})
return filtered
}