toposort
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Toposort is an incredibly useful npm package that performs a topological sort on directed acyclic graphs. This makes Toposort perfect for sorting things like dependency lists. By arranging nodes in such a way that every edge is directed from an earlier node to a later one, it provides a legal execution order, which is particularly valuable in scenarios such as package and task dependencies, order processing, data serialization, and more.
To use Toposort, you'll first need to install it using npm by running the command npm install toposort
. Once installed, you can require it at the beginning of your JavaScript file like so:
var toposort = require('toposort');
After that, you can use it to sort your graph. For example, consider you have the following tasks and their dependencies:
var graph = [
['put on your shoes', 'tie your shoes'],
['put on your shirt', 'put on your jacket'],
['put on your shorts', 'put on your jacket'],
['put on your shorts', 'put on your shoes']
]
You sort these tasks using Toposort like so:
toposort(graph)
The above code will provide an output like this, which is the order that the tasks should be executed:
[
'put on your shirt',
'put on your shorts',
'put on your jacket',
'put on your shoes',
'tie your shoes'
]
For sorting dependencies, you can use the toposort.array(nodes, edges)
method:
var graph = [
['tie your shoes', 'put on your shoes'],
['put on your jacket', 'put on your shirt'],
['put on your shoes', 'put on your shorts'],
['put on your jacket', 'put on your shorts']
]
toposort(graph).reverse()
The output will be a legal execution order based on the dependencies defined.
Documentation for Toposort and its API is available directly in the README file at its GitHub repository. The URL for the repository is git+https://github.com/marcelklehr/toposort.git. The README provides a thorough guide on how to install and use Toposort, its methods, usage examples, and more. Learn and get started with Toposort, the powerful tool for sorting directed acyclic graphs in a snap!