From fd7fc877fcca48c32008227ca2b660f8c131018b Mon Sep 17 00:00:00 2001 From: alina sireneva Date: Mon, 16 Sep 2024 00:59:32 +0300 Subject: [PATCH] fix: removeOne(-1) from the end --- index.js | 2 +- test/denque.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6b2e9d8..ae393f9 100644 --- a/index.js +++ b/index.js @@ -184,7 +184,7 @@ Denque.prototype.removeOne = function removeOne(index) { i = (this._head + i) & this._capacityMask; var item = this._list[i]; var k; - if (index < size / 2) { + if (i < size / 2) { for (k = index; k > 0; k--) { this._list[i] = this._list[i = (i - 1 + len) & this._capacityMask]; } diff --git a/test/denque.js b/test/denque.js index 47fbd0f..eadeb1e 100644 --- a/test/denque.js +++ b/test/denque.js @@ -446,6 +446,12 @@ describe('Denque.prototype.removeOne', function () { assert(a.length === 0); }); + it('Should work with negative indexing', function () { + var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8]); + assert(a.removeOne(-1) === 8); + assert(a.length === 7); + assert.deepEqual(a.toArray(), [1, 2, 3, 4, 5, 6, 7]); + }); }); describe('Denque.prototype.remove', function () {