Socket
Socket
Sign inDemoInstall

toposort-class

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

toposort-class

Topological sort of directed acyclic graphs (like dependecy lists)


Version published
Weekly downloads
2M
decreased by-12.01%
Maintainers
1
Weekly downloads
 
Created

What is toposort-class?

The toposort-class npm package is a JavaScript library that provides functionality for topological sorting. Topological sorting is a linear ordering of vertices in a directed graph such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. This is particularly useful in scenarios like resolving dependencies, scheduling tasks, and organizing data with precedence constraints.

What are toposort-class's main functionalities?

Add Edges

This feature allows you to add directed edges to the graph. In the example, 'a' depends on 'b' and 'b' depends on 'c'. The sort method then returns the topologically sorted order.

const Toposort = require('toposort-class');
const ts = new Toposort();
ts.add('a', 'b');
ts.add('b', 'c');
console.log(ts.sort()); // Output: ['a', 'b', 'c']

Add Multiple Edges

This feature allows you to add multiple edges at once. In the example, 'a' depends on both 'b' and 'c', and both 'b' and 'c' depend on 'd'. The sort method returns the topologically sorted order.

const Toposort = require('toposort-class');
const ts = new Toposort();
ts.add('a', ['b', 'c']);
ts.add('b', 'd');
ts.add('c', 'd');
console.log(ts.sort()); // Output: ['a', 'b', 'c', 'd']

Cycle Detection

This feature detects cycles in the graph. In the example, adding edges 'a' -> 'b', 'b' -> 'c', and 'c' -> 'a' creates a cycle. The sort method throws an error indicating the presence of a cycle.

const Toposort = require('toposort-class');
const ts = new Toposort();
ts.add('a', 'b');
ts.add('b', 'c');
ts.add('c', 'a');
try {
  console.log(ts.sort());
} catch (e) {
  console.log(e.message); // Output: 'There is a cycle in the graph. It is not possible to sort.'
}

Other packages similar to toposort-class

Keywords

FAQs

Package last updated on 02 Jul 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc