A simple function for providing defaults to an options to argument. No dependencies, tiny amount of code.
default-args makes input validation simpler, by always ensuring you will have well defined object shape you can work with.
It also prevents the inclusion of extraneous properties by only copying properties that are defined in the default options object. This ensures that the final object only contains the properties that are specified. This not only prevents unexpected behavior but also potential security vulnerabilities caused by the inclusion of extraneous properties.
By using this package you also eliminate a class of vulnerability called object prototype pollution by correctly handling options.
import defaults from 'default-args';
function helloWorld(options) {
options = defaults({
foo: true,
bar: {
a: 1,
b: 2,
},
}, options);
// do something with options
}
npm install default-args
yarn add default-args
<script type="text/javascript" src="https://unpkg.com/default-args/"></script>
File | Module Type | Transpiled | Source Maps |
---|---|---|---|
dist/default-args.esm.mjs | esm | No | No |
dist/default-args.cjs.js | cjs | Yes | Yes |
dist/default-args.esm.js | esm | Yes | Yes |
dist/default-args.js | umd | Yes | Yes |
The code is available under the MIT license.
We are open to contributions, see CONTRIBUTING.md for more info.