diff --git a/src/compare.js b/src/compare.js index 2205fb9..14c4a66 100644 --- a/src/compare.js +++ b/src/compare.js @@ -1,32 +1,64 @@ +// import _ from 'lodash'; + +// const buildDiff = (obj1, obj2) => { +// const keys = _.union(Object.keys(obj1), Object.keys(obj2)).sort(); // eslint-disable-line +// const ast = keys.map((key) => { +// if (!_.has(obj1, key)) { +// return { key, state: 'added', value: obj2[key] }; +// } +// if (!_.has(obj2, key)) { +// return { key, state: 'deleted', value: obj1[key] }; +// } + +// if (_.isObject(obj1[key]) && _.isObject(obj2[key])) { +// const children = buildDiff(obj1[key], obj2[key]); +// return { key, state: 'nested', children }; +// } + +// if (obj1[key] === obj2[key]) { +// return { key, state: 'unchanged', value: obj2[key] }; +// } + +// return { +// key, +// state: 'changed', +// oldValue: obj1[key], +// newValue: obj2[key], +// }; +// }); +// return ast; +// }; + +// export default buildDiff; + import _ from 'lodash'; +const getKeyState = (obj1, obj2, key) => { + if (!_.has(obj1, key)) { + return { key, state: 'added', value: obj2[key] }; + } + if (!_.has(obj2, key)) { + return { key, state: 'deleted', value: obj1[key] }; + } + if (_.isObject(obj1[key]) && _.isObject(obj2[key])) { + const children = buildDiff(obj1[key], obj2[key]); + return { key, state: 'nested', children }; + } + if (obj1[key] === obj2[key]) { + return { key, state: 'unchanged', value: obj2[key] }; + } + return { + key, + state: 'changed', + oldValue: obj1[key], + newValue: obj2[key], + }; +}; + const buildDiff = (obj1, obj2) => { - const keys = _.union(Object.keys(obj1), Object.keys(obj2)).sort(); // eslint-disable-line - const ast = keys.map((key) => { - if (!_.has(obj1, key)) { - return { key, state: 'added', value: obj2[key] }; - } - if (!_.has(obj2, key)) { - return { key, state: 'deleted', value: obj1[key] }; - } - - if (_.isObject(obj1[key]) && _.isObject(obj2[key])) { - const children = buildDiff(obj1[key], obj2[key]); - return { key, state: 'nested', children }; - } - - if (obj1[key] === obj2[key]) { - return { key, state: 'unchanged', value: obj2[key] }; - } - - return { - key, - state: 'changed', - oldValue: obj1[key], - newValue: obj2[key], - }; - }); + const keys = _.union(Object.keys(obj1), Object.keys(obj2)).sort(); + const ast = keys.map((key) => getKeyState(obj1, obj2, key)); return ast; }; -export default buildDiff; +export default buildDiff; \ No newline at end of file diff --git a/src/formatters/stylish.js b/src/formatters/stylish.js index 48c5153..96d73fa 100644 --- a/src/formatters/stylish.js +++ b/src/formatters/stylish.js @@ -17,37 +17,41 @@ const formatValue = (values, depth) => { return `${values}`; }; -const renderStylish = (content) => { - const iter = (node, depth) => { - if (node.state === 'added') { - const indent = makeIndent(depth, '+'); - const formattedValue = formatValue(node.value, depth); - return `${indent}${node.key}: ${formattedValue}`; - } - if (node.state === 'deleted') { - const indent = makeIndent(depth, '-'); - const formattedValue = formatValue(node.value, depth); - return `${indent}${node.key}: ${formattedValue}`; - } - if (node.state === 'unchanged') { +const renderNode = (node, depth, indentMarker = null) => { + const indent = makeIndent(depth, indentMarker); + const formattedValue = formatValue(node.value, depth); + return `${indent}${node.key}: ${formattedValue}`; +}; + +const renderChangedNode = (node, depth) => { + const deleteIndent = makeIndent(depth, '-'); + const addedIndent = makeIndent(depth, '+'); + const formattedOldValue = formatValue(node.oldValue, depth); + const formattedNewValue = formatValue(node.newValue, depth); + return [ + `${deleteIndent}${node.key}: ${formattedOldValue}`, + `${addedIndent}${node.key}: ${formattedNewValue}`, + ]; +}; + +const iter = (node, depth) => { + switch (node.state) { + case 'added': + return renderNode(node, depth, '+'); + case 'deleted': + return renderNode(node, depth, '-'); + case 'unchanged': + return renderNode(node, depth); + case 'changed': + return renderChangedNode(node, depth); + default: const indent = makeIndent(depth); - const formattedValue = formatValue(node.value, depth); - return `${indent}${node.key}: ${formattedValue}`; - } - if (node.state === 'changed') { - const deleteIndent = makeIndent(depth, '-'); - const addedIndent = makeIndent(depth, '+'); - const formattedNewValue = formatValue(node.newValue, depth); - const formattedOldValue = formatValue(node.oldValue, depth); - return [ - `${deleteIndent}${node.key}: ${formattedOldValue}`, - `${addedIndent}${node.key}: ${formattedNewValue}`, - ]; - } - const indent = makeIndent(depth); - const innerTree = node.children.flatMap((children) => iter(children, depth + 1)).join('\n'); - return `${indent}${node.key}: {\n${innerTree}\n${indent}}`; - }; + const innerTree = node.children.flatMap((child) => iter(child, depth + 1)).join('\n'); + return `${indent}${node.key}: {\n${innerTree}\n${indent}}`; + } +}; + +const renderStylish = (content) => { const tree = content.flatMap((node) => iter(node, 1)).join('\n'); return `{\n${tree}\n${makeIndent()}}`; };