UNPKG

resolved-graph

Version:

Generates and updates a graph of nodes and links with resolved relationships for ease of traversal and extraction

168 lines (167 loc) 5.78 kB
import { Node } from './Node'; import { Link } from './Link'; import { ResolvedLink } from './ResolvedLink'; import { ResolvedNode } from './ResolvedNode'; import { Graph } from './Graph'; export declare class ResolvedGraph<NodeData = any, LinkData = NodeData> { private _nodes; private _links; /** * Creates a graph where all the nodes and links have references to its connected entitites * @param graph The graph to resolve on creation */ constructor(graph?: Graph<NodeData, LinkData>); /** * An array of all the resolved nodes in the graph */ get nodes(): ResolvedNode<NodeData, LinkData>[]; /** * An array of all the resolved links in the graph */ get links(): ResolvedLink<LinkData, NodeData>[]; private resolveNode; private resolveLink; /** * Merges a simple graph into the existing resolved graph, resolving all links and nodes affected in the process * @param graph The graph to merge into the existing resolved graph */ mergeGraph(graph: Graph<NodeData, LinkData>): void; /** * Either creates a new node or sets a node, deleting any non-vital properties not included in the argument * @param node The node to create or set */ setNode(node: Node<NodeData> | ResolvedNode<NodeData, LinkData>): void; /** * Merges or creates a node, overwriting any properties in the node argument while not changing any other ones * @param node The node to merge */ mergeNode(node: Node<NodeData> | ResolvedNode<NodeData, LinkData>): void; /** * Returns the first node that matches the query object, including nested links and their nodes * @param query Contains properties that must be matched by the node * @example * resolvedGraph.findNode({ data: { answer: 42 } }) * > { id: "Adams", data: { answer: 42, question: "What do you get when you multiply six by nine?" }, from: [...], to: [...]} } * @example * resolvedGraph.findNode({ id: "A", from: [{ to: { id: "B"} }]}) * > { id: "A", from: [ { id: "1", from: [Circular] , to: { id: "B", from: [...], to: [ [Circular] , ...] }}, ...], to: [...] } */ findNode(query: object): ResolvedNode<NodeData, LinkData>; /** * Returns an array of nodes that matches the query object, including nested links and their nodes * @param query Contains properties that must be matched by the nodes * @example * resolvedGraph.findNodes({ data: { type: "Movie" } }) * > [ * { * id: "TMTRX", * data: { * name: "The Matrix" * type: "Movie", * rating: "Breathtaking!" * }, * from: [...], * to: [...] * }, * { * id: "PRDTR", * data: { * name: "Predator" * type: "Movie", * rating: "Auuuagh! Auugh!" * }, * from: [...], * to: [...] * } * ] */ findNodes(query: object): ResolvedNode<NodeData, LinkData>[]; /** * Returns a node with a given id * @param id The id of the node */ node(id: string): ResolvedNode<NodeData, LinkData>; /** * Either creates a new link or sets a link, deleting any non-vital properties not included in the argument * @param link The link to create or set */ setLink(link: Link<LinkData> | ResolvedLink<LinkData, NodeData>): void; /** * Merges or creates a link, overwriting any properties in the link argument while not changing any other ones * @param link The link to merge */ mergeLink(link: Link<LinkData> | ResolvedLink<LinkData, NodeData>): void; /** * Returns the first link that matches the query object, including nested nodes and their links * @param query Contains properties that must be matched by the link * @example * resolvedGraph.findLink({ data: { type: "Acted in" } to: { id: 'TMTRX' }}) * > * { * id: "123", * data: { type: "Acted in" }, * from: { * id: "KNU", * data: { name: "Keanu Reeves" }, * from: [ [Circular], ...], * to: [...] * }, * to: { * id: "TMTRX", * data: { * name: "The Matrix" * type: "Movie", * rating: "Breathtaking!" * }, * from: [...], * to: [ [Circular], ...] * }, * } */ findLink(query: object): ResolvedLink<LinkData, NodeData>; /** * Returns any links that match the query object, including nested nodes and their links * @param query Contains properties that must be matched by the links * @example * resolvedGraph.findLinks({ from: { id: "A" } }) * > * [ * { * id: "AB", * from: { * id: "A", * from: [[Circular], ...], * to: [...] * }, * to: { * id: "B" * from: [[Circular], ...], * to: [...] * } * }, * { * id: "AC", * from: { * id: "A", * from: [[Circular], ...], * to: [...] * }, * to: { * id: "C" * from: [[Circular], ...], * to: [...] * } * } * ] */ findLinks(query: object): ResolvedLink<LinkData, NodeData>[]; /** * Returns a link with a given id * @param id The id of the link */ link(id: string): ResolvedLink<LinkData, NodeData>; /** * Creates an unresolved Graph object out of a the Resolved Graph */ dissolve(): Graph<NodeData, LinkData>; }