@kingjs/descriptor.nested.update

1.0.18 • Public • Published

@kingjs/descriptor.nested.update

Updates the leafs of a tree to values returned by a callback which takes the current value.

Usage

Replace the name of the person followed with the object representing the person being followed like this:

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

var people = {

  alice: {
    name: 'Alice',
    follows: 'bob'
  },
  bob: {
    name: 'Bob', 
    follows: 'chris'
  },
  chris: {
    name: 'Chris',
    follows: 'alice'
  }
};

var result = update.call(
  people,
  people,
  { '*': { follows: null } },
  function(name) { return this[name]; }
)

result:

{
  alice: {
    name: 'Alice',
    follows: { /* bob */ }
  },
  bob: {
    name: 'Bob', 
    follows: { /* chris */ }
  },
  chris: {
    name: 'Chris',
    follows: { /* alice */ }
  }
}

API

declare function update(
  tree: NestedDescriptor,
  path: NestedDescriptor,
  callback: (leaf, path) => any,
  thisArg?
): NestedDescriptor

Interfaces

Parameters

  • tree: The tree whose leafs are going to be updated.
  • path: The paths of the values to be updated.
  • callback: Used to update paths of tree:
    • leaf: The leaf value.
    • path: The path value.
  • thisArg: The this argument to pass to callback.

Returns

Returns tree with updated values for the leafs found at paths.

Install

With npm installed, run

$ npm install @kingjs/descriptor.nested.update

License

MIT

Analytics

Readme

Keywords

none

Package Sidebar

Install

npm i @kingjs/descriptor.nested.update

Weekly Downloads

3

Version

1.0.18

License

MIT

Unpacked Size

4.8 kB

Total Files

4

Last publish

Collaborators

  • kingces95