defaults
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
clone | 1.0.4 | 4.35 kB | MIT | prod |
The "defaults" npm package empowers JavaScript developers to handle defaults for their options efficiently. This package is distinct and practical because it doesn't overwrite options if they haven't been defined within the options
object. Plus, it supports deep merging of objects while providing strong defense against prototype pollution attacks.
To use the defaults
package in your JavaScript application, follow these step-by-step instructions. Firstly, install the defaults
package using npm with this command:
npm install defaults
Then, import defaults
into your JavaScript file. You can apply defaults
to your options objects. Here's an example:
import defaults from 'defaults';
const calculate = options => {
options = defaults(options, {
timeout: {
before: 100,
after: 100
}
});
// Use options as per your requirements
// β¦
}
// β¦
calculate({timeout: {before: 200}});
In this example, the calculate
function accepts an options object. If no options are provided, or if certain options are missing, the defaults
function will ensure that the options return with defined timeout
before
and after
parameters, hence providing reliable default options.
The documentation for the defaults
npm package can be found on the package's GitHub page. Here, you can learn more about its API, installation, usage, and additional features. Importantly, the defaults
function accepts two arguments, options
and defaultOptions
. The options
object contains user-provided options, while the defaultOptions
parameter hosts the default options to be returned when a value isn't provided in the options
object.