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 2.0.4 to 3.0.0

20

CHANGELOG.md

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

## [3.0.0](https://github.com/nuxt-contrib/defu/compare/v2.0.4...v3.0.0) (2020-07-28)
### ⚠ BREAKING CHANGES
* defau will merge arrays too (#18)
### Features
* extend and custom merger ([#19](https://github.com/nuxt-contrib/defu/issues/19)) ([4932232](https://github.com/nuxt-contrib/defu/commit/493223278840132a6de2c3291b60f7b00b3fa477))
* merge arrays ([#18](https://github.com/nuxt-contrib/defu/issues/18)) ([22c631e](https://github.com/nuxt-contrib/defu/commit/22c631e354d9bc50380ce7beb8914bd44feb2309))
### [2.0.4](https://github.com/nuxt-contrib/defu/compare/v2.0.3...v2.0.4) (2020-05-22)

@@ -36,3 +48,3 @@

<a name="1.0.0"></a>
# [1.0.0](https://github.com/jsless/defu/compare/v0.0.4...v1.0.0) (2020-02-02)
# [1.0.0](https://github.com/nuxt-contrib/defu/compare/v0.0.4...v1.0.0) (2020-02-02)

@@ -42,3 +54,3 @@

<a name="0.0.4"></a>
## [0.0.4](https://github.com/jsless/defu/compare/v0.0.3...v0.0.4) (2020-01-01)
## [0.0.4](https://github.com/nuxt-contrib/defu/compare/v0.0.3...v0.0.4) (2020-01-01)

@@ -48,3 +60,3 @@

* improve es5 compatibility ([#2](https://github.com/jsless/defu/issues/2), [#9](https://github.com/jsless/defu/issues/9)) ([5a6de7c](https://github.com/jsless/defu/commit/5a6de7c))
* improve es5 compatibility ([#2](https://github.com/nuxt-contrib/defu/issues/2), [#9](https://github.com/nuxt-contrib/defu/issues/9)) ([5a6de7c](https://github.com/nuxt-contrib/defu/commit/5a6de7c))

@@ -54,3 +66,3 @@

<a name="0.0.3"></a>
## [0.0.3](https://github.com/jsless/defu/compare/v0.0.1...v0.0.3) (2019-05-25)
## [0.0.3](https://github.com/nuxt-contrib/defu/compare/v0.0.1...v0.0.3) (2019-05-25)

@@ -57,0 +69,0 @@

12

dist/defu.d.ts

@@ -1,5 +0,9 @@

declare type defuObj = {
[key: string]: defuObj | any;
};
declare function defu<T extends defuObj>(...args: T | any): T;
declare type Merger = (obj: any, key: string, value: any) => any;
declare type DefuFn = <T>(...args: T | any) => T;
interface Defu {
<T>(...args: T | any): T;
fn: DefuFn;
extend(merger?: Merger): DefuFn;
}
declare const defu: Defu;
export default defu;
'use strict';
function _typeof(obj) {
"@babel/helpers - typeof";
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function isObject(val) {
return val !== null && _typeof(val) === 'object' && !Array.isArray(val);
}
return val !== null && typeof val === 'object';
} // Base function to apply defaults
function _defu(baseObj, defaults) {
if (!isObject(baseObj)) {
return _defu({}, defaults);
}
function _defu(baseObj, defaults, merger) {
if (!isObject(defaults)) {

@@ -45,3 +26,9 @@ return _defu(baseObj, {});

if (isObject(val) && isObject(obj[key])) {
if (merger && merger(obj, key, val)) {
continue;
}
if (Array.isArray(val) && Array.isArray(obj[key])) {
obj[key] = obj[key].concat(val);
} else if (isObject(val) && isObject(obj[key])) {
obj[key] = _defu(val, obj[key]);

@@ -54,12 +41,29 @@ } else {

return obj;
}
} // Create defu wrapper with optional merger and multi arg support
function defu() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
function extend(merger) {
return function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return args.reduce(function (p, c) {
return _defu(p, c, merger);
}, {});
};
} // Basic version
var defu = extend(); // Custom version with function merge support
defu.fn = extend(function (obj, key, value) {
if (typeof value === 'function') {
obj[key] = value(obj[key]);
return true;
}
}); // Support user extending
return args.reduce(_defu, {});
}
defu.extend = extend;
module.exports = defu;
{
"name": "defu",
"version": "2.0.4",
"version": "3.0.0",
"description": "Recursively assign default properties. Lightweight and Fast!",

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

@@ -20,11 +20,7 @@ # 🇩 defu

```bash
yarn add defu
# or
npm install defu
```
OR
```bash
yarn add defu
```
## Usage

@@ -50,2 +46,39 @@

## Custom Merger
Sometimes default merging strategy is not desirable. Using `defu.extend` we can create a custom instance with different merging strategy.
This function accepts `obj` (target object), `key` and `value` (default value) and should return `true` if applied custom merging.
**Example:** Sum numbers instead of overriding
```js
const ext = defu.extend((obj, key, val) => {
if (typeof val === 'number') {
obj[key] += val
return true
}
})
ext({ cost: 15 }, { cost: 10 }) // { cost: 25 }
```
## Function Merger
Using `defu.fn`, if user provided a function, it will be called with default value instead of merging. Mostly useful for array merging.
**Example:** Filter some items from defaults (array)
```js
defu.fn({
ignore(val) => val.filter(i => i !== 'dist')
}, {
ignore: [
'node_modules',
'dist
]
}) // { ignore: ['node_modules'] }
```
### Remarks

@@ -56,2 +89,7 @@

- 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)
```js
console.log(defu({ array: ['b', 'c'] }, { array: ['a'] }))
// => { array: [a', 'b', 'c']}
```

@@ -78,9 +116,9 @@ ## License

[david-src]: https://flat.badgen.net/david/dep/jsless/defu
[david-href]: https://david-dm.org/jsless/defu
[david-src]: https://flat.badgen.net/david/dep/nuxt-contrib/defu
[david-href]: https://david-dm.org/nuxt-contrib/defu
[codecov-src]: https://flat.badgen.net/codecov/c/github/jsless/defu/master
[codecov-href]: https://codecov.io/gh/jsless/defu
[codecov-src]: https://flat.badgen.net/codecov/c/github/nuxt-contrib/defu/master
[codecov-href]: https://codecov.io/gh/nuxt-contrib/defu
[circleci-src]: https://flat.badgen.net/circleci/github/jsless/defu/master
[circleci-href]: https://circleci.com/gh/jsless/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