npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

recurserator

v4.1.1

Published

Handle object recursion like a boss

Downloads

44

Readme

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 | A 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.

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