Skip to content

v4.0.0

Compare
Choose a tag to compare
@curran curran released this 12 Sep 10:27
· 11 commits to master since this release

Many thanks to @JesusTheHun for massively refactoring the library and API in this major version!

The following details come from #81:

Breaking changes :

  • Graph is now a class, so it is instantiated with new Graph() instead of Graph().
  • Graph.adjacent() now returns a Set<Node> or undefined if the node is not found.
  • Graph.nodes() is no longer available. A property nodes: Set<Node> is now exposed.
  • Graph.serialize() is no longer available. A standalone function serializeGraph is available instead.
  • Graph.deserialize() is no longer available. A standalone function deserializeGraph is available instead.
  • Graph.hasCycle() is no longer available. A standalone function hasCycle is available instead.
  • All algorithm methods (i.e. graph.topologicalSort() are no longer available. A standalone function is available for each of them.
  • The shortestPath algorithm now returns an object { nodes: Node[], weight: number } instead of an augmented array.

Internal changes :

  • The project now uses a composition pattern. Closes #18
  • Native data structure Map and Set are used instead of records and arrays. Closes #27
  • Nodes are now references by their instance instead of their id property.
  • The tests have been migrated to vitest in order to be able to write tests in TypeScript without troubles.
  • Additional tests for types input and output have been written.
  • The library is now bundled using rollup. The current solution does not support the export of types coming from type-only files. In addition, both .d.cts and .d.mts declaration files are now distributed.

Features :

  • Nodes can now be anything, not just string. Closes #51, closes #38
  • New functions getNode, getFirstNode, findNodes to help you retrieve node references when they are objects.
  • Edge can now have properties. Closes #80
  • Algorithm depthFirstSearch now accepts a function shouldFollow as an option, to conditionally follow an edge.
  • Graph now accept 2 generics to define the type of the nodes and edge properties.
  • The graph types are inferred from deserializeGraph() and the serialization type carries the types with it.
const g = new Graph<{ id: string }, { type: string }>();

// Good ✅
g.addNode({ id: "abcdef" });
g.setEdgeProperties(source, target, { type: 'foo' });

// Bad ❌ TS Error
g.addNode({ id: 1 });
g.setEdgeProperties(source, target, { label: 'wrong prop name' });

const node = g.nodes();
//     ^? { id: string }[]

const props = g.getEdgeProperties(source, target);
//     ^? { type: string }

Migration

Serialization

-const serialized = Graph().serialize();
+const serialized = serializeGraph(graph);

-const graph = Graph().deserialize(serialized);
+const graph = deserializeGraph(serialized);

Algorithms

-const sorted = graph.topologicalSort();
+const sorted = topologicalSort(graph);

-const path = graph.shortestPath();
-const nodes = path;
-const weight = path.weight;
+const { nodes, weight } = shortestPath(graph);

What's Changed

New Contributors

Full Changelog: v3.5.0...v4.0.0