# traverser

> lib for traversing trees and graphs

Latest version **0.0.5** (published 2012-06-13) · 0 weekly downloads

## Install

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

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2012-06-13 |
| First published | 2011-02-07 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | Dominic Tarr |
| Maintainers | dominictarr |

## Links

- npm: https://www.npmjs.com/package/traverser
- Repository: https://github.com/dominictarr/traverser
- npm.io page: https://npm.io/package/traverser

## Dependencies (1)

- [curry](https://npm.io/package/curry.md) 0.0.x

## Recent versions

- 0.0.5 (latest) — 2012-06-13
- 0.0.4 — 2012-06-13
- 1.0.0 — 2011-06-25
- 0.0.2-2 — 2011-05-24
- 0.0.2-1 — 2011-05-24
- 0.0.1 — 2011-02-07
- 0.0.0 — 2011-02-07

## README

#Traverser#

search through a complex object (nearly) as easily as forEach

all the nitty-gritty stuff is taken care of.

including:
  # checking for repeats.
  # checking for cycles.
  # getting the path the the current object. etc!

#simple example#
collect all branches in a tree.

    var traverser = require('traverser')
      , branches = []

    function branch(props){
      if(!props.reference) //since we check for references with will work in cyclic graphs, without stackoverflows.
        branches.push(props.value)

      props.each()//continue working through the children of this object.
    }

    traverser([1,2,{},3,["x"]],{branch: branch})
    
    console.log(branches)

#simple example2#
collect all leaves in a tree. (only functions and primitives)

    var traverser = require('traverser')

    exports.leaves = leaves

    function leaves (obj){
        var leaves = []

        function leaf(props){
          leaves.push(props.value)
        }

        traverser(obj,{leaf: leaf})
        return leaves
    }

    traverser([1,2,{},3,["x"]],{leaf: leaves})

    console.log(leaves([1,2,{},3,["x"]]))
    //[1,2,3,'x']
    
#complex example#
even quite complex things like a topological sort are now achivable in ~30 lines.
see `traverser/examples/topo-sort`


#API#

  `traverser (obj, options)`
  
  obj: graph/tree to traverse
  
    options: (each of the following is optional)
    {
      branch: function to call on a branch of the tree (by default, where typeof == 'object'
    , leaf: function to call on primitives and functions
    , isBranch: return true if current value should be treated as a branch
    }
    
each function is passed one arg, a properties object which describes the state of the traverse.
  
    {
      value: this object
    , parent: object which this item is a property of. (null if your on the root object)
    , key: key of this item on parent. (null if your on the root object)
    , path: [list of keys from root object to current item]
    , reference: true if this object is a repeat
    , circular: true if this object is in the ancestors list.
    , seen: list of objects seen so far
    , ancestors: list of objects between value and root object.

    //when your in the branch function, you need to call one of the following to iterate over the children.
    //none of these require an argument.

    , each: iterate over each property
    , find: iterate untill the first truthy return, returns the item.
    , map: collects return values into an Array
    , copy: copies objects, preserving whether it's a {} or a []
    , max: iterate over all properties and return max return value
    , min: iterate over all properties and return min return value
    }

###feel free to ask me questions if you need help!###

###see https://github.com/dominictarr/trees for more examples.###

##License

Mit/Apache2

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