# hierarchy-closure

> keep closure of parents and children in hierarchy

Latest version **1.2.2** (published 2023-11-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install hierarchy-closure
pnpm add hierarchy-closure
yarn add hierarchy-closure
bun add hierarchy-closure
```

## Health

**Score 40/100 (D)** — status: abandoned.

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

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2023-11-05 |
| First published | 2018-07-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Eric Prud'hommeaux |
| Maintainers | ericprud |

## Links

- npm: https://www.npmjs.com/package/hierarchy-closure
- Repository: https://github.com/ericprud/hierarchy-closure
- Homepage: https://github.com/ericprud/hierarchy-closure#readme
- Issues: https://github.com/ericprud/hierarchy-closure/issues
- npm.io page: https://npm.io/package/hierarchy-closure

## Recent versions

- 1.2.2 (latest) — 2023-11-05
- 1.2.1 — 2019-06-28
- 1.2.0 — 2019-06-28
- 1.1.0 — 2018-07-24
- 1.0.1 — 2018-07-23
- 1.0.0 — 2018-07-23

## README

[![NPM Version](https://badge.fury.io/js/hierarchy-closure.png)](https://npmjs.org/package/hierarchy-closure)
[![Build Status](https://travis-ci.org/ericprud/hierarchy-closure.svg?branch=master)](https://travis-ci.org/ericprud/hierarchy-closure)
[![Coverage Status](https://coveralls.io/repos/github/ericprud/hierarchy-closure/badge.svg?branch=master)](https://coveralls.io/github/ericprud/hierarchy-closure?branch=master)

# hierarchy-closure
Maintain simple hierarchy by adding members in any order.


## install

```
npm install --save hierarchy-closure
```

## create ()

This constructs a hierarchy.

``` javascript
// create hierarchy
myHierarchy = Hierarchy.create()
```

## add (parent, child)

Adding parent/child pairs builds a structure called `myHierarchy.roots` with the trees of added parent/child pairs.
It also creates two closures:
- *parents*: a mapping from child to its list of parents.
- *children*: a mapping from parent to its list of children.

### No order dependence

You can fill in your tree in any order and get the same closures:

``` javascript
// populating myHierarchy created above...
// add single entry B->C
myHierarchy.add('B', 'C')
// add single entry B->C
myHierarchy.add('B', 'C')
// add child C->D
myHierarchy.add('C', 'D')
// add disconnected entry F->G
myHierarchy.add('F', 'G')
// add parent E->F
myHierarchy.add('E', 'F')
// add middle D->E
myHierarchy.add('D', 'E')
// add top A->B
myHierarchy.add('A', 'B')
// add bottom G->H
myHierarchy.add('G', 'H')
// add redundant entry (no effect)
myHierarchy.add('A', 'B')
```

### add Exanple Results

``` javascript
{ add: myHierarchy.add,
  roots: {A: {B: {C: {D: {E: {F: {G: {H: {}}}}}}}}},
  parents: {
    A: [],
    B: ['A'],
    C: ['B', 'A'],
    D: ['C', 'B', 'A'],
    E: ['D', 'C', 'B', 'A'],
    F: ['E', 'D', 'C', 'B', 'A'],
    G: ['F', 'E', 'D', 'C', 'B', 'A'],
    H: ['G', 'F', 'E', 'D', 'C', 'B', 'A']
  },
  children: {
    A: ['B', 'C', 'D', 'E', 'F', 'G', 'H'],
    B: ['C', 'D', 'E', 'F', 'G', 'H'],
    C: ['D', 'E', 'F', 'G', 'H'],
    D: ['E', 'F', 'G', 'H'],
    E: ['F', 'G', 'H'],
    F: ['G', 'H'],
    G: ['H'],
    H: []
  }
}
```

## depthFirst (root, (parent, child) => { ... })

This calls you callback function with parent/child pairs starting with any children:

``` javascript
// create hierarchy
h2 = Hierarchy.create()
h2.add('A', 'AB1')
h2.add('AB1', 'AB1C1')
h2.add('AB1C1', 'AB1C1D1')
h2.add('AB1C1', 'AB1C1D2')
h2.add('AB1', 'AB1C2')
h2.add('AB1C2', 'AB1C2D1')
h2.add('A', 'AB2')
h2.add('AB2', 'AB2C1')
h2.add('AB2C1', 'AB2C1D1')
const seen = []
Hierarchy.depthFirst(t.roots, (l, r) => seen.push([l, r]))
```

### depthFirst Example Results

``` JSON
[
  [ "AB1C1D1", "AB1C1" ],
  [ "AB1C1D2", "AB1C1" ],
  [ "AB1C1", "AB1" ],
  [ "AB1C2D1", "AB1C2" ],
  [ "AB1C2", "AB1" ],
  [ "AB1", "A" ],
  [ "AB2C1D1", "AB2C1" ],
  [ "AB2C1", "AB2" ],
  [ "AB2", "A" ]
]
```

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