npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@baileyherbert/dependency-graph

v1.0.0

Published

A simple and modern dependency graph for TypeScript.

Downloads

9

Readme

Dependency Graph

This package contains a simple and modern dependency graph data structure for TypeScript.

npm install @baileyherbert/dependency-graph


Example

import { DependencyGraph } from '@baileyherbert/dependency-graph';

const graph = new DependencyGraph<string>();

graph.addNode('a');
graph.addNode('b');
graph.addNode('c');

graph.addDependency('a', 'b');
graph.addDependency('b', 'c');

graph.getDependenciesOf('a');  // ['c', 'b']
graph.getDependenciesOf('b');  // ['c']

graph.getDependentsOf('c');    // ['a', 'b']

graph.getOverallOrder();       // ['c', 'b', 'a']
graph.getOverallOrder(true);   // ['c']

graph.addNode('d', 'data');
graph.setNodeData('c', 'more data');

graph.getNodeData('d');        // 'data'
graph.getNodeData('c');        // 'more data'
graph.getNodeData('b');        // undefined

Constructor

new DependencyGraph<T>()
new DependencyGraph<T, D>()
  • <T> is the type to use for nodes.
  • <D> is the type to use for node data. Defaults to any.

Methods

addNode(node, [data])

Adds a node to the dependency graph.

Returns: void
Parameters:

  • node: T – The node to add.
  • data?: D – Optional data to associate with this node.

removeNode(node)

Removes a node from the dependency graph.

Returns: void
Parameters:

  • node: T – The node to remove.

hasNode(node)

Returns true if the specified node exists in the graph.

Returns: boolean
Parameters:

  • node: T – The node to check for.

getNodeData(node)

Returns the data for the specified node or undefined if not available.

Returns: D | undefined
Parameters:

  • node: T – The node to retrieve data for.

setNodeData(node, [data])

Sets the data for the specified node. Setting the data to undefined will remove it.

Returns: void
Parameters:

  • node: T – The node to set data for.
  • data?: D – Optional data to associate with this node.

hasNodeData(node)

Returns true if the node has associated data.

Returns: boolean
Parameters:

  • node: T – The node to check for.

addDependency(from, to)

Registers that the from node depends on the to node.

Returns: void
Parameters:

  • from: T – The dependent node.
  • to: T – The dependency node.

removeDependency(from, to)

Removes a dependency between two nodes. The given from node will no longer be dependent upon the to node.

Returns: void
Parameters:

  • from: T – The dependent node.
  • to: T – The dependency node.

getDirectDependenciesOf(node)

Returns an array containing all direct dependencies of the given node.

Returns: T[]
Parameters:

  • node: T – The node to check for.

getDirectDependentsOf(node)

Returns an array containing the nodes that directly depend on the given node.

Returns: T[]
Parameters:

  • node: T – The node to check for.

getDependenciesOf(node, [leaves])

Returns an array containing the nodes that the specified node depends on (transitively).

Returns: T[]
Parameters:

  • node: T – The node to check for.
  • leaves: boolean – Only return leaves. Defaults to false.

getDependentsOf(node, [leaves])

Returns an array containing the nodes that depend on the specified node (transitively).

Returns: T[]
Parameters:

  • node: T – The node to check for.
  • leaves: boolean – Only return leaves. Defaults to false.

getOverallOrder([leaves])

Computes the overall processing order for the dependency graph. Throws an error if circular dependencies are detected.

Returns: T[]
Parameters:

  • leaves: boolean – Only return leaves. Defaults to false.

getEntryNodes()

Returns an array of nodes that have no dependents.

Returns: T[]

clone()

Creates a copy of the dependency graph.

Returns: DependencyGraph<T, D>

clear()

Clears all nodes and data from the dependency graph.

Returns: void

Properties

allowCircularDependencies

Whether or not circular dependencies are allowed. Defaults to false in which case errors will be thrown upon their detection.

Type: boolean
Default: false

size

The number of nodes in the graph.

Type: number (read-only)


Circular dependencies

When circular dependencies occur and allowCircularDependencies is false, an error will be thrown. You can catch this error and use the information within it to determine which nodes are responsible.

import { CircularDependencyError } from '@baileyherbert/dependency-graph';

try {
	// Implement graph with a circular dependency
	graph.addDependency('a', 'b');
	graph.addDependency('b', 'c');
	graph.addDependency('c', 'a');

	// The error will be thrown when running computations
	graph.getOverallOrder();
}
catch (error) {
	if (error instanceof CircularDependencyError) {
		error.path;     // ['a', 'b', 'c', 'a']
		error.node;     // 'a'
		error.message;  // Detected circular dependencies (a -> b -> c -> a)
	}
}