Skip to content

Commit

Permalink
feat!: concat array defaults to the last
Browse files Browse the repository at this point in the history
BREAKING CHANGE: When merging input value with defaults with an array, order is reversed
  • Loading branch information
pi0 committed Mar 21, 2022
1 parent 2a1c94d commit f6df314
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/defu.ts
Expand Up @@ -28,7 +28,7 @@ function _defu<T> (baseObj: T, defaults: any, namespace: string = '.', merger?:
}

if (Array.isArray(val) && Array.isArray(obj[key])) {
obj[key] = obj[key].concat(val)
obj[key] = val.concat(obj[key])
} else if (isObject(val) && isObject(obj[key])) {
obj[key] = _defu(val, obj[key], (namespace ? `${namespace}.` : '') + key.toString(), merger)
} else {
Expand Down
8 changes: 4 additions & 4 deletions test/defu.test.ts
Expand Up @@ -32,9 +32,9 @@ describe('defu', () => {
})

it('should concat array values by default', () => {
const result = defu({ array: ['b', 'c'] }, { array: ['a'] })
const result = defu({ array: ['a', 'b'] }, { array: ['c', 'd'] })
expect(result).toEqual({
array: ['a', 'b', 'c']
array: ['a', 'b', 'c', 'd']
})
expectTypeOf(result).toEqualTypeOf<{ array: string[] }>()
})
Expand All @@ -43,8 +43,8 @@ describe('defu', () => {
const item1 = { name: 'Name', age: 21 }
const item2 = { name: 'Name', age: '42' }
const result = defu({ items: [item1] }, { items: [item2] })
expect(result).toEqual({ items: [item2, item1] })
expectTypeOf(result).toEqualTypeOf<{ items: Array<{ name: string, age: string } | { name: string, age: number }> }>()
expect(result).toEqual({ items: [item1, item2] })
expectTypeOf(result).toEqualTypeOf<{ items: Array<{ name: string, age: number } | { name: string, age: string }> }>()
})

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

0 comments on commit f6df314

Please sign in to comment.