4.1.1 • Published 7 years ago

recurserator v4.1.1

Weekly downloads
34
License
MIT
Repository
github
Last release
7 years ago

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.

ParamTypeDescription
objectObjectThe object to recursively access
options.yieldOnFunctionA function that determines whether a value is yielded
options.traverseOnFunctionA function that determines whether a value is accessed with recursion
options.readEntryFunctionA function that extracts the key/value pair from an object. Defaults to the entries method or own enumerable keys for objects
options.readNextFunctionA 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.

ParamTypeDescription
filterFunctionA 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.

ParamTypeDescription
filterFunctionA 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.

ParamTypeDescription
readNextFunctionstringA function that returns the next item to iterate over

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.

ParamTypeDescription
readEntryFunctionA 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.

ParamTypeDescription
newStateRecursionBuilderStateNew state to merge in

RecursionBuilder.create(object?, options?) ⇒ RecursionBuilder

Creates a RecursionBuilder.

ParamTypeDescription
objectObjectThe object to recursively access
options.yieldOnFunctionA function that determines whether a value is yielded
options.traverseOnFunctionA function that determines whether a value is accessed with recursion
options.readEntryFunctionA function that extracts the key/value pair from an object. Defaults to the entries method or own enumerable keys for objects
options.readNextFunctionA function that extracts the next item to iterate over

RecursionBuilder.prototype.recurse(object?) ⇒ Iterable

Runs the recursion algorithm on the provided data object.

ParamTypeDescription
objectObjectThe 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.

ParamTypeDescription
objectObjectThe 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.

ParamTypeDescription
objectObjectThe 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.

ParamTypeDescription
objectObjectThe 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.

ParamTypeDescription
objectObjectThe 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.

ParamTypeDescription
keystringThe key to extract from the result object
objectObjectThe object to recursively access
4.1.1

7 years ago

4.1.0

7 years ago

4.0.0

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.0.6

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.10

8 years ago

1.1.9

8 years ago

1.1.8

8 years ago

1.1.7

8 years ago

1.1.6

8 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.0

8 years ago