1.0.18 • Published 5 years ago

@kingjs/descriptor.nested.update v1.0.18

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

@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