Created
October 3, 2019 01:00
-
-
Save nrkn/105987b94fd068e2378cc6aa95006623 to your computer and use it in GitHub Desktop.
object filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const objectFilter = ( obj, predicate ) => { | |
if ( obj === null || obj === undefined ) | |
throw Error( 'Cannot convert null or undefined to an object' ) | |
if ( typeof predicate !== 'function' ) | |
throw Error( 'Expected predicate to be a function' ) | |
obj = Object( obj ) | |
return Object.keys( obj ).reduce( | |
( filtered, key ) => { | |
if( | |
Object.prototype.hasOwnProperty.call( obj, key ) && | |
predicate( obj[ key ], key, obj ) | |
){ | |
filtered[ key ] = obj[ key ] | |
} | |
return filtered | |
}, | |
{} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment