Skip to content

Commit

Permalink
Merge pull request #3 from kessler/master
Browse files Browse the repository at this point in the history
expose a forEach method as part of queue api
  • Loading branch information
janogonzalez committed Feb 6, 2014
2 parents ab13031 + 9807b94 commit 9fdc4fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ PriorityQueue.prototype.size = function() {
return this._elements.length;
};

/**
* Iterates over queue elements
*
* @param {Function} fn
*/
PriorityQueue.prototype.forEach = function(fn) {
return this._elements.forEach(fn);
};

/**
* Compares the values at position `a` and `b` in the priority queue using its
* comparator function.
Expand Down
19 changes: 19 additions & 0 deletions test/priorityqueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,23 @@ describe('PriorityQueue()', function() {
expect(queue.size()).to.be(2);
});
});

describe('#forEach()', function() {
it('iterates over all queue elements', function () {
var queue = new PriorityQueue();
queue.enq('a');
queue.enq('b');
var iteration = [];

queue.forEach(function(element, index) {
iteration.push([element, index]);
});

expect(iteration.length).to.be(2);
expect(iteration[0][0]).to.be.eql('b');
expect(iteration[0][1]).to.be.eql(0);
expect(iteration[1][0]).to.be.eql('a');
expect(iteration[1][1]).to.be.eql(1);
});
});
});

0 comments on commit 9fdc4fb

Please sign in to comment.