Skip to content

Commit

Permalink
finish plain.js
Browse files Browse the repository at this point in the history
  • Loading branch information
igshipilov committed Oct 24, 2023
1 parent c3cfdb9 commit 82b7683
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<a href="https://asciinema.org/a/616114?autoplay=1"><img src="https://asciinema.org/a/616114.png" width="209"/></a>
<a href="https://asciinema.org/a/616842?autoplay=1"><img src="https://asciinema.org/a/616842.png" width="209"/></a>
132 changes: 67 additions & 65 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Check failure on line 50 in __tests__/index.test.js

View workflow job for this annotation

GitHub Actions / build

More than 1 blank line not allowed

describe('stylish', () => {
test('compare complex JSON-files', () => {
const desired = readFile('expected.txt');
const filepath1 = getFixturePath('file1.json');
const filepath2 = getFixturePath('file2.json');

Check failure on line 57 in __tests__/index.test.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
// const actual = genDiff(filepath1, filepath2);
const actual = genDiff(filepath1, filepath2);

Check failure on line 59 in __tests__/index.test.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
// expect(actual).toEqual(desired);
// });
expect(actual).toEqual(desired);
});

Check failure on line 62 in __tests__/index.test.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed

Check failure on line 63 in __tests__/index.test.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed

Check failure on line 63 in __tests__/index.test.js

View workflow job for this annotation

GitHub Actions / build

More than 1 blank line not allowed
// 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');

Check failure on line 68 in __tests__/index.test.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
// const actual = genDiff(filepath1, filepath2);
const actual = genDiff(filepath1, filepath2);

Check failure on line 70 in __tests__/index.test.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
// 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', () => {

Check warning on line 96 in __tests__/index.test.js

View workflow job for this annotation

GitHub Actions / build

Some tests seem to be commented
// 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)', () => {

Check warning on line 106 in __tests__/index.test.js

View workflow job for this annotation

GitHub Actions / build

Some tests seem to be commented
// 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);
// });

// });
});



Expand All @@ -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);
});



Expand Down
Empty file added a.out
Empty file.
4 changes: 2 additions & 2 deletions bin/gendiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <type>', 'output format', 'stylish')
.arguments('<filepath1> <filepath2>')
.action((file1, file2, options) => console.log(genDiff(file1, file2, options)));
.action((file1, file2, options) => console.log(genDiff(file1, file2, options.format)));

program.parse();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 5 additions & 5 deletions src/buildTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/formatter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
export { getData };
31 changes: 15 additions & 16 deletions src/formatter/plain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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('/', '.')
Expand All @@ -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 '';
}
});
Expand Down
22 changes: 15 additions & 7 deletions src/formatter/stylish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`;
}
Expand Down

0 comments on commit 82b7683

Please sign in to comment.