diff options
author | Chocobozzz <me@florianbigard.com> | 2021-12-17 11:58:15 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-12-17 12:24:03 +0100 |
commit | c55e3d7227fe1453869e309025996b9d75256d5d (patch) | |
tree | 08e9b0ca210d75c82c8606fef0852eca020e8e0e /shared/core-utils/common/object.ts | |
parent | bf54587a3e2ad9c2c186828f2a5682b91ee2cc00 (diff) | |
download | PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.tar.gz PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.tar.zst PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.zip |
Move test functions outside extra-utils
Diffstat (limited to 'shared/core-utils/common/object.ts')
-rw-r--r-- | shared/core-utils/common/object.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/shared/core-utils/common/object.ts b/shared/core-utils/common/object.ts new file mode 100644 index 000000000..88d6b7514 --- /dev/null +++ b/shared/core-utils/common/object.ts | |||
@@ -0,0 +1,30 @@ | |||
1 | function 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 | |||
13 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { | ||
14 | return (a: any, b: any) => { | ||
15 | if (a[key] < b[key]) { | ||
16 | return order === 'asc' ? -1 : 1 | ||
17 | } | ||
18 | |||
19 | if (a[key] > b[key]) { | ||
20 | return order === 'asc' ? 1 : -1 | ||
21 | } | ||
22 | |||
23 | return 0 | ||
24 | } | ||
25 | } | ||
26 | |||
27 | export { | ||
28 | pick, | ||
29 | sortObjectComparator | ||
30 | } | ||