UNPKG

rc-js-util

Version:

A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.

22 lines (19 loc) 439 B
/** * @public * Replaces the first match of itemToRemove with replaceWith. * * @returns true if an element was replaced. * * @remarks * See {@link arrayReplaceOne}. */ export function arrayReplaceOne<TItem>(items: TItem[], itemToRemove: TItem, replaceWith: TItem): boolean { const index = items.indexOf(itemToRemove); if (index === -1) { return false; } items[index] = replaceWith; return true; }