From e7f801783187c75eecde9b8ac3e3c0d7cf8e8d19 Mon Sep 17 00:00:00 2001 From: Deepu KS Date: Tue, 3 Nov 2015 20:15:27 +0800 Subject: [PATCH] polish --- angular-object-diff.js | 118 +++++++++++++++++++++++++++++++---------- 1 file changed, 89 insertions(+), 29 deletions(-) diff --git a/angular-object-diff.js b/angular-object-diff.js index 5d4361d..e4b9b70 100644 --- a/angular-object-diff.js +++ b/angular-object-diff.js @@ -3,29 +3,58 @@ angular .module('ds.objectDiff', []) - .factory('ObjectDiff', objectDiff); + .factory('ObjectDiff', objectDiff) + .filter('toJsonView', toJsonViewFilter) + .filter('toJsonDiffView', toJsonDiffViewFilter) + .filter('objToJsonView', objToJsonViewFilter); + + toJsonViewFilter.$inject = ['ObjectDiff']; + toJsonDiffViewFilter.$inject = ['ObjectDiff']; + objToJsonViewFilter.$inject = ['ObjectDiff']; /* service implementation */ function objectDiff() { - var service = { - diff: diff, - diffOwnProperties: diffOwnProperties, - toJsonView: formatToJsonXMLString, - objToJsonView: formatObjToJsonXMLString, - toJsonDiffView: formatChangesToXMLString - }; + + var openChar = '{', + closeChar = '}', + service = { + setOpenChar: setOpenChar, + setCloseChar: setCloseChar, + diff: diff, + diffOwnProperties: diffOwnProperties, + toJsonView: formatToJsonXMLString, + objToJsonView: formatObjToJsonXMLString, + toJsonDiffView: formatChangesToXMLString + }; + return service; + /* service methods */ + /** + * @param char + */ + function setOpenChar(char) { + openChar = char; + } + + /** + * @param char + */ + function setCloseChar(char) { + closeChar = char; + } + /** * diff between object a and b * @param {Object} a * @param {Object} b + * @param shallow * @param isOwn * @return {Object} */ - function diff(a, b, isOwn) { + function diff(a, b, shallow, isOwn) { if (a === b) { return equalObj(a); @@ -39,7 +68,7 @@ if (a[key] === b[key]) { diffValue[key] = equalObj(a[key]); } else { - if (isValidAttr(a[key], b[key])) { + if (!shallow && isValidAttr(a[key], b[key])) { var valueDiff = diff(a[key], b[key], isOwn); if (valueDiff.changed == 'equal') { diffValue[key] = equalObj(a[key]); @@ -91,29 +120,31 @@ * @param {Object} a * @param {Object} b * @return {Object} + * @param deep */ - function diffOwnProperties(a, b) { - return diff(a, b, true); + function diffOwnProperties(a, b, deep) { + return diff(a, b, deep, true); } /** * Convert to a readable xml/html Json structure * @param {Object} changes * @return {string} + * @param shallow */ - function formatToJsonXMLString(changes) { + function formatToJsonXMLString(changes, shallow) { var properties = []; var diff = changes.value; if (changes.changed == 'equal') { - return inspect(diff); + return inspect(diff, shallow); } for (var key in diff) { - properties.push(formatChange(key, diff[key])); + properties.push(formatChange(key, diff[key], shallow)); } - return '{\n
' + properties.join(',\n') + '\n
}'; + return '' + openChar + '\n
' + properties.join(',\n') + '\n
' + openChar + ''; } @@ -121,17 +152,19 @@ * Convert to a readable xml/html Json structure * @return {string} * @param obj + * @param shallow */ - function formatObjToJsonXMLString(obj) { - return inspect(obj); + function formatObjToJsonXMLString(obj, shallow) { + return inspect(obj, shallow); } /** * Convert to a readable xml/html Json structure * @param {Object} changes * @return {string} + * @param shallow */ - function formatChangesToXMLString(changes) { + function formatChangesToXMLString(changes, shallow) { var properties = []; if (changes.changed == 'equal') { @@ -143,10 +176,10 @@ for (var key in diff) { var changed = diff[key].changed; if (changed !== 'equal') - properties.push(formatChange(key, diff[key], true)); + properties.push(formatChange(key, diff[key], shallow, true)); } - return '{\n
' + properties.join(',\n') + '\n
}'; + return '' + openChar + '\n
' + properties.join(',\n') + '\n
' + closeChar + ''; } @@ -176,8 +209,10 @@ * @param key * @param diffItem * @returns {*} + * @param shallow + * @param diffOnly */ - function formatChange(key, diffItem, diffOnly) { + function formatChange(key, diffItem, shallow, diffOnly) { var changed = diffItem.changed; var property; switch (changed) { @@ -201,7 +236,7 @@ break; case 'object change': - property = (stringifyObjectKey(key) + ': ' + ( diffOnly ? formatChangesToXMLString(diffItem) : formatToJsonXMLString(diffItem))); + property = shallow ? '' : (stringifyObjectKey(key) + ': ' + ( diffOnly ? formatChangesToXMLString(diffItem) : formatToJsonXMLString(diffItem))); break; } @@ -229,30 +264,36 @@ /** * @param {Object} obj * @return {string} + * @param shallow */ - function inspect(obj) { + function inspect(obj, shallow) { - return _inspect('', obj); + return _inspect('', obj, shallow); /** * @param {string} accumulator * @param {object} obj * @see http://jsperf.com/continuation-passing-style/3 * @return {string} + * @param shallow */ - function _inspect(accumulator, obj) { + function _inspect(accumulator, obj, shallow) { switch (typeof obj) { case 'object': if (!obj) { accumulator += 'null'; break; } + if (shallow) { + accumulator += '[object]'; + break; + } var keys = Object.keys(obj); var length = keys.length; if (length === 0) { - accumulator += '{}'; + accumulator += '' + openChar + closeChar + ''; } else { - accumulator += '{\n
'; + accumulator += '' + openChar + '\n
'; for (var i = 0; i < length; i++) { var key = keys[i]; accumulator = _inspect(accumulator + stringifyObjectKey(escapeHTML(key)) + ': ', obj[key]); @@ -260,7 +301,7 @@ accumulator += ',\n'; } } - accumulator += '\n
}' + accumulator += '\n
' + closeChar + '' } break; @@ -280,4 +321,23 @@ } } } + + /* filter implementation */ + function toJsonViewFilter(ObjectDiff) { + return function (value) { + return ObjectDiff.toJsonView(value); + }; + } + + function toJsonDiffViewFilter(ObjectDiff) { + return function (value) { + return ObjectDiff.toJsonDiffView(value); + }; + } + + function objToJsonViewFilter(ObjectDiff) { + return function (value) { + return ObjectDiff.objToJsonView(value); + }; + } })();