Skip to content

Instantly share code, notes, and snippets.

@abezhinaru
Last active January 25, 2022 20:00
Show Gist options
  • Save abezhinaru/0afb4f43f3d5c13b6f83528e9d037bc3 to your computer and use it in GitHub Desktop.
Save abezhinaru/0afb4f43f3d5c13b6f83528e9d037bc3 to your computer and use it in GitHub Desktop.
/**
* Remove item from array by index
*
* @param {object[]} arr
* @param {number} index
*
* @returns {*[]}
*/
export const removeByIndex = (arr, index) => ([
...arr.slice(0, index),
...arr.slice(index + 1, arr.length)
]);
const addByIndex = (arr, index, newItem) => ([
...arr.slice(0, index),
newItem,
...arr.slice(index, arr.length)
]);
/**
* Remove item from array by value index
*
* @param arr
* @param value
* @return {*[]}
*/
export const removeByValue = (arr, value) => {
const index = arr.indexOf(value);
if (index === -1) {
return arr;
}
return removeByIndex(arr, index);
};
/**
* check that array contains a value
* @param array
* @param value
* @return {*}
*/
export const includes = (array, value) => array.includes(value);
/**
* Check if element is last in array by index
* @param items
* @param index
* @returns {boolean}
*/
export const isLastByIndex = (items, index) => index === items.length - 1;
/**
*
* Check if index is in array range
* @param arr
* @param index
* @returns {boolean}
*/
export const isValidIndex = (arr, index) => arr.length > index && index >= 0;
/**
* Replace item from array by index by newItem
* @param {object[]} arr
* @param {number} index
* @param {object} newItem
* @returns {*[]}
*/
export const setByIndex = (arr, index, newItem) => ([
...arr.slice(0, index),
newItem,
...arr.slice(index + 1, arr.length)
]);
/**
* Add or remove value from array
* @param items
* @param value
* @param comparator
* @return {*[]}
*/
export function addOrRemoveValue(items, value, comparator = includes) {
return comparator(items, value) ? removeByValue(items, value) : [...items, value];
}
/**
*
* @param items
* @param sourceIndex
* @param destinationIndex
* @returns {[]}
*/
export const swapValues = (items, sourceIndex, destinationIndex) => {
if (!isValidIndex(items, sourceIndex) || !isValidIndex(items, destinationIndex)) {
return items;
}
const newItems = [...items];
const value = newItems[sourceIndex];
newItems[sourceIndex] = newItems[destinationIndex];
newItems[destinationIndex] = value;
return newItems;
};
/**
* @param items
* @param sourceIndex
* @param destinationIndex
* @returns {[]}
*/
export const moveItemTo = (items, sourceIndex, destinationIndex) => {
if (!isValidIndex(items, sourceIndex) || !isValidIndex(items, destinationIndex)) {
return items;
}
const newItems = [...items];
const [removedItem] = newItems.splice(sourceIndex, 1);
newItems.splice(destinationIndex, 0, removedItem);
return newItems;
};
/**
* Helper to check whether array contains sub array.
*
* The order of arrays is important.
* For example, arr = [1, 2, 3, 4] and subArr = [2, 1] will return false.
* But arr = [1, 2, 3, 4] and subArr = [1, 2] will return true
*
* @param {array} items - The base array
* @param {array} subArray - The sub array
* @returns {boolean}
*/
export const isIncludesSubArray = (items, subArray) => {
const [firstSubArrItem] = subArray;
const firstSubArrItemIndex = items.indexOf(firstSubArrItem);
if (firstSubArrItemIndex < 0) return false;
return subArray
.every((item, index) => (
items.includes(item)
&& items.indexOf(item) === firstSubArrItemIndex + index
));
};
/**
* A function helper for inserting an item into array
* @param {array} items
* @param {number} index
* @param {*} item
*/
export const appendBefore = (items, index, item) => ([
...items.slice(0, index),
item,
...items.slice(index)
]);
/**
* Set new value in object inside items by index and return new array
*
* @param {object[]} items
* @param {number} index
* @param {object} data
* @return {object[]}
*/
export const patchObjectInArray = (items, index, data) => {
const newItems = [...items];
newItems[index] = {
...newItems[index],
...data
};
return newItems;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment