|
| 1 | +/** |
| 2 | + * Removes a specified element from an array if it exists. |
| 3 | + * |
| 4 | + * @param {Array} elements - The array from which to remove the element. |
| 5 | + * @param {*} element - The element to be removed from the array. |
| 6 | + */ |
| 7 | +export function removeArrayElement(elements, element) { |
| 8 | + const index = elements.indexOf(element); |
| 9 | + if (index !== -1) { |
| 10 | + elements.splice(index, 1); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Adds an element to an array after a specified element if the element is not already present in the array. |
| 16 | + * |
| 17 | + * @param {Array} elements - The array to which the element will be added. |
| 18 | + * @param {*} afterElement - The element after which the new element will be added. |
| 19 | + * @param {*} element - The element to be added to the array. |
| 20 | + */ |
| 21 | +export function addArrayElementAfter(elements, afterElement, element) { |
| 22 | + const elementIndex = elements.indexOf(element); |
| 23 | + if (elementIndex === -1) { |
| 24 | + elements.splice(elements.indexOf(afterElement) + 1, 0, element); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Adds new elements to an array after a specified element, ensuring no duplicates. |
| 30 | + * |
| 31 | + * @param {Array} elements - The original array to modify. |
| 32 | + * @param {*} afterElement - The element after which new elements will be added. |
| 33 | + * @param {Array} newElements - The new elements to add to the array. |
| 34 | + */ |
| 35 | +export function addArrayElementsAfter(elements, afterElement, newElements) { |
| 36 | + const afterElementIndex = elements.indexOf(afterElement); |
| 37 | + if (afterElementIndex === -1) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const newElementsToAdd = newElements.filter((element) => !elements.includes(element)); |
| 42 | + elements.splice(afterElementIndex + 1, 0, ...newElementsToAdd); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Adds an element to an array before a specified element if the element is not already present in the array. |
| 47 | + * |
| 48 | + * @param {Array} elements - The array to which the element will be added. |
| 49 | + * @param {*} beforeElement - The element before which the new element will be added. |
| 50 | + * @param {*} element - The element to be added to the array. |
| 51 | + */ |
| 52 | +export function addArrayElementBefore(elements, beforeElement, element) { |
| 53 | + const elementIndex = elements.indexOf(element); |
| 54 | + if (elementIndex === -1) { |
| 55 | + elements.splice(elements.indexOf(beforeElement), 0, element); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Adds an element to an array if it is not already present. |
| 61 | + * |
| 62 | + * @param {Array} elements - The array to which the element should be added. |
| 63 | + * @param {*} element - The element to add to the array. |
| 64 | + */ |
| 65 | +export function addArrayElement(elements, element) { |
| 66 | + if (!elements.includes(element)) { |
| 67 | + elements.push(element); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Replaces an element in an array with a new element. |
| 73 | + * |
| 74 | + * @param {Array} elements - The array containing the element to be replaced. |
| 75 | + * @param {*} element - The element to be replaced. |
| 76 | + * @param {*} replacement - The new element to replace the old element. |
| 77 | + */ |
| 78 | +export function replaceArrayElement(elements, element, replacement) { |
| 79 | + const index = elements.indexOf(element); |
| 80 | + if (index !== -1) { |
| 81 | + elements[index] = replacement; |
| 82 | + } |
| 83 | +} |
0 commit comments