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 - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

11

index.js

@@ -32,10 +32,11 @@ /**

if (deps) {
deps = Array.isArray(deps) ? deps : [deps];
deps.forEach(function(dep) {
var tmp = Array.isArray(deps) ? deps : [deps];
tmp.forEach(function(dep) {
if (typeof dep !== "string" || !dep) {
throw new TypeError("Dependency name must be given as a not empty string");
}
edges.push([item, dep]);
});
tmp.unshift(item);
edges.push(tmp);
} else {

@@ -105,2 +106,2 @@ edges.push([item]);

module.exports = Toposort;
module.exports = exports.Toposort = Toposort;
{
"name": "toposort-class",
"version": "0.1.0",
"version": "0.1.1",
"description": "Topological sort of directed acyclic graphs (like dependecy lists)",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,2 +0,4 @@

# Sorting directed acyclic graphs
# Toposort
__Sorting directed acyclic graphs__
_This was originally done by Marcel Klehr. [Why not checkout his original repo?](https://github.com/marcelklehr/node-toposort)_

@@ -7,64 +9,54 @@ ## Installation

## Example
Let's say, you have a list of pluginsor tasks, which depend on each other (`depends` defines plugins or tasks that should be executed before the plugin that declares the directive):
Let's say you have the following dependency graph:
* Plugin depends on Backbone and jQuery UI Button;
* Backbone depends on jQuery and Underscore;
* jQuery UI Button depends on jQuery UI Core and jQuery UI Widget;
* jQuery UI Widget and jQuery UI Core depend on jQuery;
* jQuery and Underscore don't depend on anyone.
```
var plugins =
[ {name: "foo", depends: ['bar']}
, {name: "bar", depends: ["ron"]}
, {name: "john", depends: ["bar"]}
, {name: "tom", depends: ["john"]}
, {name: "ron", depends: []}
]
```
Now, how would you sort this in a way that each asset will be correctly placed? You'll probably need the following sorting:
* `jQuery`, `jQuery UI Core`, `jQuery UI Widget`, `jQuery UI Button`, `Underscore`, `Backbone`, `Plugin`
A quick analysis, will result in the following dependency tree:
You can achieve it with the following code, using `toposort-class`:
```javascript
var Toposort = require('./index'),
t = new Toposort();
t.add("jquery-ui-core", "jquery")
.add("jquery-ui-widget", "jquery")
.add("jquery-ui-button", ["jquery-ui-core", "jquery-ui-widget"])
.add("plugin", ["backbone", "jquery-ui-button"])
.add("backbone", ["underscore", "jquery"]);
console.log(t.sort().reverse());
/* Will output:
* ['jquery', 'jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-button', 'underscore', 'backbone', 'plugin']
*
* And you're done.
*/
```
tom
|
john foo
| |
- - - -
|
bar
|
ron
```
and thus the following execution flow:
## API
First of all:
```javascript
var Toposort = require('toposort-class'),
t = new Toposort();
// If you prefer, you can do this way also:
t = new require('toposort-class').Toposort();
```
ron
|
bar
- - - -
| |
john foo
|
tom
```
Let's try this with `toposort`:
### .add(item, deps)
* _{String}_ `item` - The name of the dependent item that is being added
* _{Array|String}_ `deps` - A dependency or list of dependencies of `item`
```js
var toposort = require('toposort-class');
__Returns:__ _{Toposort}_ The Toposort instance, for chaining.
// this will sort our plugins by dependecy
var toposort = new Toposort();
toposort.add("foo", "bar");
toposort.add("bar", "ron");
toposort.add("john", "bar");
toposort.add("tom", "john");
### .sort()
__Returns:__ _{Array}_ The list of dependencies topologically sorted.
// now, we reverse the results to get the resulting execution flow, as above
var results = toposort.sort();
This method will check for cyclic dependencies, like "A is dependent of A".
console.log(results)
/*
Output:
[ 'ron', 'bar', 'foo', 'john', 'tom' ]
*/
```
## Legal
MIT License
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