@platform/state
Version:
A small, simple, strongly typed, [rx/observable] state-machine.
192 lines (191 loc) • 7.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TreeQuery = void 0;
var common_1 = require("../common");
var TreeIdentity_1 = require("../TreeIdentity");
var TreeQuery = (function () {
function TreeQuery(args) {
var _this = this;
this.walkDown = function (visit) {
var stopped = false;
var walk = function (args) {
if (!args.node || stopped) {
return;
}
var _a = TreeIdentity_1.TreeIdentity.parse(args.node.id), id = _a.id, key = _a.key, namespace = _a.namespace;
if (_this.namespace && namespace !== _this.namespace) {
return;
}
var skipChildren = false;
visit({
id: id,
key: key,
namespace: namespace,
node: args.node,
parent: args.parent,
index: args.index,
level: args.depth,
stop: function () { return (stopped = true); },
skip: function () { return (skipChildren = true); },
});
if (stopped) {
return;
}
var index = -1;
if (!skipChildren) {
for (var _i = 0, _b = TreeQuery.children(args.node, undefined, { assign: false }); _i < _b.length; _i++) {
var child = _b[_i];
index++;
walk({
node: child,
parent: args.node,
index: index,
depth: args.depth + 1,
});
}
}
};
return walk({ node: _this.root, depth: 0, index: -1 });
};
this.walkUp = function (startAt, visit) {
var level = -1;
var inner = function (startAt, visit) {
var current = _this.findById((0, common_1.toNodeId)(startAt));
level++;
if (current) {
var stop_1 = false;
var parentNode_1 = _this.parent(current);
var _a = TreeIdentity_1.TreeIdentity.parse(current.id), id = _a.id, key = _a.key, namespace = _a.namespace;
var args = {
id: id,
key: key,
namespace: namespace,
node: current,
parent: parentNode_1,
get index() {
var id = current ? current.id : '';
return !parentNode_1
? -1
: (parentNode_1.children || []).findIndex(function (node) { return node.id === id; });
},
level: level,
stop: function () { return (stop_1 = true); },
};
visit(args);
if (!stop_1 && parentNode_1) {
inner(args.parent, visit);
}
}
};
return inner(startAt, visit);
};
this.find = function (match) {
var result;
_this.walkDown(function (e) {
if (match(e) === true) {
result = e.node;
e.stop();
}
});
return result ? result : undefined;
};
this.findById = function (id) {
if (!id) {
return undefined;
}
else {
var target_1 = TreeIdentity_1.TreeIdentity.parse(typeof id === 'string' ? id : id.id);
return _this.find(function (e) {
if (!target_1.namespace && e.key === target_1.key) {
return true;
}
else {
return e.key === target_1.key && e.namespace === target_1.namespace;
}
});
}
};
this.parent = function (node) {
if (!node) {
return undefined;
}
if (typeof node !== 'object') {
var id = node;
node = _this.findById(id);
if (!node) {
throw new Error("Cannot find parent of '".concat(id, "' because that child node was not found."));
}
}
var result;
var target = node;
_this.walkDown(function (e) {
if (TreeQuery.hasChild(e.node, target)) {
result = e.node;
e.stop();
}
});
return result;
};
this.ancestor = function (node, match) {
var result;
_this.walkUp(node, function (e) {
if (match(e)) {
result = e.node;
e.stop();
}
});
return result;
};
this.depth = function (node) {
var depth = -1;
if (!node || !_this.root) {
return depth;
}
else {
var id_1 = (0, common_1.toNodeId)(node);
_this.walkDown(function (e) {
if (e.node.id === id_1) {
depth = e.level;
e.stop();
}
});
return depth;
}
};
this.exists = function (input) {
var node = typeof input === 'function' ? _this.find(input) : _this.findById(input);
return Boolean(node);
};
this.root = args.root;
this.namespace = (args.namespace || '').trim();
}
TreeQuery.create = function (args) {
var input = args;
var isQueryArgs = typeof input.root === 'object';
var root = (isQueryArgs ? input.root : args);
var namespace = isQueryArgs ? input.namespace || '' : '';
return new TreeQuery({ root: root, namespace: namespace });
};
TreeQuery.children = function (of, fn, options) {
if (options === void 0) { options = {}; }
options = (typeof fn === 'object' ? fn : options) || {};
var children = (!of ? [] : of.children || []);
if (options.assign !== false && of && !of.children) {
of.children = children;
}
if (typeof fn === 'function') {
fn(children);
}
return children;
};
TreeQuery.hasChild = function (parent, child) {
var nodes = TreeQuery.children(parent);
var id = (0, common_1.toNodeId)(child);
return nodes.some(function (node) { return node.id === id; });
};
TreeQuery.childAt = function (index, parent) {
return TreeQuery.children(parent)[index];
};
return TreeQuery;
}());
exports.TreeQuery = TreeQuery;