Code coverage report for lib/queue.js

Statements: 100% (33 / 33)      Branches: 100% (6 / 6)      Functions: 100% (6 / 6)      Lines: 100% (33 / 33)      Ignored: none     

All files » lib/ » queue.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74          13 11461 11461 11461                             13 11467 7929 7929 7929 7929 7929 7929 2296     9171 9171 9171 9171     13 9244     13 2   2   2 1 1     1 1       2     13 14486     13   14486       13  
'use strict';
 
// Queue class adapted from Tim Caswell's pattern library
// http://github.com/creationix/pattern/blob/master/lib/pattern/queue.js
 
function Queue() {
    this.tail = [];
    this.head = [];
    this.offset = 0;
}
 
// Queue.prototype.peek = function () {
//     // Mirroring shift's logic.
//     if (this.offset === this.head.length) {
//         if (this.tail.length === 0) {
//             return;
//         } else {
//             return this.tail[0];
//         }
//     }
//     return this.head[this.offset];
// };
 
Queue.prototype.shift = function () {
    if (this.offset === this.head.length) {
        var tmp = this.head;
        tmp.length = 0;
        this.head = this.tail;
        this.tail = tmp;
        this.offset = 0;
        if (this.head.length === 0) {
            return;
        }
    }
    var item = this.head[this.offset];
    this.head[this.offset] = null;
    this.offset++;
    return item;
};
 
Queue.prototype.push = function (item) {
    return this.tail.push(item);
};
 
Queue.prototype.forEach = function (fn, thisv) {
    var array = this.head.slice(this.offset), i, il;
 
    array.push.apply(array, this.tail);
 
    if (thisv) {
        for (i = 0, il = array.length; i < il; i += 1) {
            fn.call(thisv, array[i], i, array);
        }
    } else {
        for (i = 0, il = array.length; i < il; i += 1) {
            fn(array[i], i, array);
        }
    }
 
    return array;
};
 
Queue.prototype.getLength = function () {
    return this.head.length - this.offset + this.tail.length;
};
 
Object.defineProperty(Queue.prototype, "length", {
    get: function () {
        return this.getLength();
    }
});
 
module.exports = Queue;