X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcircular-array.ts;h=bb958d8287519acf91efc4c7a1468fe422da0a86;hb=0eb836fa3f5683400594b9c595650b49ff8ae0ac;hp=654abb656da74bac380372cb39341aa9f2f831c5;hpb=41aa7dcd8bbc5ff5a5aefe8765fe8a7c57d6be00;p=poolifier.git diff --git a/src/circular-array.ts b/src/circular-array.ts index 654abb65..bb958d82 100644 --- a/src/circular-array.ts +++ b/src/circular-array.ts @@ -17,7 +17,7 @@ export class CircularArray extends Array { } } - /** @inheritdoc */ + /** @inheritDoc */ public push (...items: T[]): number { const length = super.push(...items) if (length > this.size) { @@ -26,7 +26,7 @@ export class CircularArray extends Array { return this.length } - /** @inheritdoc */ + /** @inheritDoc */ public unshift (...items: T[]): number { const length = super.unshift(...items) if (length > this.size) { @@ -35,7 +35,7 @@ export class CircularArray extends Array { return this.length } - /** @inheritdoc */ + /** @inheritDoc */ public concat (...items: Array>): CircularArray { const concatenatedCircularArray = super.concat( items as T[] @@ -50,19 +50,29 @@ export class CircularArray extends Array { return concatenatedCircularArray } - /** @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) + /** @inheritDoc */ + 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 {