recurserator
TypeScript icon, indicating that this package has built-in type declarations

4.1.1 • Public • Published

Recurserator

Recurserator is a set of recursive generators for recursively accessing an object.

npm version

Install

npm install --save recurserator

Usage

import RecursionBuilder from 'recurserator';

You can also use named imports.

import { RecursionBuilder } from 'recurserator';

API

RecursionBuilder(object, options) ⇒ RecursionBuilder

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 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
options.readNext Function A function that extracts the next item to iterate over

Example

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]
}

RecursionBuilder.prototype.yieldOn(filter) ⇒ RecursionBuilder

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 Function A function that determines whether a value is yielded

RecursionBuilder.prototype.traverseOn(filter) ⇒ RecursionBuilder

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 Function A function that determines whether a value is accessed with recursion

RecursionBuilder.prototype.extractor(extractor) ⇒ RecursionBuilder

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 Function string

RecursionBuilder.prototype.readEntry(fn) ⇒ RecursionBuilder

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 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
options.readNext Function A function that extracts the next item to iterate over

RecursionBuilder.prototype.recurse(object?) ⇒ Iterable

Runs the recursion algorithm on the provided data object.

Param Type Description
object Object The object to recursively access

RecursionBuilder.prototype.keys(object?) ⇒ Iterable

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 Object The object to recursively access

RecursionBuilder.prototype.values(object?) ⇒ Iterable

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 Object The object to recursively access

RecursionBuilder.prototype.paths(object?) ⇒ Iterable

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 Object The object to recursively access

RecursionBuilder.prototype.parents(object?) ⇒ Iterable

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 Object The object to recursively access

RecursionBuilder.prototype.extract(key, object?) ⇒ Iterable

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 string The key to extract from the result object
object Object The object to recursively access

Package Sidebar

Install

npm i recurserator

Weekly Downloads

5

Version

4.1.1

License

MIT

Last publish

Collaborators

  • steelsojka