function logDiff(log) { var changes = diff(startDocs || firstDocs, endDocs || lastDocs); log.info(options); log.info(changes); }
/** * This function uses the objectdiff library to diff two objects or * literals and generates a nice-looking diff (inspired by file diffs) * from the result. * * @param a {*} diff operand 1, can be object or literal. * @param b {*} diff operand 2, can be object or literal. * @param colors {boolean} whether to colorize the output. * @returns {Array} empty if equal, otherwise a list of changes. * @private */ function _generateObjectDiff(a, b, colors) { var result = []; var numberPat = /^\d+$/; // Format strings var changeStr = ['* %s = %s -> %s', '\x1B[36m* %s\x1B[0m = \x1B[31m%s\x1B[0m -> \x1B[32m%s\x1B[0m']; var changeStr2 = ['* %s -> %s', '\x1B[36m*\x1B[0m \x1B[31m%s\x1B[0m -> \x1B[32m%s\x1B[0m']; var removeStr = ['- %s = %s', '\x1B[31m- %s\x1B[0m = \x1B[31m%s\x1B[0m']; var addStr = ['+ %s = %s', '\x1B[32m+ %s\x1B[0m = \x1B[32m%s\x1B[0m']; var idx = colors ? 1 : 0; // Traverse the diff object to find all changes. function traverse(node, path) { var keys, i; if (node.changed === 'object change') { keys = Object.keys(node.value); for (i = 0; i < keys.length; i++) { var newPath; // If the key is a number, it's probably an array index if (numberPat.test(keys[i])) { newPath = '[' + keys[i] + ']'; if (path) { newPath = path + newPath; } } else { newPath = path ? path + '.' + keys[i] : keys[i]; } traverse(node.value[keys[i]], newPath); } } else if (node.changed === 'primitive change') { result.push(_lineBreak(util.format(changeStr[idx] , path , util.inspect(node.removed) , util.inspect(node.added)))); } else if (node.changed === 'removed') { result.push(_lineBreak(util.format(removeStr[idx] , path , util.inspect(node.value)))); } else if (node.changed === 'added') { result.push(_lineBreak(util.format(addStr[idx] , path , util.inspect(node.value)))); } } // objectdiff only supports comparing two objects. if (a instanceof Object && b instanceof Object) { var diff = objectDiff.diff(a, b); traverse(diff, ''); } else { if (a !== b) { result.push(_lineBreak(util.format(changeStr2[idx] , util.inspect(a) , util.inspect(b)))); } } return result; }