/** * Insert one or multiple elements into a given position of an array. * * @param {Array} array Source array. * @param {*} elements Elements to insert. * @param {number} index Insert Position. * * @return {Array} Result. */ export function insertAt< T, U >( array: T[], elements: U | U[], index: number ) { return [ ...array.slice( 0, index ), ...( Array.isArray( elements ) ? elements : [ elements ] ), ...array.slice( index ), ]; } /** * Moves an element in an array. * * @param {Array} array Source array. * @param {number} from Source index. * @param {number} to Destination index. * @param {number} count Number of elements to move. * * @return {Array} Result. */ export function moveTo< T >( array: T[], from: number, to: number, count = 1 ) { const withoutMovedElements = [ ...array ]; withoutMovedElements.splice( from, count ); return insertAt( withoutMovedElements, array.slice( from, from + count ), to ); }