deepmerge-json
Advanced tools
Comparing version 1.0.2 to 1.1.0
# Changelog | ||
## [1.0.1] - 2019-07-22 | ||
## [1.1.0] - 2020-03-27 | ||
### Added | ||
* New array operation: `$replace` | ||
### Fixed | ||
* Fixed security issues with some dependencies | ||
## [1.0.2] - 2019-07-22 | ||
### Fixed | ||
* Arrays of objects were not being merged | ||
### Added | ||
* $push tests | ||
* `$push` tests | ||
## [1.0.0] - 2019-07-07 | ||
Initial version | ||
[1.0.0]: https://github.com/kleber-swf/deepmerge-json/tree/v1.0.0 | ||
[1.0.0]: https://github.com/kleber-swf/deepmerge-json/tree/v1.0.0 | ||
[1.0.2]: https://github.com/kleber-swf/deepmerge-json/tree/v1.0.2 | ||
[1.1.0]: https://github.com/kleber-swf/deepmerge-json/tree/v1.1.0 |
@@ -5,3 +5,3 @@ (function (global, factory) { | ||
(global = global || self, global.merge = factory()); | ||
}(this, function () { 'use strict'; | ||
}(this, (function () { 'use strict'; | ||
@@ -12,23 +12,29 @@ const directReplace = (_, pos) => pos; | ||
const mergeObjects = function (pre, pos) { | ||
const mergeObjects = function(pre, pos) { | ||
pre = Object.assign({}, pre); | ||
Object.keys(pos).forEach(k => | ||
pre[k] = merge(pre[k], pos[k])); | ||
Object.keys(pos).forEach(k => (pre[k] = merge(pre[k], pos[k]))); | ||
return pre; | ||
}; | ||
const mergeArrays = function (pre, pos) { | ||
const mergeArrays = function(pre, pos) { | ||
pre = pre.slice(); | ||
pos.forEach((v, i) => pre[i] = merge(pre[i], v)); | ||
pos.forEach((v, i) => (pre[i] = merge(pre[i], v))); | ||
return pre; | ||
}; | ||
const mergeArrayWithParams = function (pre, pos) { | ||
const mergeArrayWithParams = function(pre, pos) { | ||
pre = pre.slice(); | ||
const key = Object.keys(pos)[0]; // (x_x) This is ugly | ||
if (key in arrayMergeFn) | ||
return arrayMergeFn[key](pre, pos[key]); | ||
if (key in arrayMergeFn) return arrayMergeFn[key](pre, pos[key]); | ||
return pos; | ||
}; | ||
const indexedReplace = function(pre, pos) { | ||
pre = pre.slice(); | ||
Object.keys(pos).forEach(k => { | ||
pre[k] = merge(pre[k], pos[k]); | ||
}); | ||
return pre; | ||
}; | ||
const arrayMergeFn = { | ||
@@ -38,2 +44,3 @@ $push: (pre, pos) => pre.concat(pos), | ||
$prepend: (pre, pos) => pos.concat(pre), | ||
$replace: indexedReplace, | ||
$set: cloneArray | ||
@@ -58,4 +65,4 @@ }; | ||
if (!pos) return pos; | ||
const tt = Array.isArray(pre) ? 'a' : (typeof pre === 'object' ? 'o' : 'b'); | ||
const st = Array.isArray(pos) ? 'a' : (typeof pos === 'object' ? 'o' : 'b'); | ||
const tt = Array.isArray(pre) ? 'a' : typeof pre === 'object' ? 'o' : 'b'; | ||
const st = Array.isArray(pos) ? 'a' : typeof pos === 'object' ? 'o' : 'b'; | ||
return fn[tt + st](pre, pos); | ||
@@ -66,3 +73,3 @@ } | ||
})); | ||
}))); | ||
//# sourceMappingURL=index.js.map |
38
index.js
@@ -5,23 +5,29 @@ const directReplace = (_, pos) => pos; | ||
const mergeObjects = function (pre, pos) { | ||
const mergeObjects = function(pre, pos) { | ||
pre = Object.assign({}, pre); | ||
Object.keys(pos).forEach(k => | ||
pre[k] = merge(pre[k], pos[k])); | ||
Object.keys(pos).forEach(k => (pre[k] = merge(pre[k], pos[k]))); | ||
return pre; | ||
} | ||
}; | ||
const mergeArrays = function (pre, pos) { | ||
const mergeArrays = function(pre, pos) { | ||
pre = pre.slice(); | ||
pos.forEach((v, i) => pre[i] = merge(pre[i], v)); | ||
pos.forEach((v, i) => (pre[i] = merge(pre[i], v))); | ||
return pre; | ||
} | ||
}; | ||
const mergeArrayWithParams = function (pre, pos) { | ||
const mergeArrayWithParams = function(pre, pos) { | ||
pre = pre.slice(); | ||
const key = Object.keys(pos)[0]; // (x_x) This is ugly | ||
if (key in arrayMergeFn) | ||
return arrayMergeFn[key](pre, pos[key]); | ||
if (key in arrayMergeFn) return arrayMergeFn[key](pre, pos[key]); | ||
return pos; | ||
} | ||
}; | ||
const indexedReplace = function(pre, pos) { | ||
pre = pre.slice(); | ||
Object.keys(pos).forEach(k => { | ||
pre[k] = merge(pre[k], pos[k]); | ||
}); | ||
return pre; | ||
}; | ||
const arrayMergeFn = { | ||
@@ -31,4 +37,5 @@ $push: (pre, pos) => pre.concat(pos), | ||
$prepend: (pre, pos) => pos.concat(pre), | ||
$replace: indexedReplace, | ||
$set: cloneArray | ||
} | ||
}; | ||
@@ -47,8 +54,8 @@ const fn = { | ||
ba: cloneArray | ||
} | ||
}; | ||
function merge(pre, pos) { | ||
if (!pos) return pos; | ||
const tt = Array.isArray(pre) ? 'a' : (typeof pre === 'object' ? 'o' : 'b'); | ||
const st = Array.isArray(pos) ? 'a' : (typeof pos === 'object' ? 'o' : 'b'); | ||
const tt = Array.isArray(pre) ? 'a' : typeof pre === 'object' ? 'o' : 'b'; | ||
const st = Array.isArray(pos) ? 'a' : typeof pos === 'object' ? 'o' : 'b'; | ||
return fn[tt + st](pre, pos); | ||
@@ -58,2 +65,1 @@ } | ||
export default merge; | ||
{ | ||
"name": "deepmerge-json", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A library to deep merge json files with array operations", | ||
@@ -32,6 +32,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"mocha": "^6.1.4", | ||
"rollup": "^1.16.6" | ||
"mocha": "^6.2.3", | ||
"rollup": "^1.32.1" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -198,2 +198,16 @@ # Deepmerge Json | ||
#### $replace | ||
Use `$replace` to replace or add indexed elements by their indexes. | ||
```js | ||
const left = [10, 20, 30]; | ||
const right = { $replace: { 0: 100, 2: 300, 4: 400 } }; | ||
const result = merge(left, right); | ||
// Result (note that the element with index 3 was never given) | ||
[100, 20, 300, , 400] | ||
``` | ||
#### Skipping elements | ||
@@ -200,0 +214,0 @@ |
Sorry, the diff of this file is not supported yet
15594
132
237