Skip to content

Commit 8ee6d12

Browse files
committed
chore: add dset/merge to readme
1 parent 1241f63 commit 8ee6d12

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

readme.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,34 @@
44
55
For _accessing_ deep object properties, please see [`dlv`](https://github.com/developit/dlv).
66

7+
> **Using GraphQL?** You may want `dset/merge` – see [Merging](#merging) for more info.
8+
79
## Install
810

911
```sh
1012
$ npm install --save dset
1113
```
1214

15+
## Modes
16+
17+
There are two "versions" of `dset` available:
18+
19+
#### `dset`
20+
> **Size (gzip):** 196 bytes<br>
21+
> **Availability:** [CommonJS](https://unpkg.com/dset/dist/index.js), [ES Module](https://unpkg.com/dset/dist/index.mjs), [UMD](https://unpkg.com/dset/dist/index.min.js)
22+
23+
```js
24+
import { dset } from 'dset';
25+
```
26+
27+
#### `dset/merge`
28+
> **Size (gzip):** 289 bytes<br>
29+
> **Availability:** [CommonJS](https://unpkg.com/dset/merge/index.js), [ES Module](https://unpkg.com/dset/merge/index.mjs), [UMD](https://unpkg.com/dset/merge/index.min.js)
30+
31+
```js
32+
import { dset } from 'dset/merge';
33+
```
34+
1335

1436
## Usage
1537

@@ -64,6 +86,39 @@ console.log(baz);
6486
//=> }
6587
```
6688

89+
## Merging
90+
91+
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](https://foundation.graphql.org/news/2020/12/08/improving-latency-with-defer-and-stream-directives/), you will need to merge the response chunks into a single object/list. This is why `dset/merge` exists~!
92+
93+
Below is a quick illustration of the difference between `dset` and `dset/merge`:
94+
95+
```js
96+
let input = {
97+
hello: {
98+
abc: 123
99+
}
100+
};
101+
102+
dset(input, 'hello', { world: 123 });
103+
console.log(input);
104+
105+
// via `dset`
106+
//=> {
107+
//=> hello: {
108+
//=> world: 123
109+
//=> }
110+
//=> }
111+
112+
// via `dset/merge`
113+
//=> {
114+
//=> hello: {
115+
//=> abc: 123,
116+
//=> world: 123
117+
//=> }
118+
//=> }
119+
```
120+
121+
67122
## Immutability
68123

69124
As shown in the examples above, all `dset` interactions mutate the source object.
@@ -72,8 +127,8 @@ If you need immutable writes, please visit [`clean-set`](https://github.com/fwil
72127
Alternatively, you may pair `dset` with [`klona`](https://github.com/lukeed/klona), a 366B utility to clone your source(s). Here's an example pairing:
73128

74129
```js
75-
import { klona } from 'klona';
76130
import { dset } from 'dset';
131+
import { klona } from 'klona';
77132

78133
export function deepset(obj, path, val) {
79134
let copy = klona(obj);

0 commit comments

Comments
 (0)