1.0.0 • Published 6 years ago

@kingjs/map.path v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

@kingjs/map.path

Maps an objects property values that are matched by a path.

Usage

Deduct a dollar from the balances of every account like this:

var mapPath = require('@kingjs/map.path');

var accounts = {
  alice: {
    id: 0,
    balance: 101
  },
  bob: {
    id: 1,
    balance: 201
  }
}

mapPath.call(
  accounts,
  '*.balance',
  function(x) { return x - 1; }
)

result:

{
  alice: {
    id: 0,
    balance: 100
  },
  bob: {
    id: 1,
    balance: 200
  }
}

API

declare function path(
  this,
  path: string | string[],
  callback: (x) => any
): any

Parameters

  • this: The object whose property's values are mapped.
  • path: An array of property names and/or the wildcard '*' which specify the path(s) to map.
  • callback: The mapping function to apply to each property that matches the path.
    • x: The value of the property being mapped.

Returns

Returns this after mapping properties which matched path.

Remarks

The path may also be a '.' delimited string composed of property names and/or the wildcard symbol '*'.

Install

With npm installed, run

$ npm install @kingjs/map.path

License

MIT