Socket
Socket
Sign inDemoInstall

merge-anything

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

merge-anything - npm Package Compare versions

Comparing version 2.2.0 to 2.2.1

10

dist/index.cjs.js

@@ -7,3 +7,3 @@ 'use strict';

// work directly on newComer if its not an object
if (!isWhat.isObject(newComer)) {
if (!isWhat.isPlainObject(newComer)) {
// extend merge rules

@@ -18,3 +18,3 @@ if (extensions && isWhat.isArray(extensions)) {

// define newObject to merge all values upon
var newObject = (isWhat.isObject(origin))
var newObject = (isWhat.isPlainObject(origin))
? Object.keys(origin)

@@ -33,3 +33,3 @@ .reduce(function (carry, key) {

var newVal = newComer[key];
var targetVal = (isWhat.isObject(origin))
var targetVal = (isWhat.isPlainObject(origin))
? origin[key]

@@ -49,3 +49,3 @@ : undefined;

// When newVal is an object do the merge recursively
if (isWhat.isObject(newVal)) {
if (isWhat.isPlainObject(newVal)) {
carry[key] = mergeRecursively(targetVal, newVal, extensions);

@@ -75,3 +75,3 @@ return carry;

var base = origin;
if (isWhat.isObject(origin) && origin.extensions && Object.keys(origin).length === 1) {
if (isWhat.isPlainObject(origin) && origin.extensions && Object.keys(origin).length === 1) {
base = {};

@@ -78,0 +78,0 @@ extensions = origin.extensions;

@@ -1,6 +0,6 @@

import { isArray, isObject } from 'is-what';
import { isArray, isPlainObject } from 'is-what';
function mergeRecursively(origin, newComer, extensions) {
// work directly on newComer if its not an object
if (!isObject(newComer)) {
if (!isPlainObject(newComer)) {
// extend merge rules

@@ -15,3 +15,3 @@ if (extensions && isArray(extensions)) {

// define newObject to merge all values upon
var newObject = (isObject(origin))
var newObject = (isPlainObject(origin))
? Object.keys(origin)

@@ -30,3 +30,3 @@ .reduce(function (carry, key) {

var newVal = newComer[key];
var targetVal = (isObject(origin))
var targetVal = (isPlainObject(origin))
? origin[key]

@@ -46,3 +46,3 @@ : undefined;

// When newVal is an object do the merge recursively
if (isObject(newVal)) {
if (isPlainObject(newVal)) {
carry[key] = mergeRecursively(targetVal, newVal, extensions);

@@ -72,3 +72,3 @@ return carry;

var base = origin;
if (isObject(origin) && origin.extensions && Object.keys(origin).length === 1) {
if (isPlainObject(origin) && origin.extensions && Object.keys(origin).length === 1) {
base = {};

@@ -75,0 +75,0 @@ extensions = origin.extensions;

{
"name": "merge-anything",
"version": "2.2.0",
"version": "2.2.1",
"description": "Merge objects & other types recursively. A simple & small integration.",

@@ -10,3 +10,4 @@ "main": "dist/index.cjs.js",

"test": "ava",
"build": "rollup -c build/rollup.js && npm run test"
"rollup": "rollup -c build/rollup.js",
"build": "npm run rollup && npm run test"
},

@@ -22,2 +23,4 @@ "repository": {

"deepmerge",
"object.assign",
"deep assign",
"recursively"

@@ -41,4 +44,4 @@ ],

"dependencies": {
"is-what": "^3.0.1"
"is-what": "^3.1.1"
}
}

@@ -11,6 +11,22 @@ # Merge anything 🥡

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**.
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 *all of them break things they are not supposed to break*... 😞
I was mostely looking for something that **keeps special objects (like JavaScript classes) "as is"** and doesn't mess with those losing the prototypes in many cases! With merge-anything your classes are just pasted with their prototypes without problem.
I was looking for:
- a simple merge function like `Object.assign()`
- supports merging of nested properties
- **does not break special class instances** ‼️
This last one is crucial! In JavaScript almost everything is _an object_, sure, but I don't want a merge function trying to merge eg. two `new Date()` instances! So many libraries use custom classes that create objects with special prototypes, and such objects all break when trying to merge them. So we gotta be careful!
merge-anything will merge objects and nested properties, but only as long as they're "plain objects". As soon as a sub-prop is not a "plain object" and has a special prototype, it will copy that instance over "as is". ♻️
## Meet the family
- [merge-anything](https://github.com/mesqueeb/merge-anything)
- [filter-anything](https://github.com/mesqueeb/filter-anything)
- [find-and-replace-anything](https://github.com/mesqueeb/find-and-replace-anything)
- copy-anything (WIP)
- [is-what](https://github.com/mesqueeb/is-what)
## Usage

@@ -24,7 +40,7 @@

const starter = {name: 'Squirtle', type: 'water'}
const newValues = {name: 'Warturtle', level: 16}
const newValues = {name: 'Wartortle', level: 16}
merge(starter, newValues, {is: 'cool'})
// returns {
// name: 'Warturtle',
// name: 'Wartortle',
// type: 'water,

@@ -59,3 +75,3 @@ // level: 16,

// but non-objects overwrite objects
merge({obj: {prop: 'a'}}, {obj: null}) // returns {prop: null}
merge({obj: {prop: 'a'}}, {obj: null}) // returns {obj: null}
merge({obj: 'a'}, 'b') // returns 'b'

@@ -74,2 +90,3 @@

### Concat arrays
Eg. merge-anything will overwrite arrays by default but you could change this logic to make it so it will concat the arrays.

@@ -109,14 +126,2 @@

One work around would be to add the custom merge rule:
```js
function cloneFn (originVal, targetVal) {
if (isObject(targetVal)) return JSON.parse(JSON.stringify(targetVal))
return targetVal
}
// and do
const merged = merge({extensions: [cloneFn]}, original, merged)
// However, this is slow when working with large sets of data!
```
## Source code

@@ -127,6 +132,8 @@

```js
import { isPlainObject } from 'is-what'
function mergeRecursively (origin, newComer) {
if (!isObject(newComer)) return newComer
if (!isPlainObject(newComer)) return newComer
// define newObject to merge all values upon
const newObject = (isObject(origin))
const newObject = (isPlainObject(origin))
? Object.keys(origin)

@@ -149,3 +156,3 @@ .reduce((carry, key) => {

// When newVal is an object do the merge recursively
if (isObject(newVal)) {
if (isPlainObject(newVal)) {
carry[key] = mergeRecursively(targetVal, newVal)

@@ -152,0 +159,0 @@ return carry

@@ -1,2 +0,2 @@

import { isArray, isObject } from 'is-what'
import { isArray, isPlainObject } from 'is-what'

@@ -11,3 +11,3 @@ type Extension = (param1: any, param2: any) => any

// work directly on newComer if its not an object
if (!isObject(newComer)) {
if (!isPlainObject(newComer)) {
// extend merge rules

@@ -22,3 +22,3 @@ if (extensions && isArray(extensions)) {

// define newObject to merge all values upon
const newObject = (isObject(origin))
const newObject = (isPlainObject(origin))
? Object.keys(origin)

@@ -36,3 +36,3 @@ .reduce((carry, key) => {

let newVal = newComer[key]
const targetVal = (isObject(origin))
const targetVal = (isPlainObject(origin))
? origin[key]

@@ -52,3 +52,3 @@ : undefined

// When newVal is an object do the merge recursively
if (isObject(newVal)) {
if (isPlainObject(newVal)) {
carry[key] = mergeRecursively(targetVal, newVal, extensions)

@@ -75,3 +75,3 @@ return carry

let base = origin
if (isObject(origin) && origin.extensions && Object.keys(origin).length === 1) {
if (isPlainObject(origin) && origin.extensions && Object.keys(origin).length === 1) {
base = {}

@@ -78,0 +78,0 @@ extensions = origin.extensions

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc