Skip to content

Commit 32650f1

Browse files
committed
style: format with prettier v3
1 parent 63d0e8e commit 32650f1

File tree

7 files changed

+44
-46
lines changed

7 files changed

+44
-46
lines changed

README.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ pnpm install defu
2424
## Usage
2525

2626
```js
27-
import { defu } from 'defu'
27+
import { defu } from "defu";
2828

29-
const options = defu(object, ...defaults)
29+
const options = defu(object, ...defaults);
3030
```
3131

3232
Leftmost arguments have more priority when assigning defaults.
@@ -37,16 +37,16 @@ Leftmost arguments have more priority when assigning defaults.
3737
- **source (Object):** The source object.
3838

3939
```js
40-
import { defu } from 'defu'
40+
import { defu } from "defu";
4141

42-
console.log(defu({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }))
42+
console.log(defu({ a: { b: 2 } }, { a: { b: 1, c: 3 } }));
4343
// => { a: { b: 2, c: 3 } }
4444
```
4545

4646
### Using with CommonJS
4747

4848
```js
49-
const { defu } = require('defu')
49+
const { defu } = require("defu");
5050
```
5151

5252
## Custom Merger
@@ -58,16 +58,16 @@ This function accepts `obj` (source object), `key` and `value` (current value) a
5858
**Example:** Sum numbers instead of overriding
5959

6060
```js
61-
import { createDefu } from 'defu'
61+
import { createDefu } from "defu";
6262

6363
const ext = createDefu((obj, key, value) => {
64-
if (typeof obj[key] === 'number' && typeof value === 'number') {
65-
obj[key] += value
66-
return true
64+
if (typeof obj[key] === "number" && typeof value === "number") {
65+
obj[key] += value;
66+
return true;
6767
}
68-
})
68+
});
6969

70-
ext({ cost: 15 }, { cost: 10 }) // { cost: 25 }
70+
ext({ cost: 15 }, { cost: 10 }); // { cost: 25 }
7171
```
7272

7373
## Function Merger
@@ -79,16 +79,19 @@ It can be useful for default values manipulation.
7979
**Example:** Filter some items from defaults (array) and add 20 to the count default value.
8080

8181
```js
82-
import { defuFn } from 'defu'
82+
import { defuFn } from "defu";
8383

84-
defuFn({
85-
ignore: (val) => val.filter(item => item !== 'dist'),
86-
count: (count) => count + 20
87-
}, {
88-
ignore: ['node_modules','dist'],
89-
count: 10
90-
})
91-
/*
84+
defuFn(
85+
{
86+
ignore: (val) => val.filter((item) => item !== "dist"),
87+
count: (count) => count + 20,
88+
},
89+
{
90+
ignore: ["node_modules", "dist"],
91+
count: 10,
92+
},
93+
);
94+
/*
9295
{
9396
ignore: ['node_modules'],
9497
count: 30
@@ -133,8 +136,9 @@ defuArrayFn({
133136
- 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.
134137
- Assignment of `__proto__` and `constructor` keys will be skipped to prevent security issues with object pollution.
135138
- Will concat `array` values (if default property is defined)
139+
136140
```js
137-
console.log(defu({ array: ['b', 'c'] }, { array: ['a'] }))
141+
console.log(defu({ array: ["b", "c"] }, { array: ["a"] }));
138142
// => { array: ['b', 'c', 'a'] }
139143
```
140144

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

156160
<!-- Refs -->
161+
157162
[npm-version-src]: https://img.shields.io/npm/v/defu?style=flat&colorA=18181B&colorB=F0DB4F
158163
[npm-version-href]: https://npmjs.com/package/defu
159164
[npm-downloads-src]: https://img.shields.io/npm/dm/defu?style=flat&colorA=18181B&colorB=F0DB4F

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"build": "unbuild",
2222
"dev": "vitest",
2323
"lint": "eslint --ext .ts src && prettier -c src test",
24+
"lint:fix": "eslint --ext .ts src --fix && prettier -w src test",
2425
"prepack": "pnpm build",
2526
"release": "pnpm test && changelogen --release && git push --follow-tags && pnpm publish",
2627
"test": "pnpm lint && pnpm vitest run",

renovate.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"extends": [
3-
"github>unjs/renovate-config"
4-
]
2+
"extends": ["github>unjs/renovate-config"]
53
}

src/defu.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function _defu<T>(
99
baseObject: T,
1010
defaults: any,
1111
namespace = ".",
12-
merger?: Merger
12+
merger?: Merger,
1313
): T {
1414
if (!isObject(defaults)) {
1515
return _defu(baseObject, {}, namespace, merger);
@@ -39,7 +39,7 @@ function _defu<T>(
3939
value,
4040
object[key],
4141
(namespace ? `${namespace}.` : "") + key.toString(),
42-
merger
42+
merger,
4343
);
4444
} else {
4545
object[key] = value;
@@ -62,10 +62,7 @@ export default defu;
6262

6363
// Custom version with function merge support
6464
export const defuFn = createDefu((object, key, currentValue) => {
65-
if (
66-
typeof object[key] !== "undefined" &&
67-
typeof currentValue === "function"
68-
) {
65+
if (object[key] !== undefined && typeof currentValue === "function") {
6966
object[key] = currentValue(object[key]);
7067
return true;
7168
}

src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ export type Merger = <T extends Input, K extends keyof T>(
1111
object: T,
1212
key: keyof T,
1313
value: T[K],
14-
namespace: string
14+
namespace: string,
1515
) => any;
1616

1717
type nullish = null | undefined | void;
1818

1919
export type MergeObjects<
2020
Destination extends Input,
21-
Defaults extends Input
21+
Defaults extends Input,
2222
> = Destination extends Defaults
2323
? Destination
2424
: Omit<Destination, keyof Destination & keyof Defaults> &
@@ -35,7 +35,7 @@ export type MergeObjects<
3535

3636
export type Defu<
3737
S extends Input,
38-
D extends Array<Input | IgnoredInput>
38+
D extends Array<Input | IgnoredInput>,
3939
> = D extends [infer F, ...infer Rest]
4040
? F extends Input
4141
? Rest extends Array<Input | IgnoredInput>
@@ -50,7 +50,7 @@ export type Defu<
5050

5151
export type DefuFn = <
5252
Source extends Input,
53-
Defaults extends Array<Input | IgnoredInput>
53+
Defaults extends Array<Input | IgnoredInput>,
5454
>(
5555
source: Source,
5656
...defaults: Defaults

test/defu.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe("defu", () => {
8787

8888
it("should not override Object prototype", () => {
8989
const payload = JSON.parse(
90-
'{"constructor": {"prototype": {"isAdmin": true}}}'
90+
'{"constructor": {"prototype": {"isAdmin": true}}}',
9191
);
9292
defu({}, payload);
9393
defu(payload, {});
@@ -119,7 +119,7 @@ describe("defu", () => {
119119
baz: number[];
120120
}
121121
expectTypeOf(
122-
defu({} as SomeConfig, {} as SomeOtherConfig, {} as ThirdConfig)
122+
defu({} as SomeConfig, {} as SomeOtherConfig, {} as ThirdConfig),
123123
).toEqualTypeOf<ExpectedMergedType>();
124124
});
125125

@@ -137,11 +137,11 @@ describe("defu", () => {
137137
let options: (SomeConfig & SomeOtherConfig) | undefined;
138138

139139
expectTypeOf(
140-
defu(options ?? {}, { foo: ["test"] }, { bar: ["test2"] }, {})
140+
defu(options ?? {}, { foo: ["test"] }, { bar: ["test2"] }, {}),
141141
).toEqualTypeOf<ExpectedMergedType>();
142142

143143
expectTypeOf(
144-
defu({ foo: ["test"] }, {}, { bar: ["test2"] }, {})
144+
defu({ foo: ["test"] }, {}, { bar: ["test2"] }, {}),
145145
).toEqualTypeOf<ExpectedMergedType>();
146146
});
147147

@@ -167,8 +167,8 @@ describe("defu", () => {
167167
{
168168
ignore: ["node_modules", "dist"],
169169
num: 10,
170-
}
171-
)
170+
},
171+
),
172172
).toEqual({
173173
ignore: ["node_modules"],
174174
num: 20,
@@ -187,8 +187,8 @@ describe("defu", () => {
187187
{
188188
arr: ["a", "b"],
189189
num: 10,
190-
}
191-
)
190+
},
191+
),
192192
).toEqual({
193193
arr: ["c"],
194194
num,

tsconfig.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,5 @@
66
"skipLibCheck": true,
77
"declaration": true
88
},
9-
"include": [
10-
"src",
11-
"test"
12-
]
9+
"include": ["src", "test"]
1310
}

0 commit comments

Comments
 (0)