Handle object recursion like a boss
npm install recurseratorRecurserator
============
Recurserator is a set of recursive generators for recursively accessing an object.

Install
-------
npm install --save recurserator
Usage
-----
import RecursionBuilder from 'recurserator';
You can also use named imports.
import { RecursionBuilder } from 'recurserator';
RecursionBuilderObject | The object to recursively access |Function | A function that determines whether a value is yielded |Function | A function that determines whether a value is accessed with recursion |Function | A function that extracts the key/value pair from an object. Defaults to the entries method or own enumerable keys for objects |Function | A function that extracts the next item to iterate over |``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]
}
`
RecursionBuilderFunction | A function that determines whether a value is yielded |RecursionBuilderFunction | A function that determines whether a value is accessed with recursion |RecursionBuilderFunction|string | A function that returns the next item to iterate over |RecursionBuilder iterator if it exists.
Returns a new RecursionBuilder instance.
| Param | Type | Description |
| -------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| readEntry | Function | A function that extracts the key/value pair from an object. Defaults to the entries method or own enumerable keys for objects |RecursionBuilder.prototype.clone(newState = {}) ⇒ RecursionBuilder
Clones the builder object merging in the new state.
| Param | Type | Description |
| -------- | ------------------- | --------------------- |
| newState | RecursionBuilderState | New state to merge in |RecursionBuilder.create(object?, options?) ⇒ RecursionBuilder
Creates a RecursionBuilder.
| Param | Type | Description |
| ---------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| object | Object | The object to recursively access |
| options.yieldOn | Function | A function that determines whether a value is yielded |
| options.traverseOn | Function | A function that determines whether a value is accessed with recursion |
| options.readEntry | Function | A function that extracts the key/value pair from an object. Defaults to the entries` method or own enumerable keys for objects |Function | A function that extracts the next item to iterate over |IterableObject | The object to recursively access |IterableObject | The object to recursively access |IterableObject | The object to recursively access |IterableObject | The object to recursively access |IterableObject | The object to recursively access |Iterablestring | The key to extract from the result object |Object | The object to recursively access |