Code Monkey home page Code Monkey logo

toposort's Introduction

Toposort

Build Status Dependency Status

Sorting directed acyclic graphs, for Node.js, io.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 (Minified) 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.
 */

Usage

CommonJS (Node.js and io.js):

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

Browser with AMD:

define("myModule", ["Toposort"], function(Toposort) {
    var t = new Toposort();
});

Browser without AMD:

var t = new window.Toposort();

or whatever global object there is instead of window.

API

.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".

.clear()

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

Clears all edges, effectively resetting the instance.

.Toposort

Reference to the Toposort constructor.

Legal

MIT License

toposort's People

Contributors

marcelklehr avatar novacrazy avatar rsternagel avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.