# Code coverage report for lib/queue.js

## Statements: 30.3% (10 / 33) Branches: 0% (0 / 6) Functions: 16.67% (1 / 6) Lines: 30.3% (10 / 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

    1
    1
    1
    1

    1

    1

    1

    1

    1

    1
     '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.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;