Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

recurserator

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

recurserator

Handle object recursion like a boss

latest
Source
npmnpm
Version
4.1.1
Version published
Maintainers
1
Created
Source

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
readNextFunctionstring

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

Keywords

generators

FAQs

Package last updated on 23 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts