Skip to content

ak10m/js-closure-table

Repository files navigation

closure-table Build Status

Closure Table on javascript

Install

npm install closure-table

Usage

import ClosureTable, { ITreeNode } from 'closure-table';

// sample node

interface Sample {
  id: number;
}

const n1: Sample = { id: 1 };
const n2: Sample = { id: 2 };
const n3: Sample = { id: 3 };
const n4: Sample = { id: 4 };
const n5: Sample = { id: 5 };
const n6: Sample = { id: 6 };
const n7: Sample = { id: 7 };

// sample tree
//
//  n1 -+- n2 --- n4
//      |
//      +- n3 -+- n5
//             |
//             +- n6 --- n7

const tree: ITreeNode<Sample> = {
  value: n1,
  children: [
    {
      value: n2,
      children: [{ value: n4 }],
    },
    {
      value: n3,
      children: [
        { value: n5 },
        { value: n6, children: [{ value: n7 }] },
      ],
    },
  ],
};

// initialize
const ct = new ClosureTable(tree);

// function examples

ct.findAncestorsOf(n3);         // [ { id: 1 } ]
ct.findAncestorsOf(n3, true);   // [ { id: 3 }, { id: 1 } ]
ct.findDescendantsOf(n3);       // [ { id: 5 }, { id: 6 }, { id: 7 } ]
ct.findDescendantsOf(n3, true); // [ { id: 3 }, { id: 5 }, { id: 6 }, { id: 7 } ]
ct.findChildrenOf(n3);          // [ { id: 5 }, { id: 6 } ]

// default equal function:
//
//   function(source: T, target: T): boolean {
//     return source === target;
//   }

ct.findDescendantsOf({ id: 3 }); // []

// modify equal function

ct.setEqualFunc((s: Sample, t: Sample): boolean => s.id === t.id);

ct.findDescendantsOf({ id: 3 }); // [ { id: 5 }, { id: 6 }, { id: 7 } ]

Run tests

npm run test

Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

License

Copyright © 2019 ak10m akio.morimoto@airits.jp.
This project is MIT licensed.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published