Comments (2)
Thanks for the suggestion! I think that would make a great addition to the existing utilities. Will try and find some time this weekend to add it to the library.
from froebel.
It's implemented under the name merge
by bd90ce2 and included in v0.23.0
.
That was way more interesting than expected because of all the special cases that need to be considered.
For example, merging self-referential data:
const a = {}
const b = {}
a.foo = b
b.bar = a
const merged = merge(a, b)
What should merged
be? Clearly { foo: merged, bar: merged }
.
But what about this?
const a = [1, 2]
a.push(a)
const b = [3, 4]
b.push(b)
const merged = merge(a, b)
Probably [ 1, 2, merged, 3, 4, merged ]
(that's how it's implemented) though I'd understand if someone argues merged[2]
and merged[5]
shouldn't be the same reference.
And what about references in Map
s?
const a = new Map()
a.set([a, a])
const b = new Map()
b.set([b, b])
const merged = merge(a, b)
I don't think there is any right or wrong answer here but I decided self-references in both keys and values will also be self-referencing in the result, so [...merged.entries()]
will be [ [merged, merged] ]
.
For some reason in the last example Lodash will return [ [a, a], [merged, merged] ]
which I don't think makes any sense at all.
There were also some interesting problems in the type signature. E.g. what is the type of a
when merging { a: 1 }
and { a?: 2 }
? It could be 1
or 2
or undefined
(if the second object is { a: undefined }
) but it shouldn't be optional because there will always be a value for a
in the first object, so the resulting type should be { a: 1 | 2 | undefined }
.
And what about if one or both of the merged values is a union type? E.g. merging string | { a: 1; d: 4 }
and number | { a: 2; c: 3 }
. All the possible types of the unions must be considered so the resulting type has to be number | { a: 2; c: 3 } | { a: 2; c: 3; d: 4 }
.
And what about something like merge({ list: [1, 2] }, { list: new Set([2, 3]) })
? Should arrays and Set
s be merged? Same could be asked for regular objects and Map
s or other iterable structures. For now I decided to treat them as separate types so if the first parameter is an array and the second one a Set
then the Set
will override the array and their values won't be merged. But I think it might make sense to add the option to define custom merge strategies for these cases in the future.
from froebel.
Related Issues (9)
- React Native support HOT 1
- Future Request: castArray HOT 1
- Does the `pipe` type need to be recursive?
- Port of `_.invert`? HOT 1
- Fails to resolve types with "moduleResolution": "bundler" in tsconfig HOT 1
- Clone (structuredClone) doesn't work with proxies
- Only check staged files in precommit hook HOT 1
- Utility Request: Unary HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from froebel.