merge-options
Merge Option Objects
merge-options
considers plain objects as Option Objects, everything else as Option Values.
Install
$ npm install --save merge-options
Usage
const mergeOptions = ; //=> {foo: 0, bar: 3, baz: 2} //=> {nested: {unicorns: 'many'}} //=> {Symbol(key): 42}
Usage with custom config
const mergeOptions = ; //=> {foo: 'bar'}
API
mergeOptions.call(config, option1, ...options)
mergeOptions.apply(config, [option1, ...options])
mergeOptions(option1, ...options)mergeOptions
recursively merges one or more Option Objects into a new one and returns that. The options
are merged in order, thus Option Values of additional options
take precedence over previous ones.
The merging does not alter the passed option
arguments, taking roughly the following steps:
- recursively cloning[1] Option Objects and arrays until reaching Option Values
- copying[1] references to Option Values to the result object
const defaultOpts = false // functions are Option Values promise: Promise // all non-plain objects are Option Values array: 'foo' // arrays are Option Values nested: unicorns: 'none' // {…} is plain, therefore an Option Object; const opts = true // [1] promise: Promise // [2] array: 'baz' // [3] nested: unicorns: 'many' // [4]; //=> fn: Function // === [1] promise: Promise 'bar' // === [2] array: 'baz' // !== [3] (arrays are cloned) nested: unicorns: 'many' // !== [4] (Option Objects are cloned)
config
Type: object
config.concatArrays
Type: boolean
Default: false
Concatenate arrays:
//=> {src: ['test/**']} // Via callmergeOptions//=> {src: ['src/**', 'test/**']} // Via applymergeOptions//=> {src: ['src/**', 'test/**']}
config.ignoreUndefined
Type: boolean
Default: false
Ignore undefined values:
//=> {foo: undefined} // Via callmergeOptions//=> {foo: 'bar'} // Via applymergeOptions//=> {foo: 'bar'}
Related
- See object-assign if you need a ES2015 Object.assign() ponyfill
- See deep-assign if you need to do Object.assign() recursively
Notes
- copying and cloning take only enumerable own properties into account
License
MIT © Michael Mayer