Skip to content

Commit

Permalink
feat(utils): 新增 moveUp, moveDown, moveToTop, moveToBottom
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Sep 2, 2021
1 parent 8674666 commit 27eaf97
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 2 deletions.
60 changes: 59 additions & 1 deletion src/utils/move.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { move } from './move'
import { move, moveDown, moveToBottom, moveToTop, moveUp } from './move'

describe('move', () => {
test('是原地移动且移动正常', () => {
Expand All @@ -22,4 +22,62 @@ describe('move', () => {
expect(move(arr.slice(), 0, 1)).toEqual([])
expect(move(arr.slice(), 1, 1)).toEqual([])
})

test('移动至负数位置正常', () => {
const arr: any[] = [1, 2, 3]
expect(move(arr.slice(), 0, -1)).toEqual([1, 2, 3])
expect(move(arr.slice(), 0, -2)).toEqual([1, 2, 3])
expect(move(arr.slice(), 1, -1)).toEqual([2, 1, 3])
expect(move(arr.slice(), 1, -200)).toEqual([2, 1, 3])
expect(move(arr.slice(), 5, -1)).toEqual([1, 2, 3])
expect(move(arr.slice(), 5, -100)).toEqual([1, 2, 3])
})

test('moveUp 正常', () => {
const arr: any[] = [1, 2, 3]
expect(moveUp(arr.slice(), -1)).toEqual([1, 2, 3])
expect(moveUp(arr.slice(), 0)).toEqual([1, 2, 3])
expect(moveUp(arr.slice(), 1)).toEqual([2, 1, 3])
expect(moveUp(arr.slice(), 2)).toEqual([1, 3, 2])
expect(moveUp(arr.slice(), 3)).toEqual([1, 2, 3])

expect(moveUp(arr.slice(), -1, 2)).toEqual([1, 2, 3])
expect(moveUp(arr.slice(), 0, 2)).toEqual([1, 2, 3])
expect(moveUp(arr.slice(), 1, 2)).toEqual([2, 1, 3])
expect(moveUp(arr.slice(), 2, 2)).toEqual([3, 1, 2])
expect(moveUp(arr.slice(), 3, 2)).toEqual([1, 2, 3])
})

test('moveDown 正常', () => {
const arr: any[] = [1, 2, 3]
expect(moveDown(arr.slice(), -1)).toEqual([1, 2, 3])
expect(moveDown(arr.slice(), 0)).toEqual([2, 1, 3])
expect(moveDown(arr.slice(), 1)).toEqual([1, 3, 2])
expect(moveDown(arr.slice(), 2)).toEqual([1, 2, 3])
expect(moveDown(arr.slice(), 3)).toEqual([1, 2, 3])

expect(moveDown(arr.slice(), -1, 2)).toEqual([1, 2, 3])
expect(moveDown(arr.slice(), 0, 2)).toEqual([2, 3, 1])
expect(moveDown(arr.slice(), 1, 2)).toEqual([1, 3, 2])
expect(moveDown(arr.slice(), 2, 2)).toEqual([1, 2, 3])
expect(moveDown(arr.slice(), 3, 2)).toEqual([1, 2, 3])
})

test('moveToTop 正常', () => {
const arr: any[] = [1, 2, 3]
expect(moveToTop(arr.slice(), -1)).toEqual([1, 2, 3])
expect(moveToTop(arr.slice(), 0)).toEqual([1, 2, 3])
expect(moveToTop(arr.slice(), 1)).toEqual([2, 1, 3])
expect(moveToTop(arr.slice(), 2)).toEqual([3, 1, 2])
expect(moveToTop(arr.slice(), 3)).toEqual([1, 2, 3])
})

test('moveToBottom 正常', () => {
const arr: any[] = [1, 2, 3]
expect(moveToBottom(arr.slice(), -1)).toEqual([1, 2, 3])
expect(moveToBottom(arr.slice(), 0)).toEqual([2, 3, 1])
expect(moveToBottom(arr.slice(), 1)).toEqual([1, 3, 2])
expect(moveToBottom(arr.slice(), 2)).toEqual([1, 2, 3])
expect(moveToBottom(arr.slice(), 3)).toEqual([1, 2, 3])
})
})
69 changes: 68 additions & 1 deletion src/utils/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,75 @@
* ```
*/
export function move<T>(arr: T[], from: number, to: number): T[] {
if (arr.length === 0 || from >= arr.length || from === to) return arr
const total = arr.length
if (total === 0 || from < 0 || from >= total) return arr
to = Math.min(total - 1, Math.max(0, to))
if (from === to) return arr
const item = arr.splice(from, 1)[0]
arr.splice(to, 0, item)
return arr
}

/**
* 原地上移数组中的某个元素。
*
* @param arr 要处理的数组
* @param from 要移动元素的索引
* @param step 移动步值
* @returns 返回移动后的数组
* @example
* ```typescript
* moveUp([1, 2, 3], 2) // => [1, 3, 2]
* moveUp([1, 2, 3], 2, 2) // => [3, 1, 2]
* ```
*/
export function moveUp<T>(arr: T[], from: number, step = 1): T[] {
return move(arr, from, from - step)
}

/**
* 原地下移数组中的某个元素。
*
* @param arr 要处理的数组
* @param from 要移动元素的索引
* @param step 移动步值
* @returns 返回移动后的数组
* @example
* ```typescript
* moveDown([1, 2, 3], 0) // => [2, 1, 3]
* moveDown([1, 2, 3], 0, 2) // => [2, 3, 1]
* ```
*/
export function moveDown<T>(arr: T[], from: number, step = 1): T[] {
return move(arr, from, from + step)
}

/**
* 原地置顶数组中的某个元素。
*
* @param arr 要处理的数组
* @param from 要移动元素的索引
* @returns 返回移动后的数组
* @example
* ```typescript
* moveToTop([1, 2, 3], 2) // => [3, 1, 2]
* ```
*/
export function moveToTop<T>(arr: T[], from: number): T[] {
return move(arr, from, 0)
}

/**
* 原地置底数组中的某个元素。
*
* @param arr 要处理的数组
* @param from 要移动元素的索引
* @returns 返回移动后的数组
* @example
* ```typescript
* moveToBottom([1, 2, 3], 0) // => [2, 3, 1]
* ```
*/
export function moveToBottom<T>(arr: T[], from: number): T[] {
return move(arr, from, arr.length - 1)
}

0 comments on commit 27eaf97

Please sign in to comment.