]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/core-utils/common/object.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / shared / core-utils / common / object.ts
index 88d6b7514d8c04684ab5c708f1052f2ce917467e..2330c94038da1f436878f012bdc2f5e377921809 100644 (file)
@@ -10,6 +10,23 @@ function pick <O extends object, K extends keyof O> (object: O, keys: K[]): Pick
   return result
 }
 
+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
+}
+
+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]) {
@@ -26,5 +43,7 @@ function sortObjectComparator (key: string, order: 'asc' | 'desc') {
 
 export {
   pick,
+  omit,
+  getKeys,
   sortObjectComparator
 }