Skip to content

Commit

Permalink
fix!: push & unshift should accept undefined values to match be…
Browse files Browse the repository at this point in the history
…haviour of `Array` (#35)
  • Loading branch information
Salakar authored Aug 18, 2021
1 parent 5a2ba45 commit c6b0265
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Denque.prototype.size = function size() {
* @param item
*/
Denque.prototype.unshift = function unshift(item) {
if (item === undefined) return this.size();
if (arguments.length === 0) return this.size();
var len = this._list.length;
this._head = (this._head - 1 + len) & this._capacityMask;
this._list[this._head] = item;
Expand Down Expand Up @@ -132,7 +132,7 @@ Denque.prototype.shift = function shift() {
* @param item
*/
Denque.prototype.push = function push(item) {
if (item === undefined) return this.size();
if (arguments.length === 0) return this.size();
var tail = this._tail;
this._list[tail] = item;
this._tail = (tail + 1) & this._capacityMask;
Expand Down
13 changes: 13 additions & 0 deletions test/denque.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ describe('Denque.prototype.push', function () {
assert(ret === 0);
});


it('Should accept undefined values', function () {
var a = new Denque();
a.push(undefined);
assert(a.length === 1);
});

it('Should add falsey elements (except undefined)', function () {
var a = new Denque();
// var before = a.length;
Expand Down Expand Up @@ -163,6 +170,12 @@ describe('Denque.prototype.unshift', function () {
assert(ret === 0);
});

it('Should accept undefined values', function () {
var a = new Denque();
a.unshift(undefined);
assert(a.length === 1);
});

it('Should unshift falsey elements (except undefined)', function () {
var a = new Denque();
// var before = a.length;
Expand Down

0 comments on commit c6b0265

Please sign in to comment.