@kingjs/mutate.path

Updates property values matched by a path.

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
04Jun 21, 2021Jul 20, 2018Minified + gzip package size for @kingjs/mutate.path in KB

Readme

@kingjs/mutate.path

Updates property values matched by a path.

Usage

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

var mapPath = require('@kingjs/mutate.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: (this, x) => any
): any

Parameters

  • this: The object whose property's values are updated.
  • path: An array of property names and/or the wildcard '*' which specify the path(s) to map.
  • callback: The update function to apply to each property that matches the path.
    • this: The same this as passed to path (which may not be the same as object hosting the property of the value being upated).
    • x: The value of the property being updated.

      Returns

      Returns this after updating 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/mutate.path
      

      License

      MIT

Analytics

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.