Returns a copy of the object based on which object properties are included or excluded. If the key has a - in front, it is marked excluded, otherwise it is marked as included. Also can be specified which properties will be included by default.
Install with npm:
$ npm i object.pickomit
const pickOmit = require('object.pickomit');
To pick or omit, pass the keys
as an array:
pickOmit.select({ a: 'a', b: 'b', c: 'c', d: 'd' }, ['a', 'b'])
// pick => { a: 'a', b: 'b' }
pickOmit.select({ a: 'a', b: 'b', c: 'c', d: 'd' }, ['-a', '-b'])
// omit => { c: 'c', d: 'd' }
keys
can also be pass as string.
pickOmit.select({ a: 'a', b: 'b', c: 'c', d: 'd' }, 'a b')
// pick => { a: 'a', b: 'b' }
pickOmit.select({ a: 'a', b: 'b', c: 'c', d: 'd' }, '-a -b')
// omit => { c: 'c', d: 'd' }
Throws an error if pick and omit keys are passed together.
pickOmit.select({ a: 'a', b: 'b', c: 'c', d: 'd' }, 'a -b')
// Error: Keys cannot have a mix of inclusion and exclusion.
It sets the keys to be picked by default even if they are not passed as parameters. Keys can be passed as string or arrays as above.
const pickOmit = require('object.pickomit').setDefaultPicks('a b');
pickOmit.select({ a: 'a', b: 'b', c: 'c', d: 'd' }, 'd')
// => { a: 'a', b: 'b', d: 'd' }
If the default pick keys are not wanted to be included, they are passed with a - in front of the key.
pickOmit.select({ a: 'a', b: 'b', c: 'c', d: 'd' }, '-a d')
// => { b: 'b', d: 'd' }
Furkan Işıtan
Copyright © 2020, Furkan Işıtan Released under the MIT License.