@kingjs/descriptor.nested.reduce

1.0.11 • Public • Published

@kingjs/descriptor.nested.reduce

Accumulates the values of leafs at specific paths using a callback.

Usage

Return an array of pet names like this:

var reduce = require('@kingjs/descriptor.nested.reduce');

var people = {
  alice: { 
    pet: 'tiger' 
  },
  bob: { 
    pet: 'snuggles' 
  },
  chris: {
    pet: 'spike'
  }
}

var tree = {
  '*': { pet: null }
}

reduce(people, tree, (a, o) => {
  a.push(o);
  return a;
}, []);

result:

[ 'tiger', 'snuggles', 'spike' ]

API

declare function reduce(
  tree: NestedDescriptor,
  paths: NestedDescriptor,
  callback: (accumulator, value) => any,
  initialValue?,
  thisArg?
): any

Interfaces

Parameters

  • tree: A tree whose leaf values that correspond to a path in paths are accumulated.
  • paths: Paths to search in tree. If the path is found, then it's value is accumulated. Any property named '*' found in paths is replicated with properties whose names correspond to properties of tree not already present as siblings of '*'.
  • callback: A callback invoked on each value accumulated. Return the newly accumulated value.
    • accumulator: The accumulated value so far. Will be null by default.
    • value: The leaf being currently accumulated.
  • initialValue: Value to use as the first argument of the callback. If no initial value is supplied, the first value visited will be used.
  • thisArg: The this argument to pass to callback.

Returns

Returns the accumulated value or null if no values were accumulated..

Install

With npm installed, run

$ npm install @kingjs/descriptor.nested.reduce

License

MIT

Analytics

Readme

Keywords

none

Package Sidebar

Install

npm i @kingjs/descriptor.nested.reduce

Weekly Downloads

0

Version

1.0.11

License

MIT

Unpacked Size

4.54 kB

Total Files

4

Last publish

Collaborators

  • kingces95