diff --git a/README.md b/README.md index ca3a08d..28f88f0 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,9 @@ pnpm install defu ## Usage ```js -import { defu } from 'defu' +import { defu } from "defu"; -const options = defu(object, ...defaults) +const options = defu(object, ...defaults); ``` Leftmost arguments have more priority when assigning defaults. @@ -37,16 +37,16 @@ Leftmost arguments have more priority when assigning defaults. - **source (Object):** The source object. ```js -import { defu } from 'defu' +import { defu } from "defu"; -console.log(defu({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } })) +console.log(defu({ a: { b: 2 } }, { a: { b: 1, c: 3 } })); // => { a: { b: 2, c: 3 } } ``` ### Using with CommonJS ```js -const { defu } = require('defu') +const { defu } = require("defu"); ``` ## Custom Merger @@ -58,16 +58,16 @@ This function accepts `obj` (source object), `key` and `value` (current value) a **Example:** Sum numbers instead of overriding ```js -import { createDefu } from 'defu' +import { createDefu } from "defu"; const ext = createDefu((obj, key, value) => { - if (typeof obj[key] === 'number' && typeof value === 'number') { - obj[key] += value - return true + if (typeof obj[key] === "number" && typeof value === "number") { + obj[key] += value; + return true; } -}) +}); -ext({ cost: 15 }, { cost: 10 }) // { cost: 25 } +ext({ cost: 15 }, { cost: 10 }); // { cost: 25 } ``` ## Function Merger @@ -79,16 +79,19 @@ It can be useful for default values manipulation. **Example:** Filter some items from defaults (array) and add 20 to the count default value. ```js -import { defuFn } from 'defu' +import { defuFn } from "defu"; -defuFn({ - ignore: (val) => val.filter(item => item !== 'dist'), - count: (count) => count + 20 - }, { - ignore: ['node_modules','dist'], - count: 10 - }) - /* +defuFn( + { + ignore: (val) => val.filter((item) => item !== "dist"), + count: (count) => count + 20, + }, + { + ignore: ["node_modules", "dist"], + count: 10, + }, +); +/* { ignore: ['node_modules'], count: 30 @@ -133,8 +136,9 @@ defuArrayFn({ - 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'] })) +console.log(defu({ array: ["b", "c"] }, { array: ["a"] })); // => { array: ['b', 'c', 'a'] } ``` @@ -154,6 +158,7 @@ type Options = Defu<{ foo: 'bar' }, [{}, { bar: 'baz' }, { something: 42 }]> MIT. Made with 💖 + [npm-version-src]: https://img.shields.io/npm/v/defu?style=flat&colorA=18181B&colorB=F0DB4F [npm-version-href]: https://npmjs.com/package/defu [npm-downloads-src]: https://img.shields.io/npm/dm/defu?style=flat&colorA=18181B&colorB=F0DB4F diff --git a/package.json b/package.json index 03ff2db..1c15788 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "build": "unbuild", "dev": "vitest", "lint": "eslint --ext .ts src && prettier -c src test", + "lint:fix": "eslint --ext .ts src --fix && prettier -w src test", "prepack": "pnpm build", "release": "pnpm test && changelogen --release && git push --follow-tags && pnpm publish", "test": "pnpm lint && pnpm vitest run", diff --git a/renovate.json b/renovate.json index a9971c8..57fe916 100644 --- a/renovate.json +++ b/renovate.json @@ -1,5 +1,3 @@ { - "extends": [ - "github>unjs/renovate-config" - ] + "extends": ["github>unjs/renovate-config"] } diff --git a/src/defu.ts b/src/defu.ts index 61c8264..af9ed88 100644 --- a/src/defu.ts +++ b/src/defu.ts @@ -9,7 +9,7 @@ function _defu( baseObject: T, defaults: any, namespace = ".", - merger?: Merger + merger?: Merger, ): T { if (!isObject(defaults)) { return _defu(baseObject, {}, namespace, merger); @@ -39,7 +39,7 @@ function _defu( value, object[key], (namespace ? `${namespace}.` : "") + key.toString(), - merger + merger, ); } else { object[key] = value; @@ -62,10 +62,7 @@ export default defu; // Custom version with function merge support export const defuFn = createDefu((object, key, currentValue) => { - if ( - typeof object[key] !== "undefined" && - typeof currentValue === "function" - ) { + if (object[key] !== undefined && typeof currentValue === "function") { object[key] = currentValue(object[key]); return true; } diff --git a/src/types.ts b/src/types.ts index cfa7cc2..b17b22a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,14 +11,14 @@ export type Merger = ( object: T, key: keyof T, value: T[K], - namespace: string + namespace: string, ) => any; type nullish = null | undefined | void; export type MergeObjects< Destination extends Input, - Defaults extends Input + Defaults extends Input, > = Destination extends Defaults ? Destination : Omit & @@ -35,7 +35,7 @@ export type MergeObjects< export type Defu< S extends Input, - D extends Array + D extends Array, > = D extends [infer F, ...infer Rest] ? F extends Input ? Rest extends Array @@ -50,7 +50,7 @@ export type Defu< export type DefuFn = < Source extends Input, - Defaults extends Array + Defaults extends Array, >( source: Source, ...defaults: Defaults diff --git a/test/defu.test.ts b/test/defu.test.ts index de980ba..0deddb2 100644 --- a/test/defu.test.ts +++ b/test/defu.test.ts @@ -87,7 +87,7 @@ describe("defu", () => { it("should not override Object prototype", () => { const payload = JSON.parse( - '{"constructor": {"prototype": {"isAdmin": true}}}' + '{"constructor": {"prototype": {"isAdmin": true}}}', ); defu({}, payload); defu(payload, {}); @@ -119,7 +119,7 @@ describe("defu", () => { baz: number[]; } expectTypeOf( - defu({} as SomeConfig, {} as SomeOtherConfig, {} as ThirdConfig) + defu({} as SomeConfig, {} as SomeOtherConfig, {} as ThirdConfig), ).toEqualTypeOf(); }); @@ -137,11 +137,11 @@ describe("defu", () => { let options: (SomeConfig & SomeOtherConfig) | undefined; expectTypeOf( - defu(options ?? {}, { foo: ["test"] }, { bar: ["test2"] }, {}) + defu(options ?? {}, { foo: ["test"] }, { bar: ["test2"] }, {}), ).toEqualTypeOf(); expectTypeOf( - defu({ foo: ["test"] }, {}, { bar: ["test2"] }, {}) + defu({ foo: ["test"] }, {}, { bar: ["test2"] }, {}), ).toEqualTypeOf(); }); @@ -167,8 +167,8 @@ describe("defu", () => { { ignore: ["node_modules", "dist"], num: 10, - } - ) + }, + ), ).toEqual({ ignore: ["node_modules"], num: 20, @@ -187,8 +187,8 @@ describe("defu", () => { { arr: ["a", "b"], num: 10, - } - ) + }, + ), ).toEqual({ arr: ["c"], num, diff --git a/tsconfig.json b/tsconfig.json index f86f466..e8d5553 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,8 +6,5 @@ "skipLibCheck": true, "declaration": true }, - "include": [ - "src", - "test" - ] + "include": ["src", "test"] }