@panyam/tsutils
Version:
Some basic TS utils for personal use
212 lines • 5.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.List = exports.ValueList = void 0;
class MutableListNode {
constructor(value) {
this.value = value;
this.nextSibling = null;
this.prevSibling = null;
}
}
class ValueList {
constructor(...values) {
this._firstChild = null;
this._lastChild = null;
this._size = 0;
for (const v of values)
this.pushBack(v);
}
toJSON() {
return Array.from(this.values());
}
forEach(method) {
let tmp = this._firstChild;
let count = 0;
while (tmp != null) {
if (method(tmp) == false) {
break;
}
count++;
tmp = tmp.nextSibling;
}
return count;
}
equals(another, eqlFunc) {
if (this.size != another.size)
return false;
let tmp = this.first;
let tmp2 = another.first;
for (; tmp != null && tmp2 != null; tmp = tmp.nextSibling, tmp2 = tmp2.nextSibling) {
if (!eqlFunc(tmp, tmp2)) {
return false;
}
}
return tmp == null && tmp2 == null;
}
get isEmpty() {
return this._size == 0;
}
get size() {
return this._size;
}
get first() {
return this._firstChild;
}
get last() {
return this._lastChild;
}
*reversedValues() {
let tmp = this._lastChild;
while (tmp != null) {
yield tmp;
tmp = tmp.prevSibling;
}
}
*values() {
let tmp = this._firstChild;
while (tmp != null) {
yield tmp;
tmp = tmp.nextSibling;
}
}
add(child, before = null) {
if (child.nextSibling != null || child.prevSibling != null) {
throw new Error("New node already added to a list. Remove it first");
}
child.nextSibling = child.prevSibling = null;
this._size++;
if (this._firstChild == null || this._lastChild == null) {
this._firstChild = this._lastChild = child;
}
else if (before == null) {
child.prevSibling = this._lastChild;
child.nextSibling = null;
this._lastChild.nextSibling = child;
this._lastChild = child;
}
else if (before == this._firstChild) {
child.nextSibling = before;
child.prevSibling = null;
this._firstChild.prevSibling = child;
this._firstChild = child;
}
else {
const prev = before.prevSibling;
child.nextSibling = before;
before.prevSibling = child;
child.prevSibling = prev;
if (prev != null) {
prev.nextSibling = child;
}
}
return this;
}
pushFront(value) {
return this.add(value, this._firstChild);
}
pushBack(value) {
return this.add(value);
}
remove(child) {
const next = child.nextSibling;
const prev = child.prevSibling;
if (next == null) {
this._lastChild = prev;
if (prev == null)
this._firstChild = null;
}
else {
next.prevSibling = prev;
}
if (prev == null) {
this._firstChild = next;
if (next == null)
this._lastChild = null;
}
else {
prev.nextSibling = next;
}
this._size--;
child.prevSibling = child.nextSibling = null;
return this;
}
popBack() {
if (this._lastChild == null) {
throw new Error("No children");
}
const out = this._lastChild;
this.remove(out);
return out;
}
popFront() {
if (this._firstChild == null) {
throw new Error("No children");
}
const out = this._firstChild;
this.remove(out);
return out;
}
}
exports.ValueList = ValueList;
class List {
constructor(...values) {
this.container = new ValueList();
for (const v of values)
this.push(v);
}
toJSON() {
return Array.from(this.values());
}
forEach(method) {
return this.container.forEach((v) => method(v.value));
}
equals(another, eqlFunc) {
return this.container.equals(another.container, (a, b) => eqlFunc(a.value, b.value));
}
find(target) {
for (const v of this.container.values()) {
if (target == v.value) {
return v;
}
}
return null;
}
get isEmpty() {
return this.container.isEmpty;
}
get size() {
return this.container.size;
}
get first() {
return this.container.first;
}
get last() {
return this.container.last;
}
*reversedValues() {
for (const v of this.container.reversedValues())
yield v.value;
}
*values() {
for (const v of this.container.values())
yield v.value;
}
popBack() {
return this.container.popBack().value;
}
popFront() {
return this.container.popFront().value;
}
pushFront(value) {
return this.add(value, this.container.first);
}
push(value) {
return this.add(value);
}
add(child, before = null) {
this.container.add(new MutableListNode(child), before);
return this;
}
}
exports.List = List;
//# sourceMappingURL=list.js.map