Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

set-options

2.0.2 • Public • Published

Build Status

set-options

Merges default options to the user-given options.

Install

$ npm install set-options --save

set(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 Object not defining defaults is 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

install

npm i set-options

Downloadsweekly downloads

20

version

2.0.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability