1
0
Fork 0
peertube/shared/core-utils/common/object.ts
Chocobozzz 0628157fe9
Move uuid stuff in extra utils
Since it requires an external dependency
2021-12-29 14:44:58 +01:00

35 lines
739 B
TypeScript

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
}
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))
}
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,
getKeys,
sortObjectComparator
}