1.0.3 • Published 6 years ago

@hspkg/mutate v1.0.3

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

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