All files / confluent-kafka-javascript/lib/kafkajs _linked-list.js

48.64% Statements 36/74
37.5% Branches 9/24
37.5% Functions 6/16
48.64% Lines 36/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221                  268x   268x   268x     268x       245x                         28x 28x 28x                     268x 268x 268x 201x   67x   268x     268x   268x 268x                   267x       267x     267x   267x 201x   66x   267x 267x 267x 267x                   315x 70x     245x 245x 245x                                                                           268x 268x                                                                                                                                     133x               1x        
/**
 * Node class for linked list, after being removed
 * it cannot be used again.
 * @private
 */
class LinkedListNode {
    // Value contained by the node.
    #value;
    // Node was removed from the list.
    _removed = false;
    // Next node in the list.
    _prev = null;
    // Previous node in the list.
    _next = null;
 
    constructor(value) {
        this.#value = value;
    }
 
    get value() {
        return this.#value;
    }
 
    get prev() {
        return this._prev;
    }
 
    get next() {
        return this._next;
    }
}
 
class LinkedList {
    _head = null;
    _tail = null;
    #count = 0;
 
    *#iterator() {
        let node = this._head;
        while (node) {
            yield node.value;
            node = node._next;
        }
    }
 
    #insertInBetween(node, prev, next) {
        node._next = next;
        node._prev = prev;
        if (prev)
            prev._next = node;
        else
            this._head = node;
 
        Iif (next)
            next._prev = node;
        else
            this._tail = node;
 
        this.#count++;
        return node;
    }
 
    /**
     * Removes given node from the list,
     * if it is not already removed.
     *
     * @param {LinkedListNode} node
     */
    remove(node) {
        Iif (node._removed) {
            return;
        }
 
        Iif (node._prev)
            node._prev._next = node._next;
        else
            this._head = node._next;
 
        if (node._next)
            node._next._prev = node._prev;
        else
            this._tail = node._prev;
 
        node._next = null;
        node._prev = null;
        node._removed = true;
        this.#count--;
    }
 
    /**
     * Removes the first node from the list and returns it,
     * or null if the list is empty.
     *
     * @returns {any} The value of the first node in the list or null.
     */
    removeFirst() {
        if (this._head === null) {
            return null;
        }
 
        const node = this._head;
        this.remove(node);
        return node.value;
    }
 
    /**
     * Removes the last node from the list and returns its value,
     * or null if the list is empty.
     *
     * @returns {any} The value of the last node in the list or null.
     */
    removeLast() {
        if (this._tail === null) {
            return null;
        }
 
        const node = this._tail;
        this.remove(node);
        return node.value;
    }
 
    /**
     * Add a new node to the beginning of the list and returns it.
     *
     * @param {any} value
     * @returns {LinkedListNode} The new node.
     */
    addFirst(value) {
        const node = new LinkedListNode(value);
        return this.#insertInBetween(node, null,
            this._head);
    }
 
    /**
     * Add a new node to the end of the list and returns it.
     *
     * @param {any} value Node value.
     * @returns {LinkedListNode} The new node.
     */
    addLast(value) {
        const node = new LinkedListNode(value);
        return this.#insertInBetween(node, this._tail, null);
    }
 
    /**
     * Add a new node before the given node and returns it.
     * Given node must not be removed.
     *
     * @param {LinkedListNode} node Reference node.
     * @param {any} value New node value.
     * @returns {LinkedListNode} The new node.
     */
    addBefore(node, value) {
        if (node._removed)
            throw new Error('Node was removed');
        const newNode = new LinkedListNode(value);
        return this.#insertInBetween(newNode, node._prev, node);
    }
 
    /**
     * Add a new node after the given node and returns it.
     * Given node must not be removed.
     *
     * @param {LinkedListNode} node Reference node.
     * @param {any} value New node value.
     * @returns {LinkedListNode} The new node.
     */
    addAfter(node, value) {
        if (node._removed)
            throw new Error('Node was removed');
        const newNode = new LinkedListNode(value);
        return this.#insertInBetween(newNode, node, node._next);
    }
 
    /**
     * Concatenates the given list to the end of this list.
     *
     * @param {LinkedList} list List to concatenate.
     */
    concat(list) {
        if (list.length === 0) {
            return;
        }
 
        if (this._tail) {
            this._tail._next = list._head;
        }
 
        if (list._head) {
            list._head._prev = this._tail;
        }
 
        this._tail = list._tail;
        this.#count += list.length;
        list.#count = 0;
        list._head = null;
        list._tail = null;
    }
 
    get first() {
        return this._head;
    }
 
    get last() {
        return this._tail;
    }
 
    get length() {
        return this.#count;
    }
 
    [Symbol.iterator]() {
        return this.#iterator();
    }
}
 
module.exports = {
    LinkedList,
    LinkedListNode
};