# toposort-class

> Topological sort of directed acyclic graphs (like dependecy lists)

Latest version **1.0.1** (published 2015-08-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install toposort-class
pnpm add toposort-class
yarn add toposort-class
bun add toposort-class
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2015-08-13 |
| First published | 2013-01-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 23 |
| Maintainers | gustavohenke |
| Keywords | topological, sort, sorting, graphs, graph, dependency, list, dependencies, acyclic, browser |

## Links

- npm: https://www.npmjs.com/package/toposort-class
- Repository: https://github.com/gustavohenke/toposort
- Homepage: https://github.com/gustavohenke/toposort#readme
- Issues: https://github.com/gustavohenke/toposort/issues
- npm.io page: https://npm.io/package/toposort-class

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2015-08-13
- 1.0.0 (beta) — 2015-07-31
- 0.3.1 — 2014-04-29
- 0.3.0 — 2014-01-20
- 0.2.1 — 2013-07-23
- 0.2.0 — 2013-07-02
- 0.1.4 — 2013-03-29
- 0.1.3 — 2013-02-05
- 0.1.2 — 2013-01-17
- 0.1.1 — 2013-01-17
- 0.1.0 — 2013-01-03

## README

# Toposort
[![Build Status](http://img.shields.io/travis/gustavohenke/toposort.svg?branch=master&style=flat)](https://travis-ci.org/gustavohenke/toposort)
[![Dependency Status](http://img.shields.io/gemnasium/gustavohenke/toposort.png?style=flat)](https://gemnasium.com/gustavohenke/toposort)

__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?](https://github.com/marcelklehr/toposort)_

## 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](https://raw.githubusercontent.com/gustavohenke/toposort/master/build/toposort.js) ([Minified](https://raw.githubusercontent.com/gustavohenke/toposort/master/build/toposort.min.js)) 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`:
```javascript
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):
```javascript
var Toposort = require('toposort-class'),
	t = new Toposort();
```

Browser with AMD:
```javascript
define("myModule", ["Toposort"], function(Toposort) {
    var t = new Toposort();
});
```

Browser without AMD:
```javascript
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

---
_Source: https://npm.io/package/toposort-class · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
