import type { Writable } from 'svelte/store';

export function addToWritableArray<T>(writable: Writable<T[]>, t: T) {
  writable.update((old) => {
    old.push(t);
    return old;
  });
}

export function updateWritableArray<T>(writable: Writable<T[]>, t: T, cmp: (t: T) => boolean) {
  writable.update((old) => {
    const index = old.findIndex(cmp);
    old[index] = t;
    return old;
  });
}

export function deleteFromWritableArray<T>(writable: Writable<T[]>, cmp: (t: T) => boolean) {
  writable.update((old) => {
    const index = old.findIndex(cmp);
    old.splice(index, 1);
    return old;
  });
}
