diff --git a/gendiff.js b/bin/gendiff.js similarity index 90% rename from gendiff.js rename to bin/gendiff.js index b57320c..a8cb4dd 100755 --- a/gendiff.js +++ b/bin/gendiff.js @@ -1,7 +1,7 @@ #!/usr/bin/env node import { program } from 'commander'; -import genDiff from './src/index.js'; +import genDiff from '../src/index.js'; program .name('gendiff') diff --git a/package.json b/package.json index bb83ace..408fadd 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "test": "npx jest" }, "bin": { - "gendiff": "gendiff.js" + "gendiff": "bin/gendiff.js" }, "keywords": [ "study", diff --git a/src/index.js b/src/index.js index 94f371d..aa25d0a 100644 --- a/src/index.js +++ b/src/index.js @@ -4,15 +4,16 @@ import getParsedFile from './parsers.js'; const getStylish = (data) => { const iter = (node, depth) => { // if (!Array.isArray(node)) { + if (!_.isPlainObject(node) && !Array.isArray(node)) { return node; } + // if (_.isPlainObject(node)) { // return iter([node], depth + 1); // } - const spacer = ' '; const spacerCount = 4; const indentCount = (spacerCount * depth) - 2; @@ -33,6 +34,9 @@ const getStylish = (data) => { const [currentStat, key] = Object.keys(obj); const value = obj[key]; const stat = obj[currentStat]; + if (!_.has(obj, 'stat')) { + return obj; + } if (stat === 'added') { return `${currentIndent}${statSign(stat)}${key}: ${iter(value, depth + 1)}`; @@ -44,6 +48,15 @@ const getStylish = (data) => { const [value1, value2] = value; const [deleted, added] = statSign(stat); + const processValue = (val) => { + if (_.isPlainObject(val)) { + const entries = Object.entries(val); + const result = entries.map(([key, value]) => `${key}: ${processValue(value)}`); + return `{\n${result.join(',\n')}\n}`; + } + return val; + } + // FIXME -- возвращает неправильный результат: // `nest: [object Object]` вместо `nest: { key: value }` @@ -52,9 +65,13 @@ const getStylish = (data) => { // но если так сделать, то вылетает ошибка: // TypeError: node.map is not a function - // потому что я пытаюсь замэпить `{ key: value }`, - // когда итеративно проваливаюсь в value1 - return `${currentIndent}${deleted}${key}: ${value1}${br}${currentIndent}${added}${key}: ${value2}`; + // потому что я пытаюсь замэпить объект `{ key: value }`, + // когда итеративно проваливаюсь в value1 и value2 + + // return `${currentIndent}${deleted}${key}: ${value1}${br}${currentIndent}${added}${key}: ${value2}`; + return `${currentIndent}${deleted}${key}: ${processValue(value1)}${br}${currentIndent}${added}${key}: ${processValue(value2)}`; + // return `${currentIndent}${deleted}${key}: ${iter(value1, depth + 1)}${br}${currentIndent}${added}${key}: ${iter(value2, depth + 1)}`; + // return `${currentIndent}${deleted}${key}: ${processValue(value1)}${br}${currentIndent}${added}${key}: ${processValue(value2)}`; } if (stat === 'unchanged') { return `${currentIndent}${statSign(stat)}${key}: ${iter(value, depth + 1)}`; @@ -64,7 +81,8 @@ const getStylish = (data) => { } }); - return ['{', ...arr, `${bracketIndent}}`].join('\n'); + return ['{', ...arr, `${bracketIndent}}`] + .join('\n'); }; return iter(data, 1); @@ -171,6 +189,7 @@ const genDiff = (file1, file2, formatter = 'stylish') => { console.log('>> genDiff:'); // const testResult = JSON.stringify(genDiff('__fixtures__/gdFile1.json', '__fixtures__/gdFile2.json'), null, ' '); +// const testResult = JSON.stringify(genDiff('__fixtures__/file1.json', '__fixtures__/file2.json'), null, 2); // console.log(testResult); // console.log(genDiff('__fixtures__/gdFile1.json', '__fixtures__/gdFile2.json'));