Skip to content

Commit

Permalink
fix(move): 对空数组移动应返回空数组
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 14, 2021
1 parent bfc72ea commit f93f090
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/utils/move.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ describe('move', () => {
test('是原地移动且移动正常', () => {
const arr = [1, 2, 3]
expect(move(arr, 0, 1)).toBe(arr).toEqual([2, 1, 3])
expect(move(arr, 1, 1)).toBe(arr).toEqual([2, 1, 3])
})

test('from 等于 to 时移动正常', () => {
const arr = [1, 2, 3]
expect(move(arr, 1, 1)).toBe(arr).toEqual([1, 2, 3])
})

test('超过长度时不补空', () => {
const arr = [1, 2, 3]
expect(move(arr.slice(), 0, 5)).toEqual([2, 3, 1])
expect(move(arr.slice(), 7, 1)).toEqual([1, 2, 3])
})

test('对空数组移动应返回空数组', () => {
const arr: any[] = []
expect(move(arr.slice(), 0, 1)).toEqual([])
expect(move(arr.slice(), 1, 1)).toEqual([])
})
})
1 change: 1 addition & 0 deletions src/utils/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* ```
*/
export function move<T>(arr: T[], from: number, to: number): T[] {
if (arr.length === 0 || from >= arr.length || from === to) return arr
const item = arr.splice(from, 1)[0]
arr.splice(to, 0, item)
return arr
Expand Down

0 comments on commit f93f090

Please sign in to comment.