A tiny (197B) utility for safely writing deep Object values~!
A tiny (197B) utility for safely writing deep Object values~!
A tiny (197B) utility for safely writing deep Object values~!
For accessing deep object properties, please see dlv.
Using GraphQL? You may want
dset/merge– see Merging for more info.
$ npm install --save dsetThere are two "versions" of dset available:
dsetSize (gzip): 197 bytes
import { dset } from 'dset';dset/mergeSize (gzip): 307 bytes
import { dset } from 'dset/merge';…The main/default dset module forcibly writes values at the assigned key-path. However, in some cases, you may prefer to merge values at the key-path. For example, when using GraphQL's @stream and @defer directives, you will need to merge the response chunks into a single object/list. This is why dset/merge exists~!
Below is a quick illustration of the difference between dset and dset/merge:
let input = {
hello: {
abc: 123
}
};
dset(input, 'hello', { world: 123 });
console.log(input);
// via `dset`
//=> {
//=> hello: {
//=> world: 123
//=> }
//=> }
// via `dset/merge`
//=> {
//=> hello: {
//=> abc: 123,
//=> world: 123
//=> }
//=> }As shown in the examples above, all dset interactions mutate the source object.
If you need immutable writes, please visit clean-set (182B).
Alternatively, you may pair dset with klona, a 366B utility to clone your source(s). Here's an example pairing:
import { dset } from 'dset';
import { klona } from 'klona';
export function deepset(obj, path, val) {
let copy = klona(obj);
dset(copy, path, val);
return copy;
}Returns: void
Type: Object
The Object to traverse & mutate with a value.
Type: String or Array
The key path that should receive the value. May be in x.y.z or ['x', 'y', 'z'] formats.
Note: Please be aware that only the last key actually receives the value!
Important: New Objects are created at each segment if there is not an existing structure. However, when integers are encounted, Arrays are created instead!
Type: Any
The value that you want to set. Can be of any type!
For benchmarks and full results, check out the bench directory!
# Node 10.13.0
Validation:
✔ set-value
✔ lodash/set
✔ dset
Benchmark:
set-value x 1,701,821 ops/sec ±1.81% (93 runs sampled)
lodash/set x 975,530 ops/sec ±0.96% (91 runs sampled)
dset x 1,797,922 ops/sec ±0.32% (94 runs sampled)dset in 182 bytesMIT © Luke Edwards
No open issues yet, or sync has not completed.