bitran
Version:
📜 Highly customizable text processor and transpiler.
150 lines (149 loc) • 4 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
class BitranDomError extends Error {
constructor(message) {
super(message);
this.name = "BitranDomError";
}
}
class Node {
constructor() {
__publicField(this, "name");
__publicField(this, "parent");
}
get children() {
return [];
}
//
// Tree traversal
//
walkDown(step) {
for (const childNode of this.children)
if (step(childNode) === false || childNode.walkDown((node) => step(node)) === false)
return false;
}
__walk(next, step) {
let current = this;
while (current = next(current))
if (step(current) === false)
return;
}
walkUp(step) {
this.__walk((current) => current.parent, step);
}
}
class GroupNode extends Node {
constructor(parent) {
super();
__publicField(this, "_children", []);
this.parent = parent;
}
get children() {
return this._children;
}
hasChildren() {
return !!this._children.length;
}
isEmpty() {
return !this.hasChildren();
}
/**
* Detach given node from any GroupNode it might belongs to.
*/
static detachNode(node) {
if (node.parent && node.parent instanceof GroupNode)
node.parent.detach(node);
}
//
// Low level operations
//
__locate(node) {
const index = this.children.indexOf(node);
if (index === -1)
throw new BitranDomError("Failed to locate node! Provided node does not belong to current GroupNode!");
return index;
}
__detachAt(index) {
const detachedNode = this.children.splice(index, 1).pop();
detachedNode.parent = void 0;
}
__insertAt(index, ...nodes) {
nodes.forEach((node) => {
GroupNode.detachNode(node);
node.parent = this;
});
this.children.splice(index, 0, ...nodes);
}
__swap(indexA, indexB) {
[this.children[indexA], this.children[indexB]] = [this.children[indexB], this.children[indexA]];
}
__move(indexSource, indexTarget, before = false) {
const movingNode = this.children[indexSource];
this.children.splice(indexSource, 1);
this.children.splice(indexTarget, 0, movingNode);
}
//
// Operations
//
detach(node) {
this.__detachAt(this.__locate(node));
}
clear() {
const detachCount = this.children.length;
for (let i = 0; i < detachCount; i++)
this.__detachAt(0);
}
setNodes(nodes) {
this.clear();
let nodeArr;
switch (arguments.length) {
case 0:
return;
case 1:
const arg0 = arguments[0];
nodeArr = arg0 instanceof Node ? [arg0] : [...arg0];
break;
default:
nodeArr = [];
for (const node of arguments)
nodeArr.push(node);
break;
}
this.__insertAt(0, ...nodeArr);
}
swap(nodeA, nodeB) {
this.__swap(this.__locate(nodeA), this.__locate(nodeB));
}
move(nodeSource, nodeTarget, before = false) {
if (nodeSource === nodeTarget)
return;
const indexSource = this.__locate(nodeSource);
let indexTarget = this.__locate(nodeTarget);
if (indexTarget < indexSource)
indexTarget++;
this.__move(indexSource, Math.max(0, indexTarget - (before ? 1 : 0)));
}
prepend(...nodes) {
this.__insertAt(0, ...nodes);
}
append(...nodes) {
this.__insertAt(this.children.length, ...nodes);
}
before(refNode, ...beforeNodes) {
this.__insertAt(this.__locate(refNode), ...beforeNodes);
}
after(refNode, ...afterNodes) {
this.__insertAt(this.__locate(refNode) + 1, ...afterNodes);
}
replace(toReplace, ...withNodes) {
this.after(toReplace, ...withNodes);
this.detach(toReplace);
}
}
export {
BitranDomError as B,
GroupNode as G,
Node as N
};
//# sourceMappingURL=group-HQBJ3zxp.js.map