Skip to content

A simple function for providing default options to arguments. No dependencies, tiny amount of code.

License

Notifications You must be signed in to change notification settings

victornpb/default-args

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

default-args

Version CodeFactor Snyk Vulnerabilities for GitHub Repo bundlephobia

Downloads Node LICENSE

carbon-3

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.

Usage

import defaults from 'default-args';

function helloWorld(options) {
    options = defaults({
        foo: true,
        bar: {
            a: 1,
            b: 2,
        },
    }, options);

    // do something with options
}

Installation

npm install default-args
yarn add default-args

CDN

<script type="text/javascript" src="https://unpkg.com/default-args/"></script>

Packages

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

License

The code is available under the MIT license.

Contributing

We are open to contributions, see CONTRIBUTING.md for more info.