merge-anything
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -43,8 +43,14 @@ 'use strict'; | ||
function index (origin, newComer) { | ||
if (!isWhat.isObject(origin)) console.error('Trying to merge target:', newComer, 'onto a non-object:', origin); | ||
if (!isWhat.isObject(newComer)) console.error('Trying to merge a non-object:', newComer, 'onto:', origin); | ||
return mergeRecursively(origin, newComer); // return merge(origin, newComer) | ||
function index (origin) { | ||
for (var _len = arguments.length, newComers = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
newComers[_key - 1] = arguments[_key]; | ||
} | ||
return newComers.reduce(function (result, newComer) { | ||
if (!isWhat.isObject(result)) console.error('Trying to merge target:', newComer, 'onto a non-object:', result); | ||
if (!isWhat.isObject(newComer)) console.error('Trying to merge a non-object:', newComer, 'onto:', result); | ||
return mergeRecursively(result, newComer); | ||
}, origin); | ||
} | ||
exports.default = index; |
@@ -39,8 +39,14 @@ import { isObject } from 'is-what'; | ||
function index (origin, newComer) { | ||
if (!isObject(origin)) console.error('Trying to merge target:', newComer, 'onto a non-object:', origin); | ||
if (!isObject(newComer)) console.error('Trying to merge a non-object:', newComer, 'onto:', origin); | ||
return mergeRecursively(origin, newComer); // return merge(origin, newComer) | ||
function index (origin) { | ||
for (var _len = arguments.length, newComers = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
newComers[_key - 1] = arguments[_key]; | ||
} | ||
return newComers.reduce(function (result, newComer) { | ||
if (!isObject(result)) console.error('Trying to merge target:', newComer, 'onto a non-object:', result); | ||
if (!isObject(newComer)) console.error('Trying to merge a non-object:', newComer, 'onto:', result); | ||
return mergeRecursively(result, newComer); | ||
}, origin); | ||
} | ||
export default index; |
{ | ||
"name": "merge-anything", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Merge two objects recursively. A simple & small integration.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js", |
@@ -9,5 +9,57 @@ # Merge anything 🥡 | ||
It's literally just going through an object recursively like so: | ||
## Motivation | ||
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**. | ||
## Usage | ||
Pass the base param first and then an unlimited amount of | ||
```js | ||
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' | ||
// } | ||
``` | ||
## Rules | ||
```js | ||
// 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. | ||
## Source code | ||
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. | ||
```js | ||
function mergeRecursively (origin, newComer) { | ||
@@ -14,0 +66,0 @@ if (!isObject(newComer)) return newComer |
@@ -40,7 +40,8 @@ import { isObject } from 'is-what' | ||
*/ | ||
export default function (origin, newComer) { | ||
if (!isObject(origin)) console.error('Trying to merge target:', newComer, 'onto a non-object:', origin) | ||
if (!isObject(newComer)) console.error('Trying to merge a non-object:', newComer, 'onto:', origin) | ||
return mergeRecursively(origin, newComer) | ||
// return merge(origin, newComer) | ||
export default function (origin, ...newComers) { | ||
return newComers.reduce((result, newComer) => { | ||
if (!isObject(result)) console.error('Trying to merge target:', newComer, 'onto a non-object:', result) | ||
if (!isObject(newComer)) console.error('Trying to merge a non-object:', newComer, 'onto:', result) | ||
return mergeRecursively(result, newComer) | ||
}, origin) | ||
} |
@@ -5,3 +5,3 @@ import test from 'ava' | ||
test('merge-anything', t => { | ||
test('1. origin & target stays the same | 2. works with dates', t => { | ||
let res, origin, target | ||
@@ -12,7 +12,8 @@ const nd = new Date() | ||
res = merge(origin, target) | ||
t.true(isDate(res.dueDate)) | ||
t.is(res.body, 'a') | ||
t.deepEqual(res, {body: 'a', dueDate: nd}) | ||
t.deepEqual(origin, {body: 'a'}) | ||
t.deepEqual(target, {dueDate: nd}) | ||
}) | ||
test('1. works with multiple levels | 2. overwrites entire object with null', t => { | ||
let res, origin, target | ||
origin = { | ||
@@ -27,10 +28,14 @@ body: '', | ||
t.deepEqual(res, {body: {}, head: {}, toes: {big: true}, fingers: null}) | ||
}) | ||
test('overwrites string values', t => { | ||
let res, origin, target | ||
origin = {body: 'a'} | ||
target = {body: 'b'} | ||
res = merge(origin, target) | ||
t.is(res.body, 'b') | ||
t.deepEqual(res, {body: 'b'}) | ||
t.deepEqual(origin, {body: 'a'}) | ||
t.deepEqual(target, {body: 'b'}) | ||
}) | ||
test('works with very deep props & dates', t => { | ||
let res, origin, target | ||
const newDate = new Date() | ||
@@ -89,3 +94,5 @@ origin = { | ||
t.true(isDate(res.info.newDate)) | ||
}) | ||
test('1. does not overwrite origin prop if target prop is an empty object | 2. properly merges deep props', t => { | ||
let res, origin, target | ||
origin = { | ||
@@ -115,3 +122,5 @@ info: { | ||
}) | ||
}) | ||
test('overwrites any origin prop when target prop is an object with props', t => { | ||
let res, origin, target | ||
origin = { | ||
@@ -141,3 +150,5 @@ body: 'a', | ||
}) | ||
}) | ||
test('overwrites entire objects when target val is a simple string', t => { | ||
let res, origin, target | ||
origin = { | ||
@@ -157,1 +168,17 @@ body: 'a', | ||
}) | ||
test('works with unlimited depth', t => { | ||
let res, origin, t1, t2, t3, t4 | ||
const date = new Date() | ||
origin = {origin: 'a', t2: false, t3: {}, t4: 'false'} | ||
t1 = {t1: date} | ||
t2 = {t2: 'new'} | ||
t3 = {t3: 'new'} | ||
t4 = {t4: 'new', t3: {}} | ||
res = merge(origin, t1, t2, t3, t4) | ||
t.deepEqual(res, {origin: 'a', t1: date, t2: 'new', t3: {}, t4: 'new'}) | ||
t.deepEqual(origin, {origin: 'a', t2: false, t3: {}, t4: 'false'}) | ||
t.deepEqual(t1, {t1: date}) | ||
t.deepEqual(t2, {t2: 'new'}) | ||
t.deepEqual(t3, {t3: 'new'}) | ||
t.deepEqual(t4, {t4: 'new', t3: {}}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17770
435
95