Files
mixtape/zero.Web.UI/app/helpers/arrays.js
T
2021-03-23 00:43:33 +01:00

57 lines
822 B
JavaScript

const arrayMoveMutate = (array, from, to) =>
{
const startIndex = to < 0 ? array.length + to : to;
const item = array.splice(from, 1)[0];
array.splice(startIndex, 0, item);
};
/**
* Move an item in an array
*/
function move(array, from, to)
{
array = array.slice();
arrayMoveMutate(array, from, to);
return array;
}
/**
* Replaces an item in an array
*/
function replace(array, origin, target)
{
const index = array.indexOf(origin);
if (index < 0)
{
return index;
}
array.splice(index, 1);
array.splice(index, 0, target);
return index;
};
/**
* Removes an item from an array
*/
function remove(array, value)
{
const index = array.indexOf(value);
if (index < 0)
{
return;
}
array.splice(index, 1);
return index;
}
export default {
move,
replace,
remove
};