v4.0.0-beta.2
Deeply merge 2 or more objects respecting type information.
Repository
Current version released
3 years ago
Versions
- v7.1.3Latest
- v7.1.2
- v7.1.1
- v7.1.0
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0.0
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- v5.1.0
- v5.0.0
- v5.0.0-next.4
- v5.0.0-next.3
- v5.0.0-next.2
- v4.3.0
- v5.0.0-next.1
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.0
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v4.0.0-beta.2
- v4.0.0-beta.1
- v3.1.0-beta.1
- v3.0.1
- v3.0.0
- v3.0.0-beta.7
- v3.0.0-beta.6
- v3.0.0-beta.5
- v3.0.0-beta.4
- v3.0.0-beta.3
- v3.0.0-beta.2
- v3.0.0-beta.1
- v2.0.1
- v2.0.0
- v2.0.0-beta.1
- v1.1.8-beta.1
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
Donate
Any donations would be much appreciated. 😄
Installation
Node
# Install with npm
npm install deepmerge-ts --save-dev
# Install with yarn
yarn add -D deepmerge-ts
Deno
// import_map.json
{
"imports": {
"deepmerge-ts": "https://deno.land/x/deepmergets@__version__/dist/deno/index.ts"
}
}
Features
- Smart merging - High performance.
- Merged output has correct typing.
- Record merging support.
- Array merging support.
- Map and Set merging support.
- Customized merging.
Usage
Example using default config
import { deepmerge } from "deepmerge-ts";
const x = {
record: {
prop1: "value1",
prop2: "value2",
},
array: [1, 2, 3],
set: new Set([1, 2, 3]),
map: new Map([
["key1", "value1"],
["key2", "value2"],
]),
};
const y = {
record: {
prop1: "changed",
prop3: "value3",
},
array: [2, 3, 4],
set: new Set([2, 3, 4]),
map: new Map([
["key2", "changed"],
["key3", "value3"],
]),
};
const merged = deepmerge(x, y);
console.log(merged);
// Prettierfied output:
//
// Object {
// "record": Object {
// "prop1": "changed",
// "prop2": "value2",
// "prop3": "value3",
// },
// "array": Array [1, 2, 3, 2, 3, 4],
// "set": Set { 1, 2, 3, 4 },
// "map": Map {
// "key1" => "value1",
// "key2" => "changed",
// "key3" => "value3",
// },
// }
You can try out this example at codesandbox.io.
Using customized config
Performance
We use smart merging instead of the classic merging strategy which some alternative libraries use. This vastly improves performance, both in execution time and memory usage.
Classic Merge (not what we do)
With classic merging, each input is merged with the next input until all inputs are merged.
This strategy has large performance issues when lots of items need to be merged.
Smart Merge (what we do)
With our smart merging, we look ahead to see what can be merged and only merge those things.
In addition to performance improvements, this strategy merges multiple inputs at once; allowing for benefits such as taking averages of the inputs.