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 🙏

© 2026 – Pkg Stats / Ryan Hefner

grapherx

v0.1.1

Published

TypeScript library for working with directed graphs

Readme

GrapherX

GrapherX is a small TypeScript library for working with directed graphs with caller-provided vertex identifiers.

Graphs are stored in a map-backed adjacency structure. Callers work with VertexId keys and receive readonly snapshots instead of mutable internal vertices.

import { Graph } from 'grapherx';

const graph = new Graph<string>();

graph.addVertex(0, 'load data');
graph.addVertex(1, 'match treatments');
graph.addVertex(2, 'pull results');

graph.addEdge(0, 1);
graph.addEdge(1, 2);

graph.getAdjacent(0);
// [1]

graph.breadthFirstSearch(0);
// [0, 1, 2]

graph.depthFirstSearch(1);
// [1, 2]

graph.findShortestPath(0, 2);
// [0, 1, 2]

Model

Graph<T> is dynamically sized. Vertices are added individually with addVertex(id, value) where id is a caller-provided VertexId (string | number). The constructor takes no arguments.

The graph is directed. It can represent a directed acyclic graph, and it can detect cycles. Self-loops (an edge from a vertex to itself) are rejected at insertion time; multi-vertex cyclic edges are not prevented.

Public graph observations use snapshots:

type VertexId = string | number;

type VertexSnapshot<T> = Readonly<{
  id: VertexId;
  value: T;
}>;

addVertex throws VertexAlreadyExistsError on a duplicate ID. updateVertex, removeVertex, and getAdjacent throw VertexNotFoundError when the ID does not exist. addEdge throws VertexNotFoundError when either endpoint does not exist, throws EdgeAlreadyExistsError on a duplicate edge, and throws SelfLoopError when source and target are the same vertex. removeEdge throws VertexNotFoundError for a missing endpoint and EdgeNotFoundError when the edge does not exist. topologicalSort throws CycleError when the graph contains a cycle. All six error classes are exported and catchable via instanceof.

API Reference

Construction

const graph = new Graph<T>()

No arguments. T is the type of vertex values.

Vertex methods

addVertex(id: VertexId, value: T): void
updateVertex(id: VertexId, value: T): void
getVertex(id: VertexId): VertexSnapshot<T> | undefined
removeVertex(id: VertexId): void
get vertexCount(): number

Edge methods

addEdge(sourceId: VertexId, targetId: VertexId): void
removeEdge(sourceId: VertexId, targetId: VertexId): void
getAdjacent(id: VertexId): VertexId[]
get edgeCount(): number

Traversal and analysis

breadthFirstSearch(startId: VertexId): VertexId[]
depthFirstSearch(startId: VertexId): VertexId[]
findShortestPath(sourceId: VertexId, targetId: VertexId): VertexId[] | undefined
detectCycle(): boolean
topologicalSort(): VertexId[]

findShortestPath returns undefined when the target is unreachable and when either vertex does not exist. It does not throw VertexNotFoundError. To distinguish a missing vertex from an unreachable target, call getVertex(id) first.

Error classes

All error classes extend Error, are exported, and are catchable via instanceof.

| Class | Thrown by | Condition | |---|---|---| | VertexAlreadyExistsError | addVertex | ID already exists | | VertexNotFoundError | updateVertex, removeVertex, addEdge, removeEdge, getAdjacent | ID not found | | EdgeAlreadyExistsError | addEdge | Edge already exists | | EdgeNotFoundError | removeEdge | Edge not found | | SelfLoopError | addEdge | Source and target are the same vertex | | CycleError | topologicalSort | Graph contains a cycle |

Features and Complexity

V is the number of vertices. E is the number of edges. out(v) is the number of outgoing edges from a vertex.

  • [x] Add vertex: O(1)
  • [x] Update vertex value: O(1)
  • [x] Add edge: O(1)
  • [x] Get vertex snapshot: O(1)
  • [x] Get adjacent vertex IDs: O(out(v))
  • [x] Edge count: O(1)
  • [x] Remove vertex: O(V + out(v))
  • [x] Remove edge: O(1)
  • [x] Breadth-first search: O(V + E)
  • [x] Depth-first search: O(V + E)
  • [x] Detect cycles: O(V + E)
  • [x] Find shortest path in an unweighted graph: O(V + E)
  • [x] Topological sort (DAG): O(V + E)

Breaking Changes

breadthFirstSearch() and depthFirstSearch() now require a startId: VertexId argument. The previous zero-argument form has been removed.

// Before
graph.breadthFirstSearch()
// After
graph.breadthFirstSearch(startId)

sortTopologically() has been renamed to topologicalSort() and now throws CycleError instead of returning an empty array when the graph contains a cycle.

// Before
graph.sortTopologically() // returned [] on cyclic graphs
// After
graph.topologicalSort() // throws CycleError on cyclic graphs

findShortestPath(sourceId, targetId) now returns VertexId[] | undefined (the full vertex path) instead of number (edge count). Returns undefined when the target is unreachable or when either vertex does not exist. The previous sentinel value -1 is gone.

// Before
graph.findShortestPath(0, 2) // 2
// After
graph.findShortestPath(0, 2) // [0, 1, 2]

depthFirstTraversal(startId) has been removed. Use depthFirstSearch(startId) instead, which returns VertexId[] in the same DFS order.

checkPath(sourceId, targetId) has been removed. Use findShortestPath(sourceId, targetId) !== undefined instead.

findMotherVertex() has been removed with no direct replacement.

mapGraphOver() has been removed. To inspect the adjacency structure, iterate vertex IDs and call getAdjacent(id) for each.

LinkedList is no longer exported from the package. It is an internal data structure.

Current Limitations

  • Multi-vertex cycles are allowed at insertion time; use detectCycle() to check for them. Self-loops are rejected by addEdge with SelfLoopError.

Get Started

Prerequisites:

  • Node 16+
  • npm 5+
  • git
git clone https://github.com/rejth/grapherx.git
cd grapherx
npm install

Scripts

npm run build

Builds the library into the dist folder.

npm run test

Runs the Jest test suite.

npm run declarations

Generates TypeScript declaration files.