@hspkg/mutate

1.0.3 • Public • Published

mutate

NPM version Build Status Downloads

Modify object and return immutable object.

Usage

npm i @hspkg/mutate -S

API

/**
 * Modify object and return immutable object
 * @param {object} params The source object
 * @param {any} value The value to be set
 * @param {string[]} keys The keys in chain
 * @returns {object}
 */
mutate(params, value, ...keys);

Examples

import mutate from '@hspkg/mutate';

const source = {
  key1: 123,
  key3: 'test3',
  key4: {
    key5: {
      key6: 'Hello',
      key7: 'World'
    }
  }
};

console.log(mutate(source, 'hack', 'key4', 'key5', 'key6'));
// {
//   "key1": 123,
//   "key3": "test3",
//   "key4": {
//     "key5": {
//       "key6": "hack",
//       "key7": "World"
//     }
//   }
// }

console.log(mutate(source, 'hack', 'key4', 'key5', 'key8'));
// {
//   "key1": 123,
//   "key3": "test3",
//   "key4": {
//     "key5": {
//       "key6": "Hello",
//       "key7": "World",
//       "key8": "hack"
//     }
//   }
// }

Test

npm test

License

License

Package Sidebar

Install

npm i @hspkg/mutate

Weekly Downloads

0

Version

1.0.3

License

MIT

Last publish

Collaborators

  • int64ago