# ancestor

> find the lowest common ancestor in a directed, acyclic graph

Latest version **0.2.2** (published 2025-04-30) · 0 weekly downloads

## Install

```sh
npm install ancestor
pnpm add ancestor
yarn add ancestor
bun add ancestor
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2025-04-30 |
| First published | 2012-12-12 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 5.8 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | mirkok |

## Links

- npm: https://www.npmjs.com/package/ancestor
- Repository: https://github.com/mirkokiefer/ancestor
- Issues: https://github.com/mirkokiefer/ancestor/issues
- npm.io page: https://npm.io/package/ancestor

## Dependencies (2)

- [async](https://npm.io/package/async.md) ~0.2.9
- [underscore](https://npm.io/package/underscore.md) ~1.4.3

## Recent versions

- 0.2.2 (latest) — 2025-04-30
- 0.2.1 — 2025-04-30
- 0.2.0 — 2025-04-30
- 0.1.3 — 2013-09-08
- 0.1.2 — 2013-08-24
- 0.1.0 — 2013-06-25
- 0.0.1 — 2012-12-12

## README

# ancestor.js - Lowest Common Ancestor in JavaScript

![Test](https://github.com/mirkokiefer/ancestor/actions/workflows/test.yml/badge.svg)

![npm version](https://badge.fury.io/js/ancestor.svg)

Find the lowest-common-ancestor in a directed acyclic graph (DAG) for JavaScript & TypeScript.

## Install

```bash
npm install ancestor
```

## Usage

### ES Module

```js
import ancestor from 'ancestor'
```

### CommonJS

```js
const ancestor = require('ancestor')
```

#### Example

```js
const nodes = {1: [], 2: [1], 3: [2], 4: [2], 5: [4], 6: [3,5], 7: [6], 8: [5], 9: [8]}

/* the graph:

    4-5-8-9   
   /   \
1-2-3---6-7

*/

const readParents = (id, cb) =>
  process.nextTick(() => cb(null, nodes[id] || []))

ancestor([9, 7], readParents, (err, res) => {
  console.log(res) // 5
})
```

## API

```ts
function lowestCommonAncestor<T>(
  startNodes: T[],
  readParents: (id: T, cb: (err: Error | null, parents?: T[]) => void) => void,
  cb: (err: Error | null, res?: T) => void
): void
export default lowestCommonAncestor
```

- **startNodes**: array of node IDs  
- **readParents**: callback-based fetch of parent IDs  
- **cb**: callback with error or the LCA ID  

## Test

```bash
npm test
```

## License

BSD

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