[feature request(collections)] option to merge `undefined` in `deepMerge`
- Dominant language
- TypeScript
- Stars
- 3.6k
- Forks
- 681
- PR merge metrics
- No merged PRs in 30d
Description
**Is your feature request related to a problem? Please describe.**
when merging together deeply nested object (e.g react state), it's important to get 'nonempty' values when merging 'empty' values with 'nonempty' values.
for array, map and set it defaults to merging empty and non-empty values together:
```ts
deepMerge({ a: new Set([1]) }, { a: new Set() })
//=> { a: Set(1) { 1 } }
```
however, it doesn't work with nullish values. it'd be very useful to be able to output non-nullish value from merging nullish and non-nullish value together.
```ts
deepMerge({ a: 1 }, { a: undefined })
//=> { a: undefined }
// expected: { a: 1 }
```
**Describe the solution you'd like**
in [DeepMergeOptions](https://jsr.io/@std/collections@0.224.1/doc/deep-merge/~/DeepMergeOptions), add `nullish: MergingStrategy` that controls whether `deepMerge` will choose non-nullish value over nullish ones, such that
```ts
type State = {
pos: {
x: number
y: number
}
}
type UpdateState = Partial<{
pos: Partial<{
x: number
y: number
}>
}>
const merge = (state: State, newState: UpdateState): State => deepMerge(state, newState, { nullish: "merge" })
```
always holds. (currently the return type of `merge` will be `UpdateState`)
**Describe alternatives you've considered**
make `nullish: "merge”` as default. this may cause breaking changes.
Contributor guide
Assessment
This issue has not been assessed yet.