update
This commit is contained in:
+126
@@ -0,0 +1,126 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||
|
||||
### [4.0.1](https://github.com/unjs/defu/compare/v4.0.0...v4.0.1) (2021-04-23)
|
||||
|
||||
## [4.0.0](https://github.com/unjs/defu/compare/v3.2.2...v4.0.0) (2021-04-23)
|
||||
|
||||
|
||||
### ⚠ BREAKING CHANGES
|
||||
|
||||
* module exports
|
||||
|
||||
### Features
|
||||
|
||||
* module exports ([42df406](https://github.com/unjs/defu/commit/42df406b6938bc7ffb5f628efaa9447c4e95b211))
|
||||
|
||||
### [3.2.2](https://github.com/unjs/defu/compare/v3.2.1...v3.2.2) (2020-11-10)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* switch back to bili for es5 support till fixing in siroc ([07786c2](https://github.com/unjs/defu/commit/07786c270f9bf49529d8203f31b4b9bd696a6ee3))
|
||||
|
||||
### [3.2.1](https://github.com/unjs/defu/compare/v3.2.0...v3.2.1) (2020-11-09)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **types:** correct type inference where merged types are same ([#26](https://github.com/unjs/defu/issues/26)) ([f322607](https://github.com/unjs/defu/commit/f322607515a5bd19b1a2f28d7e3336fee9194520))
|
||||
|
||||
## [3.2.0](https://github.com/unjs/defu/compare/v3.1.0...v3.2.0) (2020-11-09)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add type inference for defu result ([#24](https://github.com/unjs/defu/issues/24)) ([934d736](https://github.com/unjs/defu/commit/934d736da31f4824d9e135d925cece361b73c533))
|
||||
* pass namespace to custom merger ([#25](https://github.com/unjs/defu/issues/25)) ([6bd7ef5](https://github.com/unjs/defu/commit/6bd7ef59ce3e94b68108355942dba9ac49284523))
|
||||
|
||||
## [3.1.0](https://github.com/unjs/defu/compare/v3.0.1...v3.1.0) (2020-08-04)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add defu.arrayFn ([#21](https://github.com/unjs/defu/issues/21)) ([df05ed0](https://github.com/unjs/defu/commit/df05ed04088d6e0f0bc1a8cd9603fae46fb59268))
|
||||
|
||||
### [3.0.1](https://github.com/unjs/defu/compare/v3.0.0...v3.0.1) (2020-07-29)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* recursively pass merger ([ec09394](https://github.com/unjs/defu/commit/ec09394d77533cd0a4753a943a5d6fbd25ef308d))
|
||||
|
||||
## [3.0.0](https://github.com/unjs/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/unjs/defu/issues/19)) ([4932232](https://github.com/unjs/defu/commit/493223278840132a6de2c3291b60f7b00b3fa477))
|
||||
* merge arrays ([#18](https://github.com/unjs/defu/issues/18)) ([22c631e](https://github.com/unjs/defu/commit/22c631e354d9bc50380ce7beb8914bd44feb2309))
|
||||
|
||||
### [2.0.4](https://github.com/unjs/defu/compare/v2.0.3...v2.0.4) (2020-05-22)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* correct path to types ([33d4bf0](https://github.com/unjs/defu/commit/33d4bf0331e70b69a3a2a392f18a8f890d45d4f9))
|
||||
|
||||
### [2.0.3](https://github.com/unjs/defu/compare/v2.0.2...v2.0.3) (2020-05-22)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* specify type declaration file more precisely ([#15](https://github.com/unjs/defu/issues/15)) ([6aa47d4](https://github.com/unjs/defu/commit/6aa47d4a06a117b34b5e9231b04f8403056c2685))
|
||||
|
||||
### [2.0.2](https://github.com/unjs/defu/compare/v2.0.1...v2.0.2) (2020-04-19)
|
||||
|
||||
### [2.0.1](https://github.com/unjs/defu/compare/v2.0.0...v2.0.1) (2020-04-19)
|
||||
|
||||
### Docs
|
||||
|
||||
* Add note about `null`
|
||||
|
||||
## [2.0.0](https://github.com/unjs/defu/compare/v1.0.0...v2.0.0) (2020-04-19)
|
||||
|
||||
### Features
|
||||
|
||||
* Support passing multiple defaults ([89ef702](https://github.com/unjs/defu/commit/89ef702736b49cd48ca99a0dc64aa6ef3bd74e2d))
|
||||
* Typescript rewrite ([9c906e6](https://github.com/unjs/defu/commit/9c906e64459da64d77124224edb66034ce92f20c))
|
||||
|
||||
<a name="1.0.0"></a>
|
||||
# [1.0.0](https://github.com/unjs/defu/compare/v0.0.4...v1.0.0) (2020-02-02)
|
||||
|
||||
|
||||
|
||||
<a name="0.0.4"></a>
|
||||
## [0.0.4](https://github.com/unjs/defu/compare/v0.0.3...v0.0.4) (2020-01-01)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* improve es5 compatibility ([#2](https://github.com/unjs/defu/issues/2), [#9](https://github.com/unjs/defu/issues/9)) ([5a6de7c](https://github.com/unjs/defu/commit/5a6de7c))
|
||||
|
||||
|
||||
|
||||
<a name="0.0.3"></a>
|
||||
## [0.0.3](https://github.com/unjs/defu/compare/v0.0.1...v0.0.3) (2019-05-25)
|
||||
|
||||
|
||||
|
||||
<a name="0.0.2"></a>
|
||||
## [0.0.2](https://github.com/jesless/defu/compare/v0.0.1...v0.0.2) (2019-05-25)
|
||||
|
||||
|
||||
|
||||
<a name="0.0.1"></a>
|
||||
## 0.0.1 (2019-02-07)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* imrpove non-object handlers ([f89fa28](https://github.com/jesless/defu/commit/f89fa28))
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 - Pooya Parsa <pooya@pi0.ir>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+155
@@ -0,0 +1,155 @@
|
||||

|
||||
|
||||
# 🌊 defu
|
||||
|
||||
> Assign default properties, recursively. Lightweight and Fast!
|
||||
|
||||
[![Standard JS][standard-src]][standard-href]
|
||||
[![david dm][david-src]][david-href]
|
||||
[![codecov][codecov-src]][codecov-href]
|
||||
|
||||
[![npm version][npm-v-src]][npm-v-href]
|
||||
[![npm downloads][npm-dm-src]][npm-dm-href]
|
||||
[![package phobia][packagephobia-src]][packagephobia-href]
|
||||
[![bundle phobia][bundlephobia-src]][bundlephobia-href]
|
||||
|
||||
## Install
|
||||
|
||||
Install package:
|
||||
|
||||
```bash
|
||||
yarn add defu
|
||||
# or
|
||||
npm install defu
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const options = defu (object, ...defaults)
|
||||
```
|
||||
|
||||
Leftmost arguments have more priority when assigning defaults.
|
||||
|
||||
### Arguments
|
||||
|
||||
- **object (Object):** The destination object.
|
||||
- **source (Object):** The source object.
|
||||
|
||||
```js
|
||||
const defu = require('defu')
|
||||
|
||||
console.log(defu({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }))
|
||||
// => { a: { b: 2, c: 3 } }
|
||||
```
|
||||
|
||||
## 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` (source object), `key` and `value` (current value) and should return `true` if applied custom merging.
|
||||
|
||||
**Example:** Sum numbers instead of overriding
|
||||
|
||||
```js
|
||||
const ext = defu.extend((obj, key, value) => {
|
||||
if (typeof obj[key] === 'number' && typeof value === '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.
|
||||
|
||||
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(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'
|
||||
],
|
||||
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 preserve.
|
||||
- Assignment of `__proto__` and `constructor` keys will be skipped to prevent security issues with object pollution.
|
||||
- Will concat `array` values (if default property is defined)
|
||||
```js
|
||||
console.log(defu({ array: ['b', 'c'] }, { array: ['a'] }))
|
||||
// => { array: ['a', 'b', 'c']}
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT. Made with 💖
|
||||
|
||||
<!-- Refs -->
|
||||
[standard-src]: https://flat.badgen.net/badge/code%20style/standard/green
|
||||
[standard-href]: https://standardjs.com
|
||||
|
||||
[npm-v-src]: https://flat.badgen.net/npm/v/defu/latest
|
||||
[npm-v-href]: https://npmjs.com/package/defu
|
||||
|
||||
[npm-dm-src]: https://flat.badgen.net/npm/dm/defu
|
||||
[npm-dm-href]: https://npmjs.com/package/defu
|
||||
|
||||
[packagephobia-src]: https://flat.badgen.net/packagephobia/install/defu
|
||||
[packagephobia-href]: https://packagephobia.now.sh/result?p=defu
|
||||
|
||||
[bundlephobia-src]: https://flat.badgen.net/bundlephobia/min/defu
|
||||
[bundlephobia-href]: https://bundlephobia.com/result?p=defu
|
||||
|
||||
[david-src]: https://flat.badgen.net/david/dep/unjs/defu
|
||||
[david-href]: https://david-dm.org/unjs/defu
|
||||
|
||||
[codecov-src]: https://flat.badgen.net/codecov/c/github/unjs/defu/master
|
||||
[codecov-href]: https://codecov.io/gh/unjs/defu
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
declare type Input = Record<string | number | symbol, any>;
|
||||
declare type Merger = <T extends Input, K extends keyof T>(obj: T, key: keyof T, value: T[K], namespace: string) => any;
|
||||
declare type MergeObjects<Destination extends Input, Defaults extends Input> = Destination extends Defaults ? Destination : Omit<Destination, keyof Destination & keyof Defaults> & Omit<Defaults, keyof Destination & keyof Defaults> & {
|
||||
-readonly [Key in keyof Destination & keyof Defaults]: Destination[Key] extends null ? Defaults[Key] extends null ? null : Defaults[Key] : Defaults[Key] extends null ? Destination[Key] : Merge<Destination[Key], Defaults[Key]>;
|
||||
};
|
||||
declare type DefuFn = <Source extends Input, Defaults extends Input>(source: Source, ...defaults: Defaults[]) => MergeObjects<Source, Defaults>;
|
||||
interface Defu {
|
||||
<Source extends Input, Defaults extends Input>(source: Source, ...defaults: Defaults[]): MergeObjects<Source, Defaults>;
|
||||
fn: DefuFn;
|
||||
arrayFn: DefuFn;
|
||||
extend(merger?: Merger): DefuFn;
|
||||
}
|
||||
declare type MergeArrays<Destination, Source> = Destination extends Array<infer DestinationType> ? Source extends Array<infer SourceType> ? Array<DestinationType | SourceType> : Source | Array<DestinationType> : Source | Destination;
|
||||
declare type Merge<Destination extends Input, Defaults extends Input> = Destination extends null ? Defaults extends null ? null : Defaults : Defaults extends null ? Destination : Destination extends Array<any> ? Defaults extends Array<any> ? MergeArrays<Destination, Defaults> : Destination | Defaults : Destination extends Function ? Destination | Defaults : Destination extends RegExp ? Destination | Defaults : Destination extends Promise<any> ? Destination | Defaults : Defaults extends Function ? Destination | Defaults : Defaults extends RegExp ? Destination | Defaults : Defaults extends Promise<any> ? Destination | Defaults : Destination extends Input ? Defaults extends Input ? MergeObjects<Destination, Defaults> : Destination | Defaults : Destination | Defaults;
|
||||
|
||||
declare const defu: Defu;
|
||||
|
||||
export default defu;
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
function isObject(val) {
|
||||
return val !== null && typeof val === "object";
|
||||
}
|
||||
function _defu(baseObj, defaults, namespace = ".", merger) {
|
||||
if (!isObject(defaults)) {
|
||||
return _defu(baseObj, {}, namespace, merger);
|
||||
}
|
||||
const obj = Object.assign({}, defaults);
|
||||
for (const key in baseObj) {
|
||||
if (key === "__proto__" || key === "constructor") {
|
||||
continue;
|
||||
}
|
||||
const val = baseObj[key];
|
||||
if (val === null) {
|
||||
continue;
|
||||
}
|
||||
if (merger && merger(obj, key, val, namespace)) {
|
||||
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], (namespace ? `${namespace}.` : "") + key.toString(), merger);
|
||||
} else {
|
||||
obj[key] = val;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
function extend(merger) {
|
||||
return (...args) => args.reduce((p, c) => _defu(p, c, "", merger), {});
|
||||
}
|
||||
const defu = extend();
|
||||
defu.fn = extend((obj, key, currentValue, _namespace) => {
|
||||
if (typeof obj[key] !== "undefined" && typeof currentValue === "function") {
|
||||
obj[key] = currentValue(obj[key]);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
defu.arrayFn = extend((obj, key, currentValue, _namespace) => {
|
||||
if (Array.isArray(obj[key]) && typeof currentValue === "function") {
|
||||
obj[key] = currentValue(obj[key]);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
defu.extend = extend;
|
||||
|
||||
module.exports = defu;
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
function isObject(val) {
|
||||
return val !== null && typeof val === "object";
|
||||
}
|
||||
function _defu(baseObj, defaults, namespace = ".", merger) {
|
||||
if (!isObject(defaults)) {
|
||||
return _defu(baseObj, {}, namespace, merger);
|
||||
}
|
||||
const obj = Object.assign({}, defaults);
|
||||
for (const key in baseObj) {
|
||||
if (key === "__proto__" || key === "constructor") {
|
||||
continue;
|
||||
}
|
||||
const val = baseObj[key];
|
||||
if (val === null) {
|
||||
continue;
|
||||
}
|
||||
if (merger && merger(obj, key, val, namespace)) {
|
||||
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], (namespace ? `${namespace}.` : "") + key.toString(), merger);
|
||||
} else {
|
||||
obj[key] = val;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
function extend(merger) {
|
||||
return (...args) => args.reduce((p, c) => _defu(p, c, "", merger), {});
|
||||
}
|
||||
const defu = extend();
|
||||
defu.fn = extend((obj, key, currentValue, _namespace) => {
|
||||
if (typeof obj[key] !== "undefined" && typeof currentValue === "function") {
|
||||
obj[key] = currentValue(obj[key]);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
defu.arrayFn = extend((obj, key, currentValue, _namespace) => {
|
||||
if (Array.isArray(obj[key]) && typeof currentValue === "function") {
|
||||
obj[key] = currentValue(obj[key]);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
defu.extend = extend;
|
||||
|
||||
export default defu;
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"defu@4.0.1",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "defu@4.0.1",
|
||||
"_id": "defu@4.0.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-lC+G0KvvWRbisQa50+iFelm3/eMmwo4IlBmfASOVlw9MZpHHyQeVsZxc5j23+TQy5ydgEoTVSrWl7ptou1kzJQ==",
|
||||
"_location": "/@nuxt/config/defu",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "defu@4.0.1",
|
||||
"name": "defu",
|
||||
"escapedName": "defu",
|
||||
"rawSpec": "4.0.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "4.0.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@nuxt/config"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/defu/-/defu-4.0.1.tgz",
|
||||
"_spec": "4.0.1",
|
||||
"_where": "/home/node/nuxt",
|
||||
"bugs": {
|
||||
"url": "https://github.com/unjs/defu/issues"
|
||||
},
|
||||
"description": "Recursively assign default properties. Lightweight and Fast!",
|
||||
"devDependencies": {
|
||||
"@nuxtjs/eslint-config-typescript": "latest",
|
||||
"@types/jest": "latest",
|
||||
"@types/node": "latest",
|
||||
"eslint": "latest",
|
||||
"expect-type": "latest",
|
||||
"jest": "latest",
|
||||
"siroc": "latest",
|
||||
"standard-version": "latest",
|
||||
"ts-jest": "latest",
|
||||
"typescript": "latest"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./dist/defu.js",
|
||||
"import": "./dist/defu.mjs"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"homepage": "https://github.com/unjs/defu#readme",
|
||||
"license": "MIT",
|
||||
"main": "./dist/defu.js",
|
||||
"module": "./dist/defu.mjs",
|
||||
"name": "defu",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/unjs/defu.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "siroc build",
|
||||
"lint": "eslint --ext .ts src",
|
||||
"prepack": "yarn build",
|
||||
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
|
||||
"test": "yarn lint && yarn jest"
|
||||
},
|
||||
"types": "./dist/defu.d.ts",
|
||||
"version": "4.0.1"
|
||||
}
|
||||
Reference in New Issue
Block a user