UNPKG

@undermuz/react-json-form

Version:
19 lines (18 loc) 560 B
// src/utils/arrayMove.ts function arrayMove(array, fromIndex, toIndex) { 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); } } function arrayMoveImmutable(array, fromIndex, toIndex) { const newArray = [...array]; arrayMove(newArray, fromIndex, toIndex); return newArray; } export { arrayMove, arrayMoveImmutable };