]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/core-utils/common/object.ts
Move uuid stuff in extra utils
[github/Chocobozzz/PeerTube.git] / shared / core-utils / common / object.ts
CommitLineData
c55e3d72
C
1function pick <O extends object, K extends keyof O> (object: O, keys: K[]): Pick<O, K> {
2 const result: any = {}
3
4 for (const key of keys) {
5 if (Object.prototype.hasOwnProperty.call(object, key)) {
6 result[key] = object[key]
7 }
8 }
9
10 return result
11}
12
0628157f
C
13function getKeys <O extends object, K extends keyof O> (object: O, keys: K[]): K[] {
14 return (Object.keys(object) as K[]).filter(k => keys.includes(k))
15}
16
c55e3d72
C
17function sortObjectComparator (key: string, order: 'asc' | 'desc') {
18 return (a: any, b: any) => {
19 if (a[key] < b[key]) {
20 return order === 'asc' ? -1 : 1
21 }
22
23 if (a[key] > b[key]) {
24 return order === 'asc' ? 1 : -1
25 }
26
27 return 0
28 }
29}
30
31export {
32 pick,
0628157f 33 getKeys,
c55e3d72
C
34 sortObjectComparator
35}