8000 make label types generic by SorenHolstHansen · Pull Request #211 · dagrejs/graphlib · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

make label types generic #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Definitions by: Dan Vanderkam <http://danvk.org/>, Dan Mironenko <wolfson@bracketedrebels.com>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

declare module '@dagrejs/graphlib' {
declare module "@dagrejs/graphlib" {
export interface GraphOptions {
directed?: boolean; // default: true.
multigraph?: boolean; // default: false.
Expand All @@ -17,7 +17,7 @@ declare module '@dagrejs/graphlib' {
name?: string;
}

export class Graph {
export class Graph<GraphLabel = any, NodeLabel = any, EdgeLabel = any> {
constructor(options?: GraphOptions);

/**
Expand All @@ -28,7 +28,7 @@ declare module '@dagrejs/graphlib' {
* @argument label - default node label.
* @returns the graph, allowing this to be chained with other functions.
*/
setDefaultNodeLabel(label: any): Graph;
setDefaultNodeLabel(label: NodeLabel): this;

/**
* Sets the default node label factory function. This function will be invoked
Expand All @@ -39,7 +39,7 @@ declare module '@dagrejs/graphlib' {
* @argument labelFn - default node label factory function.
* @returns the graph, allowing this to be chained with other functions.
*/
setDefaultNodeLabel(labelFn: (v: string) => any): Graph;
setDefaultNodeLabel(labelFn: (v: string) => NodeLabel): this;

/**
* Creates or updates the value for the node v in the graph. If label is supplied
Expand All @@ -51,7 +51,7 @@ declare module '@dagrejs/graphlib' {
* @argument label - value to set for node.
* @returns the graph, allowing this to be chained with other functions.
*/
setNode(name: string, label?: any): Graph;
setNode(name: string, label?: NodeLabel): this;

/**
* Invokes setNode method for each node in names list.
Expand All @@ -61,7 +61,7 @@ declare module '@dagrejs/graphlib' {
* @argument label - value to set for each node in list.
* @returns the graph, allowing this to be chained with other functions.
*/
setNodes(names: string[], label?: any): Graph;
setNodes(names: string[], label?: NodeLabel): this;

/**
* Sets node p as a parent for node v if it is defined, or removes the
Expand All @@ -73,7 +73,7 @@ declare module '@dagrejs/graphlib' {
* @argument p - node to be parent for v.
* @returns the graph, allowing this to be chained with other functions.
*/
setParent(v: string, p?: string): Graph;
setParent(v: string, p?: string): this;

/**
* Gets parent node for node v.
Expand Down Expand Up @@ -102,7 +102,7 @@ declare module '@dagrejs/graphlib' {
* @argument filter - filtration function detecting whether the node should stay or not.
* @returns new graph made from current and nodes filtered.
*/
filterNodes(filter: (v: string) => boolean): Graph;
filterNodes(filter: (v: string) => boolean): this;

/**
* Sets the default edge label. This label will be assigned as default label
Expand All @@ -112,7 +112,7 @@ declare module '@dagrejs/graphlib' {
* @argument label - default edge label.
* @returns the graph, allowing this to be chained with other functions.
*/
setDefaultEdgeLabel(label: any): Graph;
setDefaultEdgeLabel(label: EdgeLabel): this;

/**
* Sets the default edge label factory function. This function will be invoked
Expand All @@ -123,7 +123,7 @@ declare module '@dagrejs/graphlib' {
* @argument labelFn - default edge label factory function.
* @returns the graph, allowing this to be chained with other functions.
*/
setDefaultEdgeLabel(labelFn: (v: string) => any): Graph;
setDefaultEdgeLabel(labelFn: (v: string) => EdgeLabel): this;

/**
* Establish an edges path over the nodes in nodes list. If some edge is already
Expand All @@ -135,7 +135,7 @@ declare module '@dagrejs/graphlib' {
* @argument label - value to set for each edge between pairs of nodes.
* @returns the graph, allowing this to be chained with other functions.
*/
setPath(nodes: string[], label?: any): Graph;
setPath(nodes: string[], label?: EdgeLabel): this;

/**
* Detects whether graph has a node with specified name or not.
Expand All @@ -155,7 +155,7 @@ declare module '@dagrejs/graphlib' {
* @argument name - name of the node.
* @returns the graph, allowing this to be chained with other functions.
*/
removeNode(name: string): Graph;
removeNode(name: string): this;

/**
* Gets all nodes of the graph. Note, the in case of compound graph subnodes are
Expand All @@ -172,7 +172,7 @@ declare module '@dagrejs/graphlib' {
*
* @returns label value of the node.
*/
node(name: string): any;
node(name: string): NodeLabel;

/**
* Creates or updates the label for the edge (v, w) with the optionally supplied
Expand All @@ -187,7 +187,7 @@ declare module '@dagrejs/graphlib' {
* @argument name - unique name of the edge in order to identify it in multigraph.
* @returns the graph, allowing this to be chained with other functions.
*/
setEdge(v: string, w: string, label?: any, name?: string): Graph;
setEdge(v: 10000 string, w: string, label?: EdgeLabel, name?: string): this;

/**
* Creates or updates the label for the specified edge. If label is supplied it is
Expand All @@ -200,7 +200,7 @@ declare module '@dagrejs/graphlib' {
* @argument label - value to associate with the edge.
* @returns the graph, allowing this to be chained with other functions.
*/
setEdge(edge: Edge, label?: any): Graph;
setEdge(edge: Edge, label?: EdgeLabel): this;

/**
* Gets edges of the graph. In case of compound graph subgraphs are not considered.
Expand All @@ -219,7 +219,7 @@ declare module '@dagrejs/graphlib' {
* @argument name - name of the edge (actual for multigraph).
* @returns value associated with specified edge.
*/
edge(v: string, w: string, name?: string): any;
edge(v: string, w: string, name?: string): EdgeLabel;

/**
* Gets the label for the specified edge.
Expand All @@ -228,7 +228,7 @@ declare module '@dagrejs/graphlib' {
* @argument edge - edge descriptor.
* @returns value associated with specified edge.
*/
edge(e: Edge): any;
edge(e: Edge): EdgeLabel;

/**
* Gets the label for the specified edge and converts it to an object.
Expand Down Expand Up @@ -277,7 +277,7 @@ declare module '@dagrejs/graphlib' {
* @argument edge - edge descriptor.
* @returns the graph, allowing this to be chained with other functions.
*/
removeEdge(edge: Edge): Graph;
removeEdge(edge: Edge): this;

/**
* Removes the specified edge from the graph. No subgraphs are considered.
Expand All @@ -288,7 +288,7 @@ declare module '@dagrejs/graphlib' {
* @argument name - name of the edge (actual for multigraph).
* @returns the graph, allowing this to be chained with other functions.
*/
removeEdge(v: string, w: string, name?: string): Graph;
removeEdge(v: string, w: string, name?: string): this;

/**
* Return all edges that point to the node v. Optionally filters those edges down to just those
Expand Down Expand Up @@ -381,14 +381,14 @@ declare module '@dagrejs/graphlib' {
* @argument label - label value.
* @returns the graph, allowing this to be chained with other functions.
*/
setGraph(label: any): Graph;
setGraph(label: GraphLabel): this;

/**
* Gets the graph label.
*
* @returns currently assigned label for the graph or undefined if no label assigned.
*/
graph(): any;
graph(): GraphLabel;

/**
* Gets the number of nodes in the graph.
Expand Down Expand Up @@ -446,7 +446,9 @@ declare module '@dagrejs/graphlib' {
* @argument json - JSON serializable graph representation
* @returns graph constructed acccording to specified representation
*/
function read(json: Object): Graph;
function read<GraphLabel = any, NodeLabel = any, EdgeLabel = any>(
json: Object,
): Graph<GraphLabel, NodeLabel, EdgeLabel>;
}

export interface Path {
Expand Down Expand Up @@ -487,7 +489,7 @@ declare module '@dagrejs/graphlib' {
graph: Graph,
source: string,
weightFn?: (e: Edge) => number,
edgeFn?: (v: string) => Edge[]
edgeFn?: (v: string) => Edge[],
): { [node: string]: Path };

/**
Expand All @@ -507,7 +509,7 @@ declare module '@dagrejs/graphlib' {
function dijkstraAll(
graph: Graph,
weightFn?: (e: Edge) => number,
edgeFn?: (v: string) => Edge[]
edgeFn?: (v: string) => Edge[],
): { [source: string]: { [node: string]: Path } };

/**
Expand Down Expand Up @@ -555,7 +557,7 @@ declare module '@dagrejs/graphlib' {
function floydWarshall(
graph: Graph,
weightFn?: (e: Edge) => number,
edgeFn?: (v: string) => Edge[]
edgeFn?: (v: string) => Edge[],
): { [source: string]: { [node: string]: Path } };

/**
Expand Down
0