You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
586 B
TypeScript

export function arrayMoveMutable<ValueType>(
array: ValueType[],
fromIndex: number,
toIndex: number
) {
const startIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex
if (startIndex >= 0 && startIndex < array.length) {
const endIndex = toIndex < 0 ? array.length + toIndex : toIndex
const [item] = array.splice(fromIndex, 1)
array.splice(endIndex, 0, item)
}
}
export function arrayMoveImmutable<T>(array: T[], fromIndex: number, toIndex: number) {
const newArray = [...array]
arrayMoveMutable(newArray, fromIndex, toIndex)
return newArray
}