All files / app/codeCharta/util arrayHelper.ts

94.73% Statements 18/19
100% Branches 2/2
100% Functions 7/7
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3246x 46x   46x 4x     46x 1x     46x 3x 3x         46x 3x 3x 5x 3x     3x       8x    
import { clone } from "./clone"
import { dequal } from "dequal"
 
export function removeItemFromArray<T>(array: T[], searchItem: T) {
    return array.filter(entry => !dequal(entry, searchItem))
}
 
export function removeEntryAtIndexFromArray<T>(array: T[], index: number) {
    return [...array.slice(0, index), ...array.slice(index + 1)]
}
 
export function addItemToArray<T>(array: T[], item: T) {
    if (!arrayContainsItem(array, item)) {
        return [...array, clone(item)]
    }
    return array
}
 
export function addItemsToArray<T>(array: T[], items: T[]) {
    const newArray = [...array]
    for (const item of items) {
        if (!arrayContainsItem(newArray, item)) {
            newArray.push(item)
        }
    }
    return newArray
}
 
function arrayContainsItem<T>(array: T[], item: T) {
    return array.some(x => dequal(x, item))
}