2.0.2 • Published 8 years ago
set-options v2.0.2
set-options
Merges default options to the user-given options.
Install
$ npm install set-options --saveset(options, defaults)
Always returns an object. For a key k, if options does not have k as an own property, and defaults does, defaults[k] will copied to options.
var set = require('set-options')
var defaults = {
a: 1
}
function factory (options) {
var config = set(options, defaults)
// `options` and `defaults` will not be ruined after `set()`
console.log(config, config === options, config === defaults)
}
factory()
// {a: 1} false false
factory(undefined)
// {a: 1} false false
factory(null)
// {a: 1} false false
factory(1)
// {a: 1} false false
factory({})
// {a: 1} false false
factory({b: 1})
// {a: 1, b: 1} false false
factory({a: 0})
// {a: 0} false false- options
Object=can be undefined. - defaults
Objectnot definingdefaultsis silly, since that's the whole purpose of this lib.
Define whether should override properties
set(options, defaults, filter)let options = set({a: undefined}, {a: 1}, function (value, key, object) {
return key in object
})
options // {a: undefined}Deep merge? Nope
Do something below instead.
options = set(options, defaults)
options.config = set(options.config, default_config)License
MIT