node-beanstalk
Version:
The most comprehensive beanstalk client for nodejs
84 lines • 2.22 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkedList = void 0;
var LinkedList = /** @class */ (function () {
function LinkedList() {
this.size = 0;
}
/**
* Remove node from chain and nullish it.
*/
LinkedList.prototype.removeNode = function (node) {
var next = node.next, prev = node.prev;
if (prev)
prev.next = next;
if (next)
next.prev = prev;
if (node === this.head)
this.head = next;
if (node === this.tail)
this.tail = prev;
node.next = undefined;
node.prev = undefined;
node.list = undefined;
this.size--;
return next;
};
/**
* Push existing list node to list's endings
*/
LinkedList.prototype.pushNode = function (node) {
node.list = this;
node.prev = this.tail;
if (this.tail) {
this.tail.next = node;
}
this.tail = node;
if (!this.head) {
this.head = node;
}
this.size++;
return node;
};
/**
* Add {value} to the tail of the list.
*/
LinkedList.prototype.push = function (value) {
return this.pushNode({
list: this,
value: value,
next: undefined,
prev: undefined,
});
};
/**
* Remove {count} elements from the head of the list.
*
* @return Array containing removed values.
*/
LinkedList.prototype.unshift = function () {
if (!this.head)
return undefined;
var val = this.head.value;
this.removeNode(this.head);
return val;
};
/**
* Remove all items from list.
* Also dereferences existing list nodes.
*/
LinkedList.prototype.truncate = function () {
var item = this.head;
var items = [];
while (item) {
var next = item.next;
this.removeNode(item);
items.push(item.value);
item = next;
}
return items;
};
return LinkedList;
}());
exports.LinkedList = LinkedList;
//# sourceMappingURL=LinkedList.js.map