From 82b7683ceed48796c0bcd426bc71d23aa4bd67d4 Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 24 Oct 2023 21:26:13 +0700 Subject: [PATCH] finish plain.js --- README.md | 2 +- __tests__/index.test.js | 132 ++++++++++++++++++++------------------- a.out | 0 bin/gendiff.js | 4 +- package.json | 2 +- src/buildTree.js | 10 +-- src/formatter/index.js | 2 +- src/formatter/plain.js | 31 +++++---- src/formatter/stylish.js | 22 ++++--- 9 files changed, 107 insertions(+), 98 deletions(-) create mode 100644 a.out diff --git a/README.md b/README.md index ce46e75..d41c90e 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,4 @@ [![Test Coverage](https://api.codeclimate.com/v1/badges/0746c48ef8ae17b78479/test_coverage)](https://codeclimate.com/github/igshipilov/frontend-project-46/test_coverage) ### How it works - + diff --git a/__tests__/index.test.js b/__tests__/index.test.js index a5e578b..812710a 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -47,71 +47,73 @@ const readFile = (filename) => fs.readFileSync(getFixturePath(filename), 'utf-8' // expect(actual).toEqual(desired); // }); -// describe('stylish', () => { -// test('compare complex JSON-files', () => { -// const desired = readFile('expected.txt'); -// const filepath1 = getFixturePath('file1.json'); -// const filepath2 = getFixturePath('file2.json'); + + +describe('stylish', () => { + test('compare complex JSON-files', () => { + const desired = readFile('expected.txt'); + const filepath1 = getFixturePath('file1.json'); + const filepath2 = getFixturePath('file2.json'); -// const actual = genDiff(filepath1, filepath2); + const actual = genDiff(filepath1, filepath2); -// expect(actual).toEqual(desired); -// }); + expect(actual).toEqual(desired); + }); -// test('compare YAML-files', () => { -// const desired = readFile('expected.txt'); -// const filepath1 = getFixturePath('file1.yaml'); -// const filepath2 = getFixturePath('file2.yml'); + test('compare YAML-files', () => { + const desired = readFile('expected.txt'); + const filepath1 = getFixturePath('file1.yaml'); + const filepath2 = getFixturePath('file2.yml'); -// const actual = genDiff(filepath1, filepath2); + const actual = genDiff(filepath1, filepath2); -// expect(actual).toEqual(desired); -// }); + expect(actual).toEqual(desired); + }); -// test('compare JSON-files with YAML-files', () => { -// const desired = readFile('expected.txt'); -// const filepath1 = getFixturePath('file1.json'); -// const filepath2 = getFixturePath('file2.yml'); + test('compare JSON-files with YAML-files', () => { + const desired = readFile('expected.txt'); + const filepath1 = getFixturePath('file1.json'); + const filepath2 = getFixturePath('file2.yml'); -// const actual = genDiff(filepath1, filepath2); + const actual = genDiff(filepath1, filepath2); -// expect(actual).toEqual(desired); -// }); + expect(actual).toEqual(desired); + }); -// test('compare file without format', () => { -// const desired = readFile('expected.txt'); -// const filepath1 = getFixturePath('file1'); -// const filepath2 = getFixturePath('file2.yml'); + test('compare file without format', () => { + const desired = readFile('expected.txt'); + const filepath1 = getFixturePath('file1'); + const filepath2 = getFixturePath('file2.yml'); -// const actual = genDiff(filepath1, filepath2); + const actual = genDiff(filepath1, filepath2); -// expect(actual).toEqual(desired); -// }); + expect(actual).toEqual(desired); + }); -// // test('compare empty files', () => { -// // const desired = readFile('expectedEmpty.txt'); -// // const filepath1 = getFixturePath('fileEmpty1.json'); -// // const filepath2 = getFixturePath('fileEmpty2'); + // test('compare empty files', () => { + // const desired = readFile('expectedEmpty.txt'); + // const filepath1 = getFixturePath('fileEmpty1.json'); + // const filepath2 = getFixturePath('fileEmpty2'); -// // const actual = genDiff(filepath1, filepath2); + // const actual = genDiff(filepath1, filepath2); -// // expect(actual).toEqual(desired); -// // }); + // expect(actual).toEqual(desired); + // }); -// // test('compare empty JSON (empty objects)', () => { -// // const desired = readFile('expectedEmpty.txt'); -// // const file1 = {}; -// // const file2 = {}; + // test('compare empty JSON (empty objects)', () => { + // const desired = readFile('expectedEmpty.txt'); + // const file1 = {}; + // const file2 = {}; -// // const actual = genDiff(file1, file2); + // const actual = genDiff(file1, file2); -// // expect(actual).toEqual(desired); -// // }); + // expect(actual).toEqual(desired); + // }); -// }); +}); @@ -127,35 +129,35 @@ describe('plain', () => { }); - // test('compare YAML-files', () => { - // const desired = readFile('expectedPlain.txt'); - // const filepath1 = getFixturePath('file1.yaml'); - // const filepath2 = getFixturePath('file2.yml'); + test('compare YAML-files', () => { + const desired = readFile('expectedPlain.txt'); + const filepath1 = getFixturePath('file1.yaml'); + const filepath2 = getFixturePath('file2.yml'); - // const actual = genDiff(filepath1, filepath2, 'plain'); + const actual = genDiff(filepath1, filepath2, 'plain'); - // expect(actual).toEqual(desired); - // }); + expect(actual).toEqual(desired); + }); - // test('compare JSON-files with YAML-files', () => { - // const desired = readFile('expectedPlain.txt'); - // const filepath1 = getFixturePath('file1.json'); - // const filepath2 = getFixturePath('file2.yml'); + test('compare JSON-files with YAML-files', () => { + const desired = readFile('expectedPlain.txt'); + const filepath1 = getFixturePath('file1.json'); + const filepath2 = getFixturePath('file2.yml'); - // const actual = genDiff(filepath1, filepath2, 'plain'); + const actual = genDiff(filepath1, filepath2, 'plain'); - // expect(actual).toEqual(desired); - // }); + expect(actual).toEqual(desired); + }); - // test('compare file without format', () => { - // const desired = readFile('expectedPlain.txt'); - // const filepath1 = getFixturePath('file1'); - // const filepath2 = getFixturePath('file2.yml'); + test('compare file without format', () => { + const desired = readFile('expectedPlain.txt'); + const filepath1 = getFixturePath('file1'); + const filepath2 = getFixturePath('file2.yml'); - // const actual = genDiff(filepath1, filepath2, 'plain'); + const actual = genDiff(filepath1, filepath2, 'plain'); - // expect(actual).toEqual(desired); - // }); + expect(actual).toEqual(desired); + }); diff --git a/a.out b/a.out new file mode 100644 index 0000000..e69de29 diff --git a/bin/gendiff.js b/bin/gendiff.js index 80c5a34..c811d52 100755 --- a/bin/gendiff.js +++ b/bin/gendiff.js @@ -5,10 +5,10 @@ import { genDiff } from '../src/formatter/index.js'; program .name('gendiff') - .version('0.2.0') + .version('0.3.0') .description('Compares two configuration files and shows a difference.') .option('-f, --format ', 'output format', 'stylish') .arguments(' ') - .action((file1, file2, options) => console.log(genDiff(file1, file2, options))); + .action((file1, file2, options) => console.log(genDiff(file1, file2, options.format))); program.parse(); diff --git a/package.json b/package.json index 408fadd..885b417 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "frontend-project-46", - "version": "0.2.0", + "version": "0.3.0", "description": "Hexlet, second Project: Difference Generator", "main": "index.js", "type": "module", diff --git a/src/buildTree.js b/src/buildTree.js index dbb7f98..fca861f 100644 --- a/src/buildTree.js +++ b/src/buildTree.js @@ -18,20 +18,20 @@ export const buildTree = (file1, file2) => { if (!Object.hasOwn(obj2, key)) { const actualKey1 = isKey1Object ? iter(obj1[key], obj1[key]) : obj1[key]; - return { stat: 'removed', [key]: actualKey1 } + return { stat: 'removed', key: key, value: actualKey1 } } if (!Object.hasOwn(obj1, key)) { const actualKey2 = isKey2Object ? iter(obj2[key], obj2[key]) : obj2[key]; - return { stat: 'added', [key]: actualKey2 } + return { stat: 'added', key: key, value: actualKey2 } } if (isKey1Object && isKey2Object) { - return { stat: 'nested', [key]: iter(obj1[key], obj2[key])} + return { stat: 'nested', key: key, value: iter(obj1[key], obj2[key])} } if (obj1[key] !== obj2[key]) { - return { stat: 'updated', [key]: [obj1[key], obj2[key]] } + return { stat: 'updated', key: key, valueOld: obj1[key], valueNew: obj2[key] } } - return { stat: 'unchanged', [key]: obj2[key] } + return { stat: 'unchanged', key: key, value: obj2[key] } }); return arr; diff --git a/src/formatter/index.js b/src/formatter/index.js index 7173deb..eafadb2 100644 --- a/src/formatter/index.js +++ b/src/formatter/index.js @@ -29,4 +29,4 @@ export const genDiff = (pathFile1, pathFile2, formatName = 'stylish') => { // console.log(process.cwd()); // console.log(genDiff('__fixtures__/file1.json', '__fixtures__/file2.json')); -export { getData }; \ No newline at end of file +export { getData }; diff --git a/src/formatter/plain.js b/src/formatter/plain.js index b262e29..b046175 100644 --- a/src/formatter/plain.js +++ b/src/formatter/plain.js @@ -166,23 +166,13 @@ import { buildTree } from '../buildTree.js'; const correctValue = (value) => { - // // ------ Никогда не срабатывает -------- - // if (_.isPlainObject(value)) { - // return '[complex value]'; - // } - // // -------------------------------------- + if (_.isObject(value)) { + return '[complex value]'; + } if (Array.isArray(value)) { const result = value.map((node) => _.isPlainObject(node) ? '[complex value]' : correctValue(node)); - // const fin = result.includes('[complex value]') ? '[complex value]' : result; - return result; - - // // ------ Не работает -------- - // if (_.isPlainObject(value[0])) { - // return '[complex value]'; - // } return correctValue(value[0]); - // // --------------------------- } const correctedValue = typeof value === 'string' ? `'${value}'` : value; @@ -193,8 +183,9 @@ const correctValue = (value) => { export const getPlain = (data) => { const iter = (arr, propPath) => { const result = arr.flatMap((node) => { - const pairs = Object.entries(node); // [['stat', 'nested'], ['common', '[Array]']] - const [[statName, stat], [key, value]] = pairs; + const key = node.key; + const value = node.value; + const stat = node.stat; const propertyPath = path.join(propPath, key); const formattedPath = propertyPath.replaceAll('/', '.') @@ -204,7 +195,15 @@ export const getPlain = (data) => { case 'nested': return iter(value, formattedPath); case 'removed': return `Property '${formattedPath}' was ${stat}`; case 'added': return `Property '${formattedPath}' was ${stat} with value: ${curentValue}`; - case 'updated': return `Property '${formattedPath}' was ${stat}. From ${curentValue[0]} to ${curentValue[1]}`; + case 'updated': { + const valueOld = node.valueOld; + const valueNew = node.valueNew; + + const valueOldFormatted = correctValue(valueOld); + const valueNewFormatted = correctValue(valueNew); + + return `Property '${formattedPath}' was ${stat}. From ${valueOldFormatted} to ${valueNewFormatted}`; + }; default: return ''; } }); diff --git a/src/formatter/stylish.js b/src/formatter/stylish.js index 22cea90..84bb4f0 100644 --- a/src/formatter/stylish.js +++ b/src/formatter/stylish.js @@ -23,28 +23,36 @@ export const getStylish = (data) => { } }; - const arr = node.map((obj) => { - const [currentStat, key] = Object.keys(obj); - const value = obj[key]; - const stat = obj[currentStat]; + const arr = node.flatMap((obj) => { + // const [currentStat, key] = Object.keys(obj); + // const value = obj[key]; + // const stat = obj[currentStat]; + + const key = obj.key; + const value = obj.value; + const stat = obj.stat; switch (stat) { case 'updated': - const [value1, value2] = value; + // const [value1, value2] = value; + + const valueOld = obj.valueOld; + const valueNew = obj.valueNew; + const [deleted, added] = statSign(stat); const processValue = (val) => { if (_.isPlainObject(val)) { const entries = Object.entries(val); const result = entries.map(([key, value]) => `${currentIndent}${currentIndent}${key}: ${iter(processValue(value), depth + 1)}`); - + return ['{', ...result, `${bracketIndent}${bracketIndent}}`].join('\n'); } return val; }; - return `${currentIndent}${deleted}${key}: ${processValue(value1)}${br}${currentIndent}${added}${key}: ${processValue(value2)}`; + return `${currentIndent}${deleted}${key}: ${processValue(valueOld)}${br}${currentIndent}${added}${key}: ${processValue(valueNew)}`; default: return `${currentIndent}${statSign(stat)}${key}: ${iter(value, depth + 1)}`; }