Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Created August 12, 2016 08:50
Show Gist options
  • Save wmakeev/71cce62b8c17b1519c7a96025149084c to your computer and use it in GitHub Desktop.
Save wmakeev/71cce62b8c17b1519c7a96025149084c to your computer and use it in GitHub Desktop.
JSON diff #json #diff
'use strict'
const jsondiffpatch = require('jsondiffpatch')
const stringify = require('json-stable-stringify')
const objectHashFunctions = [
obj => obj.uuid,
obj => obj.id,
obj => stringify(obj)
]
module.exports = jsondiffpatch.create({
// used to match objects when diffing arrays, by default only === operator is used
objectHash: function (entity) {
// this function is used only to when objects are not equal by ref
let hash
for (let i = 0; i < objectHashFunctions.length; i++) {
try {
hash = objectHashFunctions[i](entity)
if (hash) { return hash }
} catch (e) {}
}
},
arrays: {
// default true, detect items moved inside the array
// (otherwise they will be registered as remove+add)
detectMove: true,
// default false, the value of items moved is not included in deltas
includeValueOnMove: false
},
textDiff: {
// default 60, minimum string length (left and right sides)
// to use text diff algorythm: google-diff-match-patch
minLength: 60
},
propertyFilter: function (name, context) {
// this optional function can be specified to ignore object properties (eg. volatile data)
// name: property name, present in either context.left or context.right objects
// context: the diff context (has context.left and context.right objects)
// Изменения в updatedBy создают слишком большой diff (вынесего в родительский объект)
return ['_id', '_rev', 'updatedBy'].indexOf(name) === -1
},
cloneDiffValues: false /* default false. if true, values in the obtained delta will be cloned,
to ensure delta keeps no references to left or right objects. this becomes useful
if you're diffing and patching the same objects multiple times without serializing deltas.
instead of true, a function can be specified here to provide a custom clone(value)
*/
})
'use strict'
const jsonDiffFormat = require('jsondiffpatch').formatters.console.format
const diffPatch = require('./diff-patch')
let diff = diffPatch.diff(updated, current)
console.log(jsonDiffFormat(diff))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment