BE-SPAN/node_modules/toposort-class
githubna-ilham 722cd440e8 first commit
2024-10-20 22:04:16 +07:00
..
test first commit 2024-10-20 22:04:16 +07:00
.gitattributes first commit 2024-10-20 22:04:16 +07:00
.jscsrc first commit 2024-10-20 22:04:16 +07:00
.jshintrc first commit 2024-10-20 22:04:16 +07:00
.npmignore first commit 2024-10-20 22:04:16 +07:00
.travis.yml first commit 2024-10-20 22:04:16 +07:00
bower.json first commit 2024-10-20 22:04:16 +07:00
Gruntfile.js first commit 2024-10-20 22:04:16 +07:00
package.json first commit 2024-10-20 22:04:16 +07:00
README.md first commit 2024-10-20 22:04:16 +07:00
toposort.js first commit 2024-10-20 22:04:16 +07:00

Toposort Build Status Dependency Status

Sorting directed acyclic graphs, for Node.js and the browser This was originally done by Marcel Klehr. Why not checkout his original repo?

Installation

There are a few ways for installing Toposort. Here are them:

  • Via NPM: npm install toposort-class
  • Via Bower: bower install toposort
  • Via Git: git clone git://github.com/gustavohenke/toposort.git
  • Direct download for use in the browser

Example

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.

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

You can achieve it with the following code, using toposort-class:

var Toposort = require('toposort-class'),
	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.
 */

API

First of all:

var Toposort = require('toposort-class'),
	t = new Toposort();

// If you prefer, you can do this way also:
t = new require('toposort-class').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

Returns: {Toposort} The Toposort instance, for chaining.

.sort()

Returns: {Array} The list of dependencies topologically sorted.

This method will check for cyclic dependencies, like "A is dependent of A".

MIT License