X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcircular-array.ts;h=bb958d8287519acf91efc4c7a1468fe422da0a86;hb=f6bc9f26d8a0246bbee14b2b03d0bcc41b8aeb52;hp=40bd19ae0fc74742380979fe05712a5e962251c6;hpb=4888bc9c988c5a0782ed642dc184c67207c767aa;p=poolifier.git diff --git a/src/circular-array.ts b/src/circular-array.ts index 40bd19ae..bb958d82 100644 --- a/src/circular-array.ts +++ b/src/circular-array.ts @@ -51,18 +51,28 @@ export class CircularArray extends Array { } /** @inheritDoc */ - public splice (start: number, deleteCount?: number, ...items: T[]): T[] { - let itemsRemoved: T[] - if (arguments.length >= 3 && deleteCount !== undefined) { - itemsRemoved = super.splice(start, deleteCount) - // FIXME: that makes the items insert not in place - this.push(...items) + public splice ( + start: number, + deleteCount?: number, + ...items: T[] + ): CircularArray { + let itemsRemoved: T[] = [] + if (arguments.length >= 3 && deleteCount != null) { + itemsRemoved = super.splice(start, deleteCount, ...items) + if (this.length > this.size) { + const itemsOverflowing = super.splice(0, this.length - this.size) + itemsRemoved = new CircularArray( + itemsRemoved.length + itemsOverflowing.length, + ...itemsRemoved, + ...itemsOverflowing + ) + } } else if (arguments.length === 2) { itemsRemoved = super.splice(start, deleteCount) } else { itemsRemoved = super.splice(start) } - return itemsRemoved + return itemsRemoved as CircularArray } public resize (size: number): void {