Skip to content

Instantly share code, notes, and snippets.

@alexkhismatulin
Created May 13, 2019 16:27
Show Gist options
  • Save alexkhismatulin/ad1993713ffd49f8f33e1af2e41acfe6 to your computer and use it in GitHub Desktop.
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
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