# directed-graph-map

> [![CircleCI](https://circleci.com/gh/wehriam/directed-graph-map.svg?style=svg)](https://circleci.com/gh/wehriam/directed-graph-map) [![npm version](https://badge.fury.io/js/directed-graph-map.svg)](http://badge.fury.io/js/directed-graph-map) [![codecov](h

Latest version **1.2.8** (published 2020-08-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install directed-graph-map
pnpm add directed-graph-map
yarn add directed-graph-map
bun add directed-graph-map
```

## 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.2.8 |
| Published | 2020-08-12 |
| First published | 2017-12-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 309.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | John Wehr |
| Maintainers | wehriam |

## Links

- npm: https://www.npmjs.com/package/directed-graph-map
- Repository: https://github.com/wehriam/directed-graph-map
- Homepage: https://github.com/wehriam/directed-graph-map#readme
- Issues: https://github.com/wehriam/directed-graph-map/issues
- npm.io page: https://npm.io/package/directed-graph-map

## Recent versions

- 1.2.8 (latest) — 2020-08-12
- 1.2.7 — 2020-08-12
- 1.2.6 — 2019-04-05
- 1.2.5 — 2019-03-19
- 1.2.4 — 2019-03-11
- 1.2.3 — 2018-02-01
- 1.2.2 — 2018-01-20
- 1.2.1 — 2018-01-20
- 1.2.0 — 2018-01-20
- 1.1.0 — 2018-01-17
- 1.0.4 — 2017-12-04
- 1.0.3 — 2017-12-04
- 1.0.2 — 2017-12-04
- 1.0.1 — 2017-12-03
- 1.0.0 — 2017-12-03

## README

# Directed Graph Map

[![CircleCI](https://circleci.com/gh/wehriam/directed-graph-map.svg?style=svg)](https://circleci.com/gh/wehriam/directed-graph-map) [![npm version](https://badge.fury.io/js/directed-graph-map.svg)](http://badge.fury.io/js/directed-graph-map) [![codecov](https://codecov.io/gh/wehriam/directed-graph-map/branch/master/graph/badge.svg)](https://codecov.io/gh/wehriam/directed-graph-map)

Directed graph data structure [implemented](https://github.com/wehriam/directed-graph-map/blob/master/src/index.js) using native `Map` and `Set` objects. Similiar to multi-key maps or bidirectional maps.

```js
const dg = new DirectedGraphMap();
                      //  A
dg.addEdge('A', 'X'); //  ├── X
dg.addEdge('A', 'Y'); //  ├── Y
dg.addEdge('A', 'Z'); //  └── Z

dg.getTargets('A');   //  X, Y, Z

dg.size; // 3
dg.edges; // [['A', 'X'], ['A', 'Y'], ['A', 'Z']]
dg.sources; // ['A']
dg.targets; // ['X', 'Y', 'z']
```

## Install

`yarn add directed-graph-map`

## Usage

```js
const DirectedGraphMap = require('directed-graph-map');

const dgm = new DirectedGraphMap([['A', 'B']]);

//  A
//  └── B

dgm.hasEdge('A', 'B'); // true

dgm.addEdge('B', 'C');

//  A
//  └── B
//      └── C

dgm.hasEdge('B', 'C'); // true
dgm.getTargets('A'); // new Set(['B']);
dgm.getTargets('B'); // new Set(['C']);
dgm.getTargets('C'); // new Set();
dgm.getSources('A'); // new Set();
dgm.getSources('B'); // new Set(['A']);
dgm.getSources('C'); // new Set(['B']);

dgm.removeSource('A');

//  B
//  └── C

dgm.hasEdge('A', 'B'); // false
dgm.getTargets('A'); // new Set();

dgm.removeTarget('C');

//  Empty

dgm.getTargets('B'); // new Set();
dgm.hasEdge('B', 'C'); // false

dgm.addEdge('A', 'B');

//  A
//  └── B

dgm.hasEdge('A', 'B'); // true

dgm.removeEdge('A', 'B');

//  Empty

dgm.hasEdge('A', 'B'); // false
```

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

-   [DirectedGraphMap](#directedgraphmap)
    -   [Parameters](#parameters)
    -   [addEdge](#addedge)
        -   [Parameters](#parameters-1)
    -   [removeEdge](#removeedge)
        -   [Parameters](#parameters-2)
    -   [hasEdge](#hasedge)
        -   [Parameters](#parameters-3)
    -   [hasSource](#hassource)
        -   [Parameters](#parameters-4)
    -   [hasTarget](#hastarget)
        -   [Parameters](#parameters-5)
    -   [removeSource](#removesource)
        -   [Parameters](#parameters-6)
    -   [removeTarget](#removetarget)
        -   [Parameters](#parameters-7)
    -   [getSources](#getsources)
        -   [Parameters](#parameters-8)
    -   [getTargets](#gettargets)
        -   [Parameters](#parameters-9)
-   [DirectedGraphMap#edges](#directedgraphmapedges)
-   [DirectedGraphMap#size](#directedgraphmapsize)
-   [DirectedGraphMap#sources](#directedgraphmapsources)
-   [DirectedGraphMap#targets](#directedgraphmaptargets)

### DirectedGraphMap

Class representing a Directed Graph Map

#### Parameters

-   `edges` **Iterable&lt;\[S, T]>** Iterable containing source -> target pairs (optional, default `[]`)

#### addEdge

Add an edge to the graph map.

##### Parameters

-   `source` **S** Source of the edge
-   `target` **T** Target of the edge

Returns **void** 

#### removeEdge

Remove an edge from the graph map.

##### Parameters

-   `source` **S** Source of the edge
-   `target` **T** Target of the edge

Returns **void** 

#### hasEdge

Test if a edge exists in the graph map.

##### Parameters

-   `source` **S** Source of the edge
-   `target` **T** Target of the edge

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Whether the edge exists in the graph map.

#### hasSource

Test if a source exists in the graph map.

##### Parameters

-   `source` **S** Source of the edge

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 

#### hasTarget

Test if a target exists in the graph map.

##### Parameters

-   `target` **T** Target of the edge

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 

#### removeSource

Remove all edges from a source.

##### Parameters

-   `source` **S** Source of the edge

Returns **void** 

#### removeTarget

Remove all edges to a target.

##### Parameters

-   `target` **T** Target of the edge

Returns **void** 

#### getSources

Get all sources with edges to a target.

##### Parameters

-   `target` **T** Target of the edge

Returns **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)&lt;S>** Set of sources

#### getTargets

Get all targets with edges from a source.

##### Parameters

-   `source` **S** Source of the edge

Returns **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)&lt;T>** Set of targets

### DirectedGraphMap#edges

Array of edges

### DirectedGraphMap#size

Edge count

### DirectedGraphMap#sources

Set of sources

### DirectedGraphMap#targets

Set of targets

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