2021-12-17 04:58:15 -06:00
|
|
|
function pick <O extends object, K extends keyof O> (object: O, keys: K[]): Pick<O, K> {
|
|
|
|
const result: any = {}
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
|
|
result[key] = object[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:25:58 -05:00
|
|
|
function omit <O extends object, K extends keyof O> (object: O, keys: K[]): Exclude<O, K> {
|
|
|
|
const result: any = {}
|
|
|
|
const keysSet = new Set(keys) as Set<string>
|
|
|
|
|
|
|
|
for (const [ key, value ] of Object.entries(object)) {
|
|
|
|
if (keysSet.has(key)) continue
|
|
|
|
|
|
|
|
result[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:44:58 -06:00
|
|
|
function getKeys <O extends object, K extends keyof O> (object: O, keys: K[]): K[] {
|
|
|
|
return (Object.keys(object) as K[]).filter(k => keys.includes(k))
|
|
|
|
}
|
|
|
|
|
2021-12-17 04:58:15 -06:00
|
|
|
function sortObjectComparator (key: string, order: 'asc' | 'desc') {
|
|
|
|
return (a: any, b: any) => {
|
|
|
|
if (a[key] < b[key]) {
|
|
|
|
return order === 'asc' ? -1 : 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a[key] > b[key]) {
|
|
|
|
return order === 'asc' ? 1 : -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
pick,
|
2022-08-17 08:25:58 -05:00
|
|
|
omit,
|
2021-12-29 07:44:58 -06:00
|
|
|
getKeys,
|
2021-12-17 04:58:15 -06:00
|
|
|
sortObjectComparator
|
|
|
|
}
|