Skip to content

Instantly share code, notes, and snippets.

@maiconrs95
Created November 21, 2022 15:00
Show Gist options
  • Save maiconrs95/18a5c268387572046155ff8b89e5a82c to your computer and use it in GitHub Desktop.
Save maiconrs95/18a5c268387572046155ff8b89e5a82c to your computer and use it in GitHub Desktop.
export function update<T>(
cache: T[] | undefined,
predicate: (value: T, index: number, data: T[]) => boolean,
updater: (data: T) => T
): T[] | undefined {
if (!cache) {
return cache;
}
const data = [...cache];
const index = data.findIndex(predicate);
const item = data[index];
if (!item) {
return data;
}
data[index] = updater(item);
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment