Files
feuerwerk/src/core/util/list.ts
T
rootandClaude Opus 5 4d84231b9f Trim needless comments and fix the picker width
Remove comments that only restate the code, keeping the section banners
and the notes that carry a reason, a measured fact or an external
contract.

ButtonOptions.width claimed to be a fixed width but only set minWidth,
so the Graph and Texture pickers grew with the name they showed. It is
now what it promises, with a separate minWidth for the buttons that have
two forms. Narrow buttons shrink their padding, otherwise a 24px icon
button had 8px left for its glyph and truncate dropped it entirely.

__gm.labelsDropped() guards that class: the overflow guard only saw text
that was too wide, never a label truncated to nothing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-08 15:52:05 +02:00

32 lines
1.3 KiB
TypeScript

export interface Removed<T> {
item: T;
index: number;
}
/** In place; `undefined` means nothing matched and the list is untouched. */
export function removeFirst<T>(list: T[], predicate: (item: T) => boolean): Removed<T> | undefined {
const index = list.findIndex(predicate);
// A valid index makes `splice` return exactly one element, which is what the `!` rests on.
return index < 0 ? undefined : { item: list.splice(index, 1)[0]!, index };
}
/** In place; the returned indices are the former ones, ascending. */
export function removeWhere<T>(list: T[], predicate: (item: T) => boolean): Removed<T>[] {
const removed: Removed<T>[] = [];
list.forEach((item, index) => {
if (predicate(item)) removed.push({ item, index });
});
for (let i = removed.length - 1; i >= 0; i--) list.splice(removed[i]!.index, 1);
return removed;
}
/** Inserting in ascending index order is what restores the list exactly. */
export function restoreAt<T>(list: T[], removed: readonly Removed<T>[]): void {
for (const entry of [...removed].sort((a, b) => a.index - b.index)) list.splice(entry.index, 0, entry.item);
}
/** Returns a new list; unlike the helpers above this one does not mutate. */
export function removeById<T extends { id: string }>(list: readonly T[], id: string): T[] {
return list.filter((item) => item.id !== id);
}