# @moyuyc/walk-tree

> Enhanced and multifunctional tree walker

Latest version **1.0.3** (published 2018-10-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @moyuyc/walk-tree
pnpm add @moyuyc/walk-tree
yarn add @moyuyc/walk-tree
bun add @moyuyc/walk-tree
```

## 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 | 1.0.3 |
| Published | 2018-10-30 |
| First published | 2018-09-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 4 |
| Unpacked size | 18.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | imcuttle |
| Maintainers | moyuyc |
| Keywords | imcuttle, walk-tree, walk, tree, traverse, traversal, bfs, dfs, post, pre, bfs, visit |

## Links

- npm: https://www.npmjs.com/package/@moyuyc/walk-tree
- Repository: https://github.com/imcuttle/walk-tree
- Homepage: https://github.com/imcuttle/walk-tree#readme
- Issues: https://github.com/imcuttle/walk-tree/issues
- npm.io page: https://npm.io/package/@moyuyc/walk-tree

## Dependencies (4)

- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2
- [lodash.set](https://npm.io/package/lodash.set.md) ^4.3.2
- [tree-crawl](https://npm.io/package/tree-crawl.md) ^1.0.5
- [lodash.unset](https://npm.io/package/lodash.unset.md) ^4.5.2

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 1.0.3 (latest) — 2018-10-30
- 1.0.2 — 2018-09-17
- 1.0.1 — 2018-09-17
- 1.0.0 — 2018-09-17

## README

# @moyuyc/walk-tree

[![Build status](https://img.shields.io/travis/imcuttle/walk-tree/master.svg?style=flat-square)](https://travis-ci.org/imcuttle/walk-tree)
[![Test coverage](https://img.shields.io/codecov/c/github/imcuttle/walk-tree.svg?style=flat-square)](https://codecov.io/github/imcuttle/walk-tree?branch=master)
[![NPM version](https://img.shields.io/npm/v/@moyuyc/walk-tree.svg?style=flat-square)](https://www.npmjs.com/package/@moyuyc/walk-tree)
[![NPM Downloads](https://img.shields.io/npm/dm/@moyuyc/walk-tree.svg?style=flat-square&maxAge=43200)](https://www.npmjs.com/package/@moyuyc/walk-tree)
[![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://prettier.io/)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)

Enhanced and multifunctional tree walker

## Installation

```bash
npm install @moyuyc/walk-tree
# or use yarn
yarn add @moyuyc/walk-tree
```

## Usage

```javascript
const walkTree = require('@moyuyc/walk-tree')

walkTree(
  {
    name: 'root',
    children: [{ name: 'c1' }, { name: 'c2' }, { name: 'c3', children: { name: 'c31' } }]
  },
  node => {
    console.log(node.name)
  }
)
// prints
// root / c1 / c2 / c3 / c31
// by order
```

## API

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

### walk

[index.js:54-138](https://github.com/imcuttle/walk-tree/blob/5f0f62e0b76892dcdd2013d2f63ad063de9003a3/index.js#L54-L138 'Source code on GitHub')

#### Parameters

- `tree` {T} - Type `T` should extends Object
- `walker` {(node, ctx: Context) => {}} - Iterator for each node by order
- `opts` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** {object}
  - `opts.path` {string} - The child's path on recursive struction (optional, default `'children'`)
  - `opts.order` {'pre' | 'post' | 'bfs'}
    <br/>
    `pre` means walking the node before walking it's children node by dfs <br/>
    `post` means walking the node after walking it's children node by dfs <br/>
    `bfs` means walking the node by bfs <br/> (optional, default `'pre'`)
  - `opts.skipVisited` {boolean}
    Should skip the node which has been visited. (optional, default `true`)
  - `opts.uniquePath` {Function | string | null}
    The unique's path for determining the node has been visited (same node) (optional, default `node=>node`)
  - `opts.state` {any}
    Inject in `context.state` on customized way

Returns **any** walkedTree {T}

## Context

A traversal context.

Four operations are available. Note that depending on the traversal order, some operations have no effects.

#### `remove`

```javascript
walk(rootNode, (node, ctx) => {
  if (node.name === 'remove-me') {
    return ctx.remove()
  }
})
```

#### `replace`

```javascript
walk(rootNode, (node, ctx) => {
  if (node.name === 'replace-me') {
    return ctx.replace({ name: 'new-me' })
  }
})
```

#### `break`

Stop traversal now.

```javascript
walk(rootNode, (node, ctx) => {
  if (node.name === 'stop') {
    return ctx.break()
  }
})
```

#### `skip`

Skip current node, children won't be visited.

```javascript
walk(rootNode, (node, ctx) => {
  if (node.name === 'skip') {
    return ctx.skip()
  }
})
```

#### `parent`

Get the parent of the current node.

#### `depth`

Get the depth of the current node. The depth is the number of ancestors the current node has.

#### `level`

Get the level of current node. The level is the number of ancestors+1 the current node has.

#### `index`

Get the index of the current node.

## Credit

The core algorithm of traverse credits to [tree-crawl](https://github.com/ngryman/tree-crawl)

walk-tree has the different with `tree-crawl`

Because tree-crawl has no idea about the intrinsic structure of your tree, you have to remove the node yourself. `Context#remove` only notifies the traversal code that the structure of the tree has changed.

Otherwise walk-tree would infers your tree by option `path` so don't requires additional remove action.

## Authors

This library is written and maintained by imcuttle, <mailto:moyuyc95@gmail.com>.

## License

MIT

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