https://github.com/vendicated/virtual-merge
A utility library to merge multiple Objects virtually
https://github.com/vendicated/virtual-merge
Last synced: 10 months ago
JSON representation
A utility library to merge multiple Objects virtually
- Host: GitHub
- URL: https://github.com/vendicated/virtual-merge
- Owner: Vendicated
- License: mit
- Created: 2023-03-12T23:27:32.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-13T14:41:48.000Z (over 1 year ago)
- Last Synced: 2025-03-21T01:23:41.843Z (about 1 year ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/virtual-merge
- Size: 7.81 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Virtual Merge
A utility library to merge multiple Objects virtually
```sh
npm install virtual-merge
yarn add virtual-merge
pnpm add virtual-merge
```
Imagine having multiple Objects and wanting to merge those. The usual way to accomplish this is to do
```js
const merged = { ...obj1, ...obj2, ...obj3 };
```
However, changes made on `merged` will not reflect back to the original Objects.
This might be desired, in which case sweet, you don't need this module!
However, if you do want those changes to reflect back to the original object, you can use this module!
## Example
```js
import virtualMerge from "virtual-merge";
// or commonjs
const virtualMerge = require("virtual-merge");
const obj1 = { im: "so cool" };
const obj2 = { hamburgers: "are delicious!" };
const merged = virtualMerge(obj1, obj2);
merged.im = "so cute!";
console.log(merged.im); // "so cute!"
console.log(obj1.im); // "so cute!"
obj1.im = "awesome!";
console.log(merged.im); // "awesome!"
// new properties work too!
merged.someNewProperty = "wow this is in neither of the objects O_O";
console.log(merged.someNewProperty); // "wow this is in neither of the objects O_O"
console.log(obj1.someNewProperty); // undefined
console.log(obj2.someNewProperty); // undefined
// and of course, the usual object methods still work fine!
console.log(Object.keys(merged)); // ["someNewProperty", "im", "hamburgers"]
console.log("im" in merged); // true
console.log(Object.hasOwn(merged, "hamburgers")); // true
```
## License
MIT - Copyright (c) 2023 Vendicated