default-options
v1.0.0
Published
Assigns default values to an object along with validating for required and unknown property keys.
Downloads
19
Maintainers
Readme
default-options
Assigns default values to an object along with validating for required and unknown property keys.
Usage
dopts(options, defaults, [doptsOptions]);
options
- The user specified options objectdefaults
- An object of defaults for values not specified inoptions
. If a default property value is set specifically toundefined
, it will be treated as a required property.doptsOptions
- Optional object of options for thedopts
function.doptsOptions.allowUnknown
If set to true, will not throw an error if an unknown property is specified. (default isfalse
)
- Returns
defaults
merged withoptions
Example:
var dopts = require('default-options');
dopts({foo: 1, bar: 1}, {
foo: undefined, // undefined means required
bar: 2, // optional
baz: null, // optional
});
// Returns {foo: 1, bar: 1, baz: null}
dopts({foo:1}, {
foo: undefined,
bar: undefined
});
// Throws an Error because `bar` is not defined
dopts({foo: 1, biz: 2}, {
foo: undefined
});
// Throws an Error because `biz` is an unknown property