@ebflat9/fp
Advanced tools
Comparing version 1.1.22 to 1.1.23
{ | ||
"name": "@ebflat9/fp", | ||
"version": "1.1.22", | ||
"version": "1.1.23", | ||
"description": "my fp utils", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -433,2 +433,23 @@ /** | ||
/** | ||
* Merge | ||
* @param {object} a - Object to merge into | ||
* @param {object} b - Object with diffs to merge | ||
* @return {object} c - Result of merge | ||
*/ | ||
export function merge(a, b) { | ||
if (!a && b) return b | ||
if (isArray(b)) { | ||
return b.map((value, i) => merge(a[i], value)) | ||
} | ||
if (isObject(b)) { | ||
const result = deepCopy(a) | ||
for (const key of Reflect.ownKeys(b)) { | ||
result[key] = merge(a[key], b[key]) | ||
} | ||
return result | ||
} | ||
return b | ||
} | ||
/** | ||
* Stringifying functions | ||
@@ -435,0 +456,0 @@ * Provides helper functions to stringify and parse JSON, along with numbers |
@@ -702,2 +702,51 @@ import * as combinators from '../src/combinators.js' | ||
}) | ||
describe('merge', function () { | ||
it('should merge arrays', function () { | ||
const a = [1, 2, 3] | ||
const b = [1, 2, 4] | ||
assert.deepEqual(combinators.merge(a, b), [1, 2, 4]) | ||
}) | ||
it('should merge objects', function () { | ||
const a = { | ||
name: 'jim', | ||
age: 15, | ||
address: { | ||
city: 'Hellosville', | ||
}, | ||
} | ||
const b = { | ||
name: 'john', | ||
likes: ['cats', 'birds'], | ||
address: { | ||
street: '123 Fake st', | ||
}, | ||
} | ||
assert.deepEqual(combinators.merge(a, b), { | ||
name: 'john', | ||
age: 15, | ||
likes: ['cats', 'birds'], | ||
address: { | ||
city: 'Hellosville', | ||
street: '123 Fake st', | ||
}, | ||
}) | ||
}) | ||
}) | ||
describe('merge and diff', function () { | ||
it('should diff and merge resulting in deepEqual objects', function () { | ||
const a = { | ||
name: 'Jim', | ||
catName: 'Steve Lu-cat-ther', | ||
} | ||
const b = { | ||
name: 'Jim', | ||
age: 16, | ||
catName: 'Steve Lu-cat-ther', | ||
} | ||
assert.deepEqual(combinators.diff(a, b), { age: 16 }) | ||
assert.deepEqual(combinators.merge(b, combinators.diff(a, b)), b) | ||
}) | ||
}) | ||
}) |
238208
7659