@rimbu/graph
Version:
Immutable Graph data structures for TypeScript
79 lines • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphElement = exports.Link = void 0;
var common_1 = require("@rimbu/common");
var Link;
(function (Link) {
/**
* Returns the given potentially valued link `link` as a 2-tuple
* @param link - the link to convert
*/
function toTuple(link) {
if (link.length === 2)
return link;
return [link[0], link[1]];
}
Link.toTuple = toTuple;
/**
* Returns a graph `Link` from the given `node1` and `node2` nodes.
* @param node1 - the first connection node
* @param node2 - the second connection node
*/
function fromArgs(node1, node2) {
return [node1, node2];
}
Link.fromArgs = fromArgs;
/**
* Returns a graph `Link` from the given 2-tuple `tuple`.
* @param tuple - the tuple to convert
*/
function fromTuple(tuple) {
return tuple;
}
Link.fromTuple = fromTuple;
})(Link || (exports.Link = Link = {}));
var GraphElement;
(function (GraphElement) {
/**
* Returns true if the given graph element `e` is a single node.
* Instructs the compiler that the type is a 1-tuple.
* @param e - the graph element
*/
function isSingleNode(e) {
return e.length === 1;
}
GraphElement.isSingleNode = isSingleNode;
/**
* Returns true if the given graph element `e` is a 2-tuple.
* Instructs the compiler that the type is a 2-tuple.
* @param e - the graph element
*/
function isLink(e) {
return e.length !== 1;
}
GraphElement.isLink = isLink;
function getSingleNode(e, otherwise) {
if (isSingleNode(e))
return e[0];
return (0, common_1.OptLazy)(otherwise);
}
GraphElement.getSingleNode = getSingleNode;
/**
* Returns the values of the link graph element if the given element `e` is
* a Link element, or undefined otherwise.
* @param e - the graph element
*/
function getLink(e) {
if (isLink(e))
return e;
return undefined;
}
GraphElement.getLink = getLink;
function getLinkElement(e, key, otherwise) {
if (isLink(e))
return e[key];
return (0, common_1.OptLazy)(otherwise);
}
GraphElement.getLinkElement = getLinkElement;
})(GraphElement || (exports.GraphElement = GraphElement = {}));
//# sourceMappingURL=link.cjs.map