stack-base-iterator
Version:
Base iterator for values retrieved using a stack of async functions returning values
87 lines • 2.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return LinkedList;
}
});
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var Node = function Node(value) {
"use strict";
_class_call_check(this, Node);
this.value = value;
this.prev = null;
this.next = null;
};
var LinkedList = /*#__PURE__*/ function() {
"use strict";
function LinkedList() {
_class_call_check(this, LinkedList);
this.head = null;
this.tail = null;
this.length = 0;
}
var _proto = LinkedList.prototype;
_proto.last = function last() {
return this.tail ? this.tail.value : undefined;
};
_proto.push = function push(value) {
var newNode = new Node(value);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
return this;
};
_proto.pop = function pop() {
if (!this.head) return undefined;
var poppedNode = this.tail;
if (this.length === 1) {
this.head = null;
this.tail = null;
} else {
this.tail = poppedNode.prev;
this.tail.next = null;
poppedNode.prev = null;
}
this.length--;
return poppedNode.value;
};
_proto.remove = function remove(value) {
if (!this.head) return undefined;
var currentNode = this.head;
while(currentNode){
if (currentNode.value === value) {
if (currentNode === this.head) {
this.head = currentNode.next;
if (this.head) this.head.prev = null;
else this.tail = null;
} else if (currentNode === this.tail) {
this.tail = currentNode.prev;
this.tail.next = null;
} else {
currentNode.prev.next = currentNode.next;
currentNode.next.prev = currentNode.prev;
}
this.length--;
return currentNode.value;
}
currentNode = currentNode.next;
}
return undefined;
};
return LinkedList;
}();
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }