@rimbu/graph
Version:
Immutable Graph data structures for TypeScript
76 lines • 2.27 kB
JavaScript
import { OptLazy } from '@rimbu/common';
export 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 || (Link = {}));
export 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 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 OptLazy(otherwise);
}
GraphElement.getLinkElement = getLinkElement;
})(GraphElement || (GraphElement = {}));
//# sourceMappingURL=link.mjs.map