update
This commit is contained in:
+94
-21
@@ -1,14 +1,15 @@
|
||||
# 🇩 defu
|
||||

|
||||
|
||||
> Recursively assign default properties. Lightweight and Fast!
|
||||
# 🌊 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]
|
||||
[![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]
|
||||
[![bundle phobia][bundlephobia-src]][bundlephobia-href]
|
||||
|
||||
@@ -16,14 +17,10 @@
|
||||
|
||||
Install package:
|
||||
|
||||
```bash
|
||||
npm install defu
|
||||
```
|
||||
|
||||
OR
|
||||
|
||||
```bash
|
||||
yarn add defu
|
||||
# or
|
||||
npm install defu
|
||||
```
|
||||
|
||||
## Usage
|
||||
@@ -32,7 +29,7 @@ yarn add defu
|
||||
const options = defu (object, ...defaults)
|
||||
```
|
||||
|
||||
Most left arguments have more perioriry when assigning defaults.
|
||||
Leftmost arguments have more priority when assigning defaults.
|
||||
|
||||
### Arguments
|
||||
|
||||
@@ -46,11 +43,90 @@ 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 to preserve.
|
||||
- Nullish values ([`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null) and [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)) are skipped. Please use [defaults-deep](https://www.npmjs.com/package/defaults-deep) or [omit-deep](http://npmjs.com/package/omit-deep) or [lodash.defaultsdeep](https://www.npmjs.com/package/lodash.defaultsdeep) if you need to preserve or different behavior.
|
||||
- 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
|
||||
|
||||
@@ -63,8 +139,8 @@ MIT. Made with 💖
|
||||
[npm-v-src]: https://flat.badgen.net/npm/v/defu/latest
|
||||
[npm-v-href]: https://npmjs.com/package/defu
|
||||
|
||||
[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
|
||||
|
||||
[packagephobia-src]: https://flat.badgen.net/packagephobia/install/defu
|
||||
[packagephobia-href]: https://packagephobia.now.sh/result?p=defu
|
||||
@@ -72,11 +148,8 @@ MIT. Made with 💖
|
||||
[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/jsless/defu
|
||||
[david-href]: https://david-dm.org/jsless/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/jsless/defu/master
|
||||
[codecov-href]: https://codecov.io/gh/jsless/defu
|
||||
|
||||
[circleci-src]: https://flat.badgen.net/circleci/github/jsless/defu/master
|
||||
[circleci-href]: https://circleci.com/gh/jsless/defu
|
||||
[codecov-src]: https://flat.badgen.net/codecov/c/github/unjs/defu/master
|
||||
[codecov-href]: https://codecov.io/gh/unjs/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 || val === void 0) {
|
||||
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;
|
||||
+19
-5
@@ -1,5 +1,19 @@
|
||||
declare type defuObj = {
|
||||
[key: string]: defuObj | any;
|
||||
};
|
||||
declare function defu<T extends defuObj>(...args: T | any): T;
|
||||
export default defu;
|
||||
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 nullish = null | undefined | void;
|
||||
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 nullish ? Defaults[Key] extends nullish ? nullish : Defaults[Key] : Defaults[Key] extends nullish ? 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 nullish ? Defaults extends nullish ? nullish : Defaults : Defaults extends nullish ? 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 { defu as default };
|
||||
|
||||
+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 || val === void 0) {
|
||||
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 { defu as default };
|
||||
+30
-24
@@ -1,70 +1,76 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"defu@2.0.4",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"defu@5.0.1",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "defu@2.0.4",
|
||||
"_id": "defu@2.0.4",
|
||||
"_from": "defu@5.0.1",
|
||||
"_id": "defu@5.0.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-G9pEH1UUMxShy6syWk01VQSRVs3CDWtlxtZu7A+NyqjxaCA4gSlWAKDBx6QiUEKezqS8+DUlXLI14Fp05Hmpwg==",
|
||||
"_integrity": "sha512-EPS1carKg+dkEVy3qNTqIdp2qV7mUP08nIsupfwQpz++slCVRw7qbQyWvSTig+kFPwz2XXp5/kIIkH+CwrJKkQ==",
|
||||
"_location": "/defu",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "defu@2.0.4",
|
||||
"raw": "defu@5.0.1",
|
||||
"name": "defu",
|
||||
"escapedName": "defu",
|
||||
"rawSpec": "2.0.4",
|
||||
"rawSpec": "5.0.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "2.0.4"
|
||||
"fetchSpec": "5.0.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@nuxt/config",
|
||||
"/@nuxt/loading-screen",
|
||||
"/@nuxt/telemetry",
|
||||
"/@nuxtjs/axios",
|
||||
"/rc9"
|
||||
"/serve-placeholder"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/defu/-/defu-2.0.4.tgz",
|
||||
"_spec": "2.0.4",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/defu/-/defu-5.0.1.tgz",
|
||||
"_spec": "5.0.1",
|
||||
"_where": "/home/node/nuxt",
|
||||
"bugs": {
|
||||
"url": "https://github.com/nuxt-contrib/defu/issues"
|
||||
"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",
|
||||
"bili": "latest",
|
||||
"eslint": "latest",
|
||||
"expect-type": "latest",
|
||||
"jest": "latest",
|
||||
"rollup-plugin-typescript2": "latest",
|
||||
"standard-version": "latest",
|
||||
"ts-jest": "latest",
|
||||
"typescript": "latest"
|
||||
"typescript": "latest",
|
||||
"unbuild": "latest"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./dist/defu.cjs",
|
||||
"import": "./dist/defu.mjs"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"homepage": "https://github.com/nuxt-contrib/defu#readme",
|
||||
"homepage": "https://github.com/unjs/defu#readme",
|
||||
"license": "MIT",
|
||||
"main": "./dist/defu.js",
|
||||
"main": "./dist/defu.cjs",
|
||||
"module": "./dist/defu.mjs",
|
||||
"name": "defu",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/nuxt-contrib/defu.git"
|
||||
"url": "git+https://github.com/unjs/defu.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "bili src/defu.ts",
|
||||
"build": "unbuild",
|
||||
"lint": "eslint --ext .ts src",
|
||||
"prepublish": "yarn build",
|
||||
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
|
||||
"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": "2.0.4"
|
||||
"version": "5.0.1"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user