Socket
Socket
Sign inDemoInstall

defu

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

defu - npm Package Compare versions

Comparing version 3.0.1 to 3.1.0

7

CHANGELOG.md

@@ -5,2 +5,9 @@ # Changelog

## [3.1.0](https://github.com/nuxt-contrib/defu/compare/v3.0.1...v3.1.0) (2020-08-04)
### Features
* add defu.arrayFn ([#21](https://github.com/nuxt-contrib/defu/issues/21)) ([df05ed0](https://github.com/nuxt-contrib/defu/commit/df05ed04088d6e0f0bc1a8cd9603fae46fb59268))
### [3.0.1](https://github.com/nuxt-contrib/defu/compare/v3.0.0...v3.0.1) (2020-07-29)

@@ -7,0 +14,0 @@

1

dist/defu.d.ts

@@ -6,2 +6,3 @@ declare type Merger = (obj: any, key: string, value: any) => any;

fn: DefuFn;
arrayFn: DefuFn;
extend(merger?: Merger): DefuFn;

@@ -8,0 +9,0 @@ }

13

dist/defu.js

@@ -58,7 +58,14 @@ 'use strict';

defu.fn = extend(function (obj, key, value) {
if (typeof value === 'function') {
obj[key] = value(obj[key]);
defu.fn = extend(function (obj, key, currentValue) {
if (typeof obj[key] !== 'undefined' && typeof currentValue === 'function') {
obj[key] = currentValue(obj[key]);
return true;
}
}); // Custom version with function merge support only for defined arrays
defu.arrayFn = extend(function (obj, key, currentValue) {
if (Array.isArray(obj[key]) && typeof currentValue === 'function') {
obj[key] = currentValue(obj[key]);
return true;
}
}); // Support user extending

@@ -65,0 +72,0 @@

{
"name": "defu",
"version": "3.0.1",
"version": "3.1.0",
"description": "Recursively assign default properties. Lightweight and Fast!",

@@ -5,0 +5,0 @@ "repository": "nuxt-contrib/defu",

@@ -8,6 +8,5 @@ # 🇩 defu

[![codecov][codecov-src]][codecov-href]
[![circleci][circleci-src]][circleci-href]
[![npm version][npm-v-src]][npm-v-href]
[![npm downloads][npm-dt-src]][npm-dt-href]
[![npm downloads][npm-dm-src]][npm-dm-href]
[![package phobia][packagephobia-src]][packagephobia-href]

@@ -32,3 +31,3 @@ [![bundle phobia][bundlephobia-src]][bundlephobia-href]

Most left arguments have more perioriry when assigning defaults.
Leftmost arguments have more priority when assigning defaults.

@@ -51,3 +50,3 @@ ### Arguments

This function accepts `obj` (target object), `key` and `value` (default value) and should return `true` if applied custom merging.
This function accepts `obj` (source object), `key` and `value` (current value) and should return `true` if applied custom merging.

@@ -57,4 +56,4 @@ **Example:** Sum numbers instead of overriding

```js
const ext = defu.extend((obj, key, val) => {
if (typeof val === 'number') {
const ext = defu.extend((obj, key, value) => {
if (typeof obj[key] === 'number' && typeof value === 'number') {
obj[key] += val

@@ -70,27 +69,64 @@ return true

Using `defu.fn`, if user provided a function, it will be called with default value instead of merging. Mostly useful for array merging.
Using `defu.fn`, if user provided a function, it will be called with default value instead of merging.
**Example:** Filter some items from defaults (array)
I can be useful for default values manipulation.
**Example:** Filter some items from defaults (array) and add 20 to the count default value.
```js
defu.fn({
ignore(val) => val.filter(i => i !== 'dist')
ignore: (val) => val.filter(item => item !== 'dist'),
count: (count) => count + 20
}, {
ignore: ['node_modules','dist],
count: 10
})
/*
{
ignore: ['node_modules'],
count: 30
}
*/
```
**Note:** if the default value is not defined, the function defined won't be called and kept as value.
## Array Function Merger
`defu.arrayFn` is similar to `defu.fn` but **only applies to array values defined in defaults**.
**Example:** Filter some items from defaults (array) and add 20 to the count default value.
```js
defu.arrayFn({
ignore(val) => val.filter(i => i !== 'dist'),
count: () => 20
}, {
ignore: [
'node_modules',
'dist
]
}) // { ignore: ['node_modules'] }
],
count: 10
})
/*
{
ignore: ['node_modules'],
count: () => 20
}
*/
```
**Note:** the function is called only if the value defined in defaults is an aray.
### Remarks
- `object` and `defaults` are not modified
- `null` values are skipped same as [defaults-deep](https://www.npmjs.com/package/defaults-deep). Please use either [omit-deep](http://npmjs.com/package/omit-deep) or [lodash.defaultsdeep](https://www.npmjs.com/package/lodash.defaultsdeep) if you need to to preserve.
- `null` values are skipped same as [defaults-deep](https://www.npmjs.com/package/defaults-deep). Please use either [omit-deep](http://npmjs.com/package/omit-deep) or [lodash.defaultsdeep](https://www.npmjs.com/package/lodash.defaultsdeep) if you need to preserve.
- Assignment of `__proto__` and `constructor` keys will be skipped to prevent security issues with object pollution.
- Will concat `array` values (it default property is defined)
- Will concat `array` values (if default property is defined)
```js
console.log(defu({ array: ['b', 'c'] }, { array: ['a'] }))
// => { array: [a', 'b', 'c']}
// => { array: ['a', 'b', 'c']}
```

@@ -109,4 +145,4 @@

[npm-dt-src]: https://flat.badgen.net/npm/dt/defu
[npm-dt-href]: https://npmjs.com/package/defu
[npm-dm-src]: https://flat.badgen.net/npm/dm/defu
[npm-dm-href]: https://npmjs.com/package/defu

@@ -124,4 +160,1 @@ [packagephobia-src]: https://flat.badgen.net/packagephobia/install/defu

[codecov-href]: https://codecov.io/gh/nuxt-contrib/defu
[circleci-src]: https://flat.badgen.net/circleci/github/nuxt-contrib/defu/master
[circleci-href]: https://circleci.com/gh/nuxt-contrib/defu
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc