@rimbu/graph
Version:
Immutable Graph data structures for TypeScript
69 lines (68 loc) • 2.64 kB
text/typescript
/**
* A potentially valued connection between two nodes,
* being a 2-valued or 3-valued tuple of which the
* first two elements are nodes
* @typeparam N - the node type
*/
export type Link<N> = [N, N] | [N, N, unknown];
export declare namespace Link {
type Target<N> = [N] | readonly [N, unknown];
/**
* Returns the given potentially valued link `link` as a 2-tuple
* @param link - the link to convert
*/
function toTuple<N>(link: Link<N>): [N, N];
/**
* 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<N>(node1: N, node2: N): Link<N>;
/**
* Returns a graph `Link` from the given 2-tuple `tuple`.
* @param tuple - the tuple to convert
*/
function fromTuple<N>(tuple: [N, N]): Link<N>;
}
/**
* A graph element is either an isolated node as a 1-tuple, or
* a link between nodes represented as a `Link` instance.
*/
export type GraphElement<N> = [N] | Link<N>;
export declare namespace 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: GraphElement<any>): e is [unknown];
/**
* 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: GraphElement<any>): e is [unknown, unknown];
/**
* Returns the value of a single node graph element if the given element `e` is a single
* node, or the given `otherwise` fallback value otherwise.
* @param e - the graph element
* @param otherwise - (default: undefined) the fallback value to return
* if the given element is not a single node
*/
function getSingleNode<N>(e: GraphElement<N>): N | undefined;
/**
* 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<N>(e: GraphElement<N>): Link<N> | undefined;
/**
* Returns the element at the given `key` in the graph element `e`,
* if the element is a Link element, or returns the given `otherwise` value
* otherwise.
* @param e - the graph element
* @param key - the link key
* @param otherwise - (default: undefined) the fallback value
*/
function getLinkElement<N>(e: GraphElement<N>, key: 0 | 1): N | undefined;
}