Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
merge-anything
Advanced tools
The 'merge-anything' npm package is a utility for deep merging objects and arrays. It provides a simple and flexible way to combine multiple objects or arrays into one, handling nested structures and various data types seamlessly.
Deep Merge Objects
This feature allows you to deeply merge two or more objects. Nested properties are merged recursively.
const { merge } = require('merge-anything');
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { b: { d: 3 }, e: 4 };
const result = merge(obj1, obj2);
console.log(result); // { a: 1, b: { c: 2, d: 3 }, e: 4 }
Merge Arrays
This feature allows you to merge arrays, concatenating their elements.
const { merge } = require('merge-anything');
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const result = merge(arr1, arr2);
console.log(result); // [1, 2, 3, 4, 5, 6]
Custom Merge Functions
This feature allows you to define custom merge functions to handle specific types of data or merge strategies.
const { merge } = require('merge-anything');
const customMerge = (target, source) => Array.isArray(target) ? target.concat(source) : undefined;
const obj1 = { a: [1, 2] };
const obj2 = { a: [3, 4] };
const result = merge({ extensions: [customMerge] }, obj1, obj2);
console.log(result); // { a: [1, 2, 3, 4] }
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. It includes a `merge` function that performs deep merging of objects. Compared to 'merge-anything', Lodash offers a broader set of utilities but may be heavier if you only need merging capabilities.
Deepmerge is a library specifically designed for deep merging of JavaScript objects. It is lightweight and focuses solely on merging, making it a good alternative to 'merge-anything' if you need a dedicated merging tool without additional utilities.
Merge-deep is another utility for deep merging objects. It is similar to 'merge-anything' in its focus on merging nested structures but may have different handling of edge cases and performance characteristics.
npm i merge-anything
Merge two objects recursively. A simple & small integration.
I created this package because I tried a lot of similar packages that do merging/deepmerging/recursive object assign etc. But all had its quirks, and none were the simple implementation I was looking for.
Pass the base param first and then an unlimited amount of params to merge onto it.
import merge from 'merge-anything'
const starter = {name: 'Squirtle', type: 'water'}
const newValues = {name: 'Warturtle', level: 16}
merge(starter, newValues, {is: 'cool'})
// returns {
// name: 'Warturtle',
// type: 'water,
// level: 16,
// is: 'cool'
// }
// all passed objects STAY AS IS and do not get modified
const a = {a: 'a'}
const b = {b: 'b'}
const c = merge(a, b)
// a === {a: 'a'}
// b === {b: 'b'}
// c === {a: 'a', b: 'b'}
// arrays get overwritten
merge({array: ['a']}, {array: ['b']}) // returns {array: ['b']}
// empty objects merge into objects
merge({obj: {prop: 'a'}}, {obj: {}}) // returns {obj: {prop: 'a'}}
// but non-objects overwrite objects
merge({obj: {prop: 'a'}}, {obj: null}) // returns {prop: null}
merge({obj: 'a'}, 'b') // returns 'b'
// and empty objects overwrite non-objects
merge({prop: 'a'}, {prop: {}}) // returns {prop: {}}
It also properly keeps others special objects in-tact like dates, regex, functions etc.
It is literally just going through an object recursively and assigning the values to a new object like below. However, it's wrapped to allow extra params etc.
function mergeRecursively (origin, newComer) {
if (!isObject(newComer)) return newComer
// define newObject to merge all values upon
const newObject = (isObject(origin))
? Object.keys(origin)
.reduce((carry, key) => {
const targetVal = origin[key]
if (!Object.keys(newComer).includes(key)) carry[key] = targetVal
return carry
}, {})
: {}
return Object.keys(newComer)
.reduce((carry, key) => {
const newVal = newComer[key]
const targetVal = origin[key]
// early return when targetVal === undefined
if (targetVal === undefined) {
carry[key] = newVal
return carry
}
// When newVal is an object do the merge recursively
if (isObject(newVal)) {
carry[key] = mergeRecursively(targetVal, newVal)
return carry
}
// all the rest
carry[key] = newVal
return carry
}, newObject)
}
FAQs
Merge objects & other types recursively. A simple & small integration.
The npm package merge-anything receives a total of 398,809 weekly downloads. As such, merge-anything popularity was classified as popular.
We found that merge-anything demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.