# graphs-tob

> Intuitive data structure for graphs

Latest version **2.0.0** (published 2018-02-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install graphs-tob
pnpm add graphs-tob
yarn add graphs-tob
bun add graphs-tob
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2018-02-05 |
| First published | 2018-02-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Maintainers | timoxley |
| Keywords | graph, data, structure |

## Links

- npm: https://www.npmjs.com/package/graphs-tob
- Repository: https://github.com/timoxley/graphs
- Issues: https://github.com/timoxley/graphs/issues
- npm.io page: https://npm.io/package/graphs-tob

## Dependencies (5)

- [sliced](https://npm.io/package/sliced.md) 0.0.5
- [afterfn](https://npm.io/package/afterfn.md) ^2.0.0
- [guardfn](https://npm.io/package/guardfn.md) ^1.0.0
- [beforefn](https://npm.io/package/beforefn.md) ^2.2.0
- [inherits](https://npm.io/package/inherits.md) ^2.0.1

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2018-02-05

## README

# graphs

An intuitive data structure for graphs, implemented using ES6 data structures.

```js
var Graph = require('graphs')
var graph = new Graph()
var a = {name: 'a'}
var b = {name: 'b'}
graph.add(a)
graph.add(b)
graph.link(a, b)
graph.traverse(function(from, to) {
  console.log(from.name, 'linked to', to.name)
})
// => a linked to b
```

## Examples

### Adding Nodes

```js
var graph = new Graph()
var a = {name: 'a'}

graph.add(a)
graph.has(a) // => true
graph.size // => 1

var b = {name: 'b'}
graph.has(b) // => false
graph.size // => 2

graph.add(b)
graph.has(b) // => true
```

### Linking Nodes

```js
graph.link(a, b)
```

Linking will also add nodes to the graph.

### Getting Links to a Node

`.to` and `.from` return ES6 Sets of connected nodes.

```js
graph.link(a, b)

graph.from(a) // Set of nodes connected from a
graph.from(a).size // => 1
graph.from(a).has(b) // => true


graph.to(b) // Set of nodes connected to b
graph.to(b).has(a) // => true
graph.from(b).size // => 0
```

### Unlinking Nodes

```js
graph.unlink(a, b)
graph.from(a).size // => 0
```

### Deleting Nodes

* Also removes any links (but not linked nodes).

```js
graph.delete(b)
```

### Iterating over all Nodes

* `.forEach` will even include entirely unlinked nodes.

```js
graph.forEach(function(node) {
  console.log('node: %s', node.name)
})

```

### Traversing the Graph

`graph.traverse` will traverse all links from the specified node.

* Arguments to the callback are `from, to`
* Starts at a node and follows links.
* May visit a node multiple times (depending on how many times it's linked to).
* The callback will always fire with valid `from` and `to` parameters.
* If startNode is not linked to anything, callback will not fire.
* Will not follow cycles.

```js
graph.traverse(startNode, function(from, to) {
  console.log('from: %s', from)
  console.log('to: %s', to)
})
```

### Visiting Linked Nodes

`graph.visit` will visit each node that can be reached from the specified node, once.

* Arguments to the callback are `to, from`
* Will follow links but will not visit any node more than once.
* The `from` argument may not be set if `visit` didn't follow a link to
the current node (e.g. on the first iteration).

```js
graph.visit(startNode, function(node, linkedFrom) {
  console.log('node: %s', node)
  console.log('linkedFrom: %s', linkedFrom)
})
```

## Before/After/Guard Hooks

Makes it easy to embed custom logic into your graph.

```js
graph.before('add', function(a,b) {
  // execute before add
})

graph.after('add', function(a,b) {
  // execute after add
})

graph.guard('add', function(node) {
  // prevent add from running if return falsey
})

```

See these libraries for usage information:

* [timoxley/beforefn](http://github.com/timoxley/beforefn)
* [timoxley/guardfn](http://github.com/timoxley/guardfn)
* [timoxley/afterfn](http://github.com/timoxley/afterfn)

## License

MIT

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