Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. export const objectFilter = ( obj, predicate ) => {
  2. if ( obj === null || obj === undefined )
  3. throw Error( 'Cannot convert null or undefined to an object' )
  4.  
  5. if ( typeof predicate !== 'function' )
  6. throw Error( 'Expected predicate to be a function' )
  7.  
  8. obj = Object( obj )
  9.  
  10. return Object.keys( obj ).reduce(
  11. ( filtered, key ) => {
  12. if(
  13. Object.prototype.hasOwnProperty.call( obj, key ) &&
  14. predicate( obj[ key ], key, obj )
  15. ){
  16. filtered[ key ] = obj[ key ]
  17. }
  18.  
  19. return filtered
  20. },
  21. {}
  22. )
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement