Skip to content

Commit

Permalink
Make priority queue only hold string keys
Browse files Browse the repository at this point in the history
  • Loading branch information
cpettitt committed Sep 24, 2014
1 parent ce27ecf commit 56d38d2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/priority-queue.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var _ = require("lodash");

module.exports = PriorityQueue;

/**
Expand Down Expand Up @@ -30,7 +32,7 @@ PriorityQueue.prototype.keys = function() {
* Returns `true` if **key** is in the queue and `false` if not.
*/
PriorityQueue.prototype.has = function(key) {
return key in this._keyIndices;
return _.has(this._keyIndices, key);
};

/**
Expand Down Expand Up @@ -67,7 +69,8 @@ PriorityQueue.prototype.min = function() {
*/
PriorityQueue.prototype.add = function(key, priority) {
var keyIndices = this._keyIndices;
if (!(key in keyIndices)) {
key = String(key);
if (!_.has(keyIndices, key)) {
var arr = this._arr;
var index = arr.length;
keyIndices[key] = index;
Expand Down
3 changes: 2 additions & 1 deletion test/priority-queue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ describe("PriorityQueue", function() {
pq.add(false, 3);
pq.add(undefined, 4);
pq.add(null, 5);
expect(_.sortBy(pq.keys())).to.eql(_.sortBy(["a", 1, false, undefined, null]));
expect(_.sortBy(pq.keys())).to.eql(
_.sortBy(["a", "1", "false", "undefined", "null"]));
});
});

Expand Down

0 comments on commit 56d38d2

Please sign in to comment.