UNPKG

@viewdo/dxp-story-cli

Version:
40 lines (33 loc) 1.29 kB
const merge = require('lodash/merge'); const jsondiffpatch = require('jsondiffpatch'); const isObject = require('lodash/isObject'); const isFunction = require('lodash/isFunction'); module.exports = (destination, source, mergeKey = "id") => { const mergedDiff = jsondiffpatch.create({ objectHash: function (obj, index) { return obj[mergeKey]; } }); const mergeJson = (context) => { const { left, right } = context; if (typeof context.right === 'undefined') { context.setResult([context.left]).exit(); return; } if (isObject(left) && isObject(right)) { const leftKeyProperty = left[mergeKey]; const rightKeyProperty = right[mergeKey]; if (leftKeyProperty && rightKeyProperty && (leftKeyProperty === rightKeyProperty)) { context.setResult([merge(left, right)]).exit(); } } if(isFunction(context.left) || isFunction(context.right)){ console.log(context); } } mergeJson.filterName = 'mergeJson' mergedDiff.processor.pipes.diff.before('trivial', mergeJson); const delta = mergedDiff.diff(destination, source); mergedDiff.patch(destination, delta); return destination; }