Skip to content

Commit

Permalink
Merge pull request #92 from thomasperi/master
Browse files Browse the repository at this point in the history
Fix reactive array splice bug when called with single argument
  • Loading branch information
justin-schroeder authored Feb 21, 2024
2 parents 31c1861 + 86bbbd6 commit f23abf1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,11 @@ describe('reactive', () => {
await nextTick()
expect(callback.mock.calls[2][0]).toBe(500)
})

it('should preserve argument count for splice', () => {
const data = reactive([1, 2, 3, 4, 5])
const tail = data.splice(2)
expect(data).toEqual([1, 2])
expect(tail).toEqual([3, 4, 5])
})
})
10 changes: 8 additions & 2 deletions src/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,14 @@ function arrayOperation(
case 'fill':
return (...args: any[]) => synthetic(...args.map((arg) => r(arg)))
case 'splice':
return (start: number, remove?: number, ...inserts: any[]) =>
synthetic(start, remove, ...inserts.map((arg) => r(arg)))
return function (start: number, remove?: number, ...inserts: any[]) {
// Preserve the argument count when there's only one argument,
// because if a second argument is passed but undefined,
// it gets treated as 0.
return arguments.length === 1 ?
synthetic(start) :
synthetic(start, remove, ...inserts.map((arg) => r(arg)))
}
default:
return native
}
Expand Down

0 comments on commit f23abf1

Please sign in to comment.