Created
May 13, 2019 16:27
-
-
Save alexkhismatulin/ad1993713ffd49f8f33e1af2e41acfe6 to your computer and use it in GitHub Desktop.
A pair of tools allowing to pick a set of properties from object or get an object without passed list of properties
This file contains 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
const match = (fields, key) => { | |
if (["string", "number"].indexOf(typeof fields) !== -1) return fields === key | |
if (fields.indexOf) return fields.indexOf(key) !== -1 | |
return false | |
} | |
const omit = (obj, fields) => Object.keys(obj).reduce((accum, propName) => { | |
if (match(fields, propName)) return accum | |
return { ...accum, [propName]: obj[propName] } | |
}, {}) | |
const pick = (obj, fields) => Object.keys(obj).reduce((accum, propName) => { | |
if (match(fields, propName)) return { ...accum, [propName]: obj[propName] } | |
return accum | |
}, {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment