SHARE
TWEET

Untitled

a guest Apr 19th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const pickFunc = <T extends {}, K extends keyof T>(
  2.     obj: T,
  3.     predicate: (k: string) => boolean
  4. ): Pick<T, K> =>
  5.     Object
  6.         .keys(obj)
  7.         .filter(predicate)
  8.         .reduce((filteredObj: Pick<T, K>, key) => ({
  9.             ...filteredObj,
  10.             [key]: obj[key as keyof T]
  11.         }), {} as Pick<T, K>)
  12.  
  13.  
  14.  
  15. // filters an object based on an array of keys
  16. const pick = <T extends {}, K extends keyof T>(obj: T, keys: Array<K>): Pick<T, K> =>
  17.     pickFunc(
  18.         obj,
  19.         k => keys.includes(k as K)
  20.     )
  21.  
  22.  
  23. const omit = <T extends {}, K extends keyof T>(obj: T, keys: Array<K>): Pick<T, K> =>
  24.     pickFunc(
  25.         obj,
  26.         k => !keys.includes(k as K)
  27.     )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top