/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
packages/block-editor/src/store/array.js
36 строк
918 B
Marin Atanasov
Lodash: Refactor away from _.castArray() (#45098)
20 окт 2022, 09:26
Не верифицирован
20 окт 2022, 09:26
1c5a80f
Код
Авторство
О чём код?
/** * 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( array, elements, index ) { 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( array, from, to, count = 1 ) { const withoutMovedElements = [ ...array ]; withoutMovedElements.splice( from, count ); return insertAt( withoutMovedElements, array.slice( from, from + count ), to ); }