Skip to content

Commit

Permalink
style: format with prettier v3
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 24, 2023
1 parent 63d0e8e commit 32650f1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 46 deletions.
47 changes: 26 additions & 21 deletions README.md
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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'] }
```

Expand All @@ -154,6 +158,7 @@ type Options = Defu<{ foo: 'bar' }, [{}, { bar: 'baz' }, { something: 42 }]>
MIT. Made with 💖

<!-- Refs -->

[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
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
4 changes: 1 addition & 3 deletions renovate.json
@@ -1,5 +1,3 @@
{
"extends": [
"github>unjs/renovate-config"
]
"extends": ["github>unjs/renovate-config"]
}
9 changes: 3 additions & 6 deletions src/defu.ts
Expand Up @@ -9,7 +9,7 @@ function _defu<T>(
baseObject: T,
defaults: any,
namespace = ".",
merger?: Merger
merger?: Merger,
): T {
if (!isObject(defaults)) {
return _defu(baseObject, {}, namespace, merger);
Expand Down Expand Up @@ -39,7 +39,7 @@ function _defu<T>(
value,
object[key],
(namespace ? `${namespace}.` : "") + key.toString(),
merger
merger,
);
} else {
object[key] = value;
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Expand Up @@ -11,14 +11,14 @@ export type Merger = <T extends Input, K extends keyof T>(
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<Destination, keyof Destination & keyof Defaults> &
Expand All @@ -35,7 +35,7 @@ export type MergeObjects<

export type Defu<
S extends Input,
D extends Array<Input | IgnoredInput>
D extends Array<Input | IgnoredInput>,
> = D extends [infer F, ...infer Rest]
? F extends Input
? Rest extends Array<Input | IgnoredInput>
Expand All @@ -50,7 +50,7 @@ export type Defu<

export type DefuFn = <
Source extends Input,
Defaults extends Array<Input | IgnoredInput>
Defaults extends Array<Input | IgnoredInput>,
>(
source: Source,
...defaults: Defaults
Expand Down
16 changes: 8 additions & 8 deletions test/defu.test.ts
Expand Up @@ -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, {});
Expand Down Expand Up @@ -119,7 +119,7 @@ describe("defu", () => {
baz: number[];
}
expectTypeOf(
defu({} as SomeConfig, {} as SomeOtherConfig, {} as ThirdConfig)
defu({} as SomeConfig, {} as SomeOtherConfig, {} as ThirdConfig),
).toEqualTypeOf<ExpectedMergedType>();

Check failure on line 123 in test/defu.test.ts

View workflow job for this annotation

GitHub Actions / ci

Expected 1 arguments, but got 0.
});

Expand All @@ -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<ExpectedMergedType>();

Check failure on line 141 in test/defu.test.ts

View workflow job for this annotation

GitHub Actions / ci

Expected 1 arguments, but got 0.

expectTypeOf(
defu({ foo: ["test"] }, {}, { bar: ["test2"] }, {})
defu({ foo: ["test"] }, {}, { bar: ["test2"] }, {}),
).toEqualTypeOf<ExpectedMergedType>();

Check failure on line 145 in test/defu.test.ts

View workflow job for this annotation

GitHub Actions / ci

Expected 1 arguments, but got 0.
});

Expand All @@ -167,8 +167,8 @@ describe("defu", () => {
{
ignore: ["node_modules", "dist"],
num: 10,
}
)
},
),
).toEqual({
ignore: ["node_modules"],
num: 20,
Expand All @@ -187,8 +187,8 @@ describe("defu", () => {
{
arr: ["a", "b"],
num: 10,
}
)
},
),
).toEqual({
arr: ["c"],
num,
Expand Down
5 changes: 1 addition & 4 deletions tsconfig.json
Expand Up @@ -6,8 +6,5 @@
"skipLibCheck": true,
"declaration": true
},
"include": [
"src",
"test"
]
"include": ["src", "test"]
}

0 comments on commit 32650f1

Please sign in to comment.