lukeed / lukeed/dset

Consider adding array merge strategy option

Open
#31 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
783
Forks
29
PR merge metrics
No merged PRs in 30d

Description

Given following code:

```js
const host = { arr: ['hello'] }
const { dset } = require('dset/merge')
dset(host, 'arr', ['world'])

// or
const host = { arr: ['hello'] }
const { merge } = require('dset/merge')
merge({arr: ['hello']}, {arr: ['world']})
```
I would expect the result to be `{ arr: ['hello', 'world'] }`, however currently the result is `{ arr: ['world'] }`
If I change the second `arr` to `barr` the result is `{ arr: ['hello'], barr: ['world'] }`

The README says:

> 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

So I would expect arrays to merge too instead of being forcibly overwritten as in the 'standard' `dset`
Anyhow i understand the merge logic is similar to [lodash.merge](https://docs-lodash.com/v4/merge/).
The [`deepmerge module`](https://github.com/TehShrike/deepmerge) has a merge strategy option, but it only handles merging, not what `dset` does.
It would be great if dset could be used with a strategy of "add" instead of "overwrite".

```diff
+ strat = strat || (a, b, k) => { a[k] = merge(a[k], b[k]) };
if (Array.isArray(a) && Array.isArray(b)) {
for (k=0; k < b.length; k++) {
- a[k] = merge(a[k], b[k]);
+ strat(a, b, k)
}
}
+ // alt strat: (a, b, k) => a.push(b[k])
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Review the README and the dset/merge entry point to understand the existing merge behavior and public API. Clarify how an optional array strategy should coexist with the current overwrite behavior, then verify that both the requested append case and existing object and array merges are covered before considering the issue done.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.