Skip to content

Commit f6df314

Browse files
committed
feat!: concat array defaults to the last
BREAKING CHANGE: When merging input value with defaults with an array, order is reversed
1 parent 2a1c94d commit f6df314

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/defu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function _defu<T> (baseObj: T, defaults: any, namespace: string = '.', merger?:
2828
}
2929

3030
if (Array.isArray(val) && Array.isArray(obj[key])) {
31-
obj[key] = obj[key].concat(val)
31+
obj[key] = val.concat(obj[key])
3232
} else if (isObject(val) && isObject(obj[key])) {
3333
obj[key] = _defu(val, obj[key], (namespace ? `${namespace}.` : '') + key.toString(), merger)
3434
} else {

test/defu.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ describe('defu', () => {
3232
})
3333

3434
it('should concat array values by default', () => {
35-
const result = defu({ array: ['b', 'c'] }, { array: ['a'] })
35+
const result = defu({ array: ['a', 'b'] }, { array: ['c', 'd'] })
3636
expect(result).toEqual({
37-
array: ['a', 'b', 'c']
37+
array: ['a', 'b', 'c', 'd']
3838
})
3939
expectTypeOf(result).toEqualTypeOf<{ array: string[] }>()
4040
})
@@ -43,8 +43,8 @@ describe('defu', () => {
4343
const item1 = { name: 'Name', age: 21 }
4444
const item2 = { name: 'Name', age: '42' }
4545
const result = defu({ items: [item1] }, { items: [item2] })
46-
expect(result).toEqual({ items: [item2, item1] })
47-
expectTypeOf(result).toEqualTypeOf<{ items: Array<{ name: string, age: string } | { name: string, age: number }> }>()
46+
expect(result).toEqual({ items: [item1, item2] })
47+
expectTypeOf(result).toEqualTypeOf<{ items: Array<{ name: string, age: number } | { name: string, age: string }> }>()
4848
})
4949

5050
it('should correctly merge different object types', () => {

0 commit comments

Comments
 (0)