Skip to content

Commit

Permalink
tried to fix stylish.js
Browse files Browse the repository at this point in the history
  • Loading branch information
zebpaa committed Jul 2, 2024
1 parent 8d3151c commit 8b63a35
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
42 changes: 36 additions & 6 deletions __tests__/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ test('Сравнение плоских файлов (yaml)', () => {
// readFile('file1.yaml');
// readFile('file2.yml');
const result = `{
- follow: false
host: hexlet.io
- proxy: 123.234.53.22
- timeout: 50
+ timeout: 20
+ verbose: true
- follow: false
host: hexlet.io
- proxy: 123.234.53.22
- timeout: 50
+ timeout: 20
+ verbose: true
}`;

expect(gendiff(filepath1, filepath2)).toEqual(result);
Expand Down Expand Up @@ -77,3 +77,33 @@ test('Рекурсивное сравнение', () => {

expect(gendiff(filepath1, filepath2)).toEqual(result);
});

// test('Плоский формат', () => {
// const filepath1 = getFixturePath('file1.json');
// const filepath2 = getFixturePath('file2.json');
// // readFile('file1.yaml');
// // readFile('file2.yml');
// const result = `Property 'common.follow' was added with value: false
// Property 'common.setting2' was removed
// Property 'common.setting3' was updated. From true to null
// Property 'common.setting4' was added with value: 'blah blah'
// Property 'common.setting5' was added with value: [complex value]
// Property 'common.setting6.doge.wow' was updated. From '' to 'so much'
// Property 'common.setting6.ops' was added with value: 'vops'
// Property 'group1.baz' was updated. From 'bas' to 'bars'
// Property 'group1.nest' was updated. From [complex value] to 'str'
// Property 'group2' was removed
// Property 'group3' was added with value: [complex value]`;

// expect(gendiff(filepath1, filepath2, 'plain')).toEqual(result);
// });

// test('Вывод в json', () => {
// const filepath1 = getFixturePath('file1.json');
// const filepath2 = getFixturePath('file2.json');
// // readFile('file1.yaml');
// // readFile('file2.yml');
// const result = '';

// expect(gendiff(filepath1, filepath2, 'json')).toEqual(result);
// });
3 changes: 2 additions & 1 deletion src/formatters/plain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const formateToPlain = (tree) => {
if (obj.type === 'changed') return `Property '${obj.key}' was updated. From ${obj.value1} to ${obj.value2}`;
return [];
});
return formatedTree.map((obj) => console.log(obj));
const result = formatedTree.map((obj) => obj);
return result.join('\n');
};

export default formateToPlain;
15 changes: 7 additions & 8 deletions src/formatters/stylish.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import _ from 'lodash';

const stringify = (value, replacer = ' ', spacesCount = 1) => {
const stringify = (value, replacer = ' ', spacesCount = 4) => {
const iter = (currentValue, depth) => {
// альтернативный вариант: (typeof currentValue !== 'object' || currentValue === null)
if (!_.isObject(currentValue)) {
return `${currentValue}`;
}

const shifting = spacesCount - 2;
const indentSize = depth * spacesCount;
const currentIndent = replacer.repeat(indentSize);
const bracketIndent = replacer.repeat(indentSize - spacesCount);
const lines = Object.entries(currentValue).map(([key, val]) => `${currentIndent}${key}: ${iter(val, depth + 1)}`);
const bracketIndent = replacer.repeat(depth * spacesCount - spacesCount);
const lines = Object.entries(currentValue).map(([key, val]) => `${(key.includes(' ')) ? replacer.repeat(indentSize - shifting) : replacer.repeat(indentSize)}${key}: ${iter(val, depth + 1)}`);

return ['{', ...lines, `${bracketIndent}}`].join('\n');
};
Expand All @@ -29,10 +28,10 @@ const formateToStylish = (tree) => {
result[`- ${obj.key}`] = obj.value1;
result[`+ ${obj.key}`] = obj.value2;
}
result[` ${obj.key}`] = obj.value;
if (obj.type === 'unchanged') result[` ${obj.key}`] = obj.value;
});
// console.log(result);
return stringify(result, ' ', 1);
console.log(result);
return stringify(result, ' ', 4);
};

export default formateToStylish;
7 changes: 2 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ const gendiff = (filepath1, filepath2, formatName = 'stylish') => {
const fullFilePath2 = fullFilePath(filepath2);
const data1 = getData(fullFilePath1);
const data2 = getData(fullFilePath2);
// console.log('data1: ', data1);
// console.log('data2: ', data2);
const tree = buildTree(data1, data2);
// console.log('Tree:', tree);
const formatedTree = format(tree, formatName);
// console.log('##########################');
// console.log('Форматированное дерево:', formatedTree);
console.log('##########################');
console.log('Форматированное дерево:', formatedTree);
return formatedTree;
};

Expand Down

0 comments on commit 8b63a35

Please sign in to comment.