# recurserator

> Handle object recursion like a boss

Latest version **4.1.1** (published 2017-07-23) · MIT license · 0 weekly downloads

## Install

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

## 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 | 4.1.1 |
| Published | 2017-07-23 |
| First published | 2015-12-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Steven Sojka |
| Maintainers | steelsojka |
| Keywords | generators, ES2015, recursion, utility |

## Links

- npm: https://www.npmjs.com/package/recurserator
- Repository: https://github.com/steelsojka/recurserator
- Homepage: https://github.com/steelsojka/recurserator#readme
- Issues: https://github.com/steelsojka/recurserator/issues
- npm.io page: https://npm.io/package/recurserator

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^1.6.1

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 4.1.1 (latest) — 2017-07-23
- 4.1.0 — 2017-07-23
- 4.0.0 — 2017-07-23
- 3.0.4 — 2017-06-15
- 3.0.3 — 2017-06-15
- 3.0.2 — 2017-06-15
- 3.0.1 — 2017-05-22
- 3.0.0 — 2017-04-28
- 2.0.6 — 2017-01-27
- 2.0.5 — 2017-01-27
- 2.0.4 — 2017-01-27
- 2.0.3 — 2016-08-12
- 2.0.2 — 2016-03-10
- 2.0.1 — 2016-02-23
- 2.0.0 — 2016-02-23
- … 14 more at https://npm.io/package/recurserator/versions

## README

Recurserator
============

Recurserator is a set of **recurs**ive gen**erator**s for recursively accessing an object.

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

Install
-------

`npm install --save recurserator`


Usage
-----

`import RecursionBuilder from 'recurserator';`

You can also use named imports.

`import { RecursionBuilder } from 'recurserator';`

API
---
<a name="RecursionBuilder"></a>
## RecursionBuilder(object, options) ⇒ <code>RecursionBuilder</code>
The RecursionBuilder builds a recursive alogorithm and iterates using that algorithm. Arguments are optional and can be supplied through a building pattern.
A RecursionBuilder instance itself is an iterable. RecursionBuilder objects are also immutable.
 
| Param                  | Type                  | Description                                                                                                                     |                                                          
| ---------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| object                 | <code>Object</code>   | The object to recursively access                                                                                                |
| options.yieldOn    | <code>Function</code> | A function that determines whether a value is yielded                                                                           |
| options.traverseOn | <code>Function</code> | A function that determines whether a value is accessed with recursion                                                           |
| options.readEntry | <code>Function</code> | A function that extracts the key/value pair from an object. Defaults to the `entries` method or own enumerable keys for objects |
| options.readNext | <code>Function</code> | A function that extracts the next item to iterate over                                                                          |

### Example

```javascript
import { RecursionBuilder } from 'recurserator';

const data = {
  value1: 10,
  aList: [{
    listKey: 'HI!'
  }],
  nested: {
    anotherNested: {
    }
  }
};

const builder = RecursionBuilder.create(data);

for (let { key, value, path, parent } of builder) {
  //=> ['value1', 10, 'value1', data]
  //=> ['aList', [...], 'aList', data]
  //=> ['0', {...}, 'aList[0]', data.aList]
  //=> ['listKey', 'Hi!', 'aList[0].listKey', data.aList[0]]
  //=> ['nested', {...}, 'nested', data]
  //=> ['anotherNested', {...}, 'nested.anotherNested', data.nested]
}

// Yield array value but don't access them

const truth = () => true;
const notArray = item => !Array.isArray(item);

for (let { key, value, path, parent } of builder.yieldOn(truth).traverseOn(notArray)) {
  //=> ['value1', 10, 'value1', data]
  //=> ['aList', [...], 'aList', data]
  //=> ['nested', {...}, 'nested', data]
  //=> ['anotherNested', {...}, 'nested.anotherNested', data.nested]
}

// Only yield objects

for (let { key, value, path, parent } of builder.yieldOn(isObject)) {
  //=> ['aList', [...], 'aList', data]
  //=> ['0', {...}, 'aList[0]', data.aList]
  //=> ['nested', {...}, 'nested', data]
  //=> ['anotherNested', {...}, 'nested.anotherNested', data.nested]
}
```

<a name="RecursionBuilder.prototype.yieldOn"></a>
## RecursionBuilder.prototype.yieldOn(filter) ⇒ <code>RecursionBuilder</code>
Sets the yield filter property. This condition is called to test whether a value should be yielded.
Returns a new RecursionBuilder instance.
 
| Param          | Type                  | Description                                           |                                                          
| -------------- | --------------------- | ----------------------------------------------------- |
| filter | <code>Function</code> | A function that determines whether a value is yielded |

<a name="RecursionBuilder.prototype.traverseOn"></a>
## RecursionBuilder.prototype.traverseOn(filter) ⇒ <code>RecursionBuilder</code>
Sets the traverse filter property. This condition is called to test whether a value should be traversed.
Returns a new RecursionBuilder instance.
 
| Param          | Type                  | Description                                                           |                                                          
| -------------- | --------------------- | --------------------------------------------------------------------- |
| filter | <code>Function</code> | A function that determines whether a value is accessed with recursion |

<a name="RecursionBuilder.prototype.readNext"></a>
## RecursionBuilder.prototype.extractor(extractor) ⇒ <code>RecursionBuilder</code>
Sets the read next property. When this method is provided, instead of traversing to the next key. This method will be called
to determine what the child of the value should be.
Returns a new RecursionBuilder instance.
 
| Param     | Type                         | Description                                           |                                                          
| --------- | ---------------------------- | ----------------------------------------------------- |
| readNext | <code>Function|string</code> | A function that returns the next item to iterate over |

<a name="RecursionBuilder.prototype.readEntry"></a>
## RecursionBuilder.prototype.readEntry(fn) ⇒ <code>RecursionBuilder</code>
Sets the extractor property. Used to extract key/value pair from an object. Defaults to `entries` iterator if it exists.
Returns a new RecursionBuilder instance.
 
| Param          | Type                  | Description                                                                                                                     |                                                          
| -------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| readEntry | <code>Function</code> | A function that extracts the key/value pair from an object. Defaults to the `entries` method or own enumerable keys for objects |

<a name="RecursionBuilder.prototype.clone"></a>
## RecursionBuilder.prototype.clone(newState = {}) ⇒ <code>RecursionBuilder</code>
Clones the builder object merging in the new state.
 
| Param    | Type                | Description           |                                                          
| -------- | ------------------- | --------------------- |
| newState | <code>RecursionBuilderState</code> | New state to merge in |

<a name="RecursionBuilder.create"></a>
## RecursionBuilder.create(object?, options?) ⇒ <code>RecursionBuilder</code>
Creates a RecursionBuilder.
 
| Param                  | Type                  | Description                                                                                                                     |                                                          
| ---------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| object                 | <code>Object</code>   | The object to recursively access                                                                                                |
| options.yieldOn    | <code>Function</code> | A function that determines whether a value is yielded                                                                           |
| options.traverseOn | <code>Function</code> | A function that determines whether a value is accessed with recursion                                                           |
| options.readEntry | <code>Function</code> | A function that extracts the key/value pair from an object. Defaults to the `entries` method or own enumerable keys for objects |
| options.readNext | <code>Function</code> | A function that extracts the next item to iterate over                                                                          |

<a name="RecursionBuilder.prototype.recurse"></a>
## RecursionBuilder.prototype.recurse(object?) ⇒ <code>Iterable</code>
Runs the recursion algorithm on the provided data object.
 
| Param                  | Type                  | Description                      |                                                          
| ---------------------- | --------------------- | ---------------------------------|
| object                 | <code>Object</code>   | The object to recursively access |

<a name="RecursionBuilder.prototype.keys"></a>
## RecursionBuilder.prototype.keys(object?) ⇒ <code>Iterable</code>
Runs the recursion algorithm on the provided data object. Yields only keys. If no object is provided the storage object in the builder will be used.
 
| Param                  | Type                  | Description                      |                                                          
| ---------------------- | --------------------- | ---------------------------------|
| object                 | <code>Object</code>   | The object to recursively access |

<a name="RecursionBuilder.prototype.values"></a>
## RecursionBuilder.prototype.values(object?) ⇒ <code>Iterable</code>
Runs the recursion algorithm on the provided data object. Yields only values. If no object is provided the storage object in the builder will be used.
 
| Param                  | Type                  | Description                      |                                                          
| ---------------------- | --------------------- | ---------------------------------|
| object                 | <code>Object</code>   | The object to recursively access |

<a name="RecursionBuilder.prototype.paths"></a>
## RecursionBuilder.prototype.paths(object?) ⇒ <code>Iterable</code>
Runs the recursion algorithm on the provided data object. Yields only paths. If no object is provided the storage object in the builder will be used.
 
| Param                  | Type                  | Description                      |                                                          
| ---------------------- | --------------------- | ---------------------------------|
| object                 | <code>Object</code>   | The object to recursively access |

<a name="RecursionBuilder.prototype.parents"></a>
## RecursionBuilder.prototype.parents(object?) ⇒ <code>Iterable</code>
Runs the recursion algorithm on the provided data object. Yields only parents. If no object is provided the storage object in the builder will be used.
 
| Param                  | Type                  | Description                      |                                                          
| ---------------------- | --------------------- | ---------------------------------|
| object                 | <code>Object</code>   | The object to recursively access |

<a name="RecursionBuilder.prototype.extract"></a>
## RecursionBuilder.prototype.extract(key, object?) ⇒ <code>Iterable</code>
Runs the recursion algorithm on the provided data object. Yields the key from the RecursionResult object. If no object is provided the storage object in the builder will be used.
 
| Param                  | Type                  | Description                      |                                                          
| ---------------------- | --------------------- | ---------------------------------|
| key                    | <code>string</code>   | The key to extract from the result object |
| object                 | <code>Object</code>   | The object to recursively access |

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