15 lines
344 B
TypeScript
15 lines
344 B
TypeScript
export const appendCapped = <T>(
|
|
prev: T[],
|
|
next: T[],
|
|
max: number,
|
|
onDrop?: (item: T) => void
|
|
) => {
|
|
const merged = [...prev, ...next]
|
|
if (merged.length <= max) return merged
|
|
const dropCount = merged.length - max
|
|
if (onDrop) {
|
|
for (let i = 0; i < dropCount; i += 1) onDrop(merged[i])
|
|
}
|
|
return merged.slice(dropCount)
|
|
}
|