cdset
Helper function to minimally clone an object/array then overwrite a potentially deeply nested property value.
It's like dset, but avoids mutating the original object.
Install
$ npm install --save cdset
Usage
const cdset = require("cdset");
const foo = { a: 1, b: 2 };
console.log(cdset(foo, "d.e.f", "hello"));
console.log(foo);
const bar = { foo: 123, bar: [4, 5, 6], baz: {} };
console.log(cdset(bar, "bar.1", 999));
console.log(bar)
const qux = {};
console.log(cdset(cdset(qux, "a.0.b.0", 1), "a.0.b.1", 2));
console.log(qux);
API
cdset(source, path, value)
Returns: a clone of source with the specified deeply-nestead-property set to value
source
Type: Object
or Array
The source object/array to clone & update with a value.
path
Type: String
or Array
The key path that should receive the value. May be in x.y.z
or ['x', 'y', 'z']
formats.
value
Type: Any
The value that you want to set. Can be of any type!