Skip to content

Commit

Permalink
Merge pull request #31 from dzencot/develop
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
dzencot committed Jan 29, 2017
2 parents e0644de + b8d125e commit faa3da8
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 120 deletions.
91 changes: 52 additions & 39 deletions __tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ describe('test gendiff', () => {
const secondPath = '__tests__/__fixtures__/after';

const expectResult = '{\n' +
' " common": {\n' +
' " setting1": "Value 1"\n' +
' "- setting2": 200\n' +
' " setting3": true\n' +
' "- setting6": {\n' +
' " key": "value"\n' +
' common: {\n' +
' setting1: Value 1\n' +
' - setting2: 200\n' +
' setting3: true\n' +
' - setting6: {\n' +
' key: value\n' +
' }\n' +
' + setting4: blah blah\n' +
' + setting5: {\n' +
' key5: value5\n' +
' }\n' +
' }\n' +
' "+ setting4": "blah blah"\n' +
' "+ setting5": {\n' +
' " key5": "value5"\n' +
' group1: {\n' +
' + baz: bars\n' +
' - baz: bas\n' +
' foo: bar\n' +
' }\n' +
' - group2: {\n' +
' abc: 12345\n' +
' }\n' +
' + group3: {\n' +
' fee: 100500\n' +
' }\n' +
' }\n' +
' " group1": {\n' +
' "+ baz": "bars"\n' +
' "- baz": "bas"\n' +
' " foo": "bar"\n' +
' }\n' +
' "- group2": {\n' +
' " abc": 12345\n' +
' }\n' +
' "+ group3": {\n' +
' " fee": 100500\n' +
' }\n' +
'}';

it('gendiff test JSON', () => {
Expand All @@ -56,67 +56,80 @@ Property 'group3' was added with complex value`;
expect(gendiff(`${firstPath}.json`, `${secondPath}.json`, 'plain')).toEqual(resultPlain);
});

const expectObj = {
common: {
const expectObj = [
{
name: 'common',
type: 'object',
data: {
setting1: {
data: [
{
name: 'setting1',
type: 'unchanged',
data: 'Value 1',
},
setting2: {
{
name: 'setting2',
type: 'removed',
data: 200,
},
setting3: {
{
name: 'setting3',
type: 'unchanged',
data: true,
},
setting6: {
{
name: 'setting6',
type: 'removed',
data: {
key: 'value',
},
},
setting4: {
{
name: 'setting4',
type: 'added',
data: 'blah blah',
},
setting5: {
{
name: 'setting5',
type: 'added',
data: {
key5: 'value5',
},
},
},
],
},
group1: {
{
name: 'group1',
type: 'object',
data: {
baz: {
data: [
{
name: 'baz',
type: 'updated',
data: 'bars',
previous: 'bas',
},
foo: {
{
name: 'foo',
type: 'unchanged',
data: 'bar',
},
},
],
},
group2: {
{
name: 'group2',
type: 'removed',
data: {
abc: 12345,
},
},
group3: {
{
name: 'group3',
type: 'added',
data: {
fee: 100500,
},
},
};
];


it('gendiff test json', () => {
expect(gendiff(`${firstPath}.json`, `${secondPath}.json`, 'json')).toEqual(JSON.stringify(expectObj, null, ' '));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "project-lvl2-s13 for Hexlet",
"main": "index.js",
"dependencies": {
"chalk": "^1.1.3",
"commander": "^2.9.0",
"ini-config-parser": "^1.0.2",
"lodash": "^4.17.4",
Expand Down
6 changes: 3 additions & 3 deletions src/bin/gendiff.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env node

// @flow
/* eslint-disable no-console */
import program from 'commander';
import genDiff from '..';
import getDiff from '..';
import output from '../output';

program
.version('0.0.1')
.arguments('<first_config> <second_config>')
.action((firstPath, secondPath) =>
console.log(genDiff(firstPath, secondPath, program.format)))
output(getDiff(firstPath, secondPath, program.format)))
.description('Compares two configuration files and shows a difference.')
.option('-f, --format [type]', 'output format');

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import fs from 'fs';
import path from 'path';
import gendiff from './lib/gendiff';
import gendiff from './lib/getDiff';
import parser from './lib/parser';
import format from './lib/formaters';

Expand Down
9 changes: 5 additions & 4 deletions src/lib/formaters/toJson.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// @flow
/* eslint no-param-reassign: ["error", { "props": false }] */
import lodash from 'lodash';

const toJson = (obj) => {
const result = Object.keys(obj).reduce((acc, key) => {
const currentObj = obj[key];
const result = lodash.flatten(obj).reduce((acc, currentObj) => {
const typeVal = currentObj.type;
const value = typeVal === 'object' ? toJson(currentObj.data) : currentObj.data;
const res = {
name: currentObj.name,
type: typeVal,
data: value,
};
if (typeVal === 'updated') res.previous = currentObj.previous;
acc[key] = res;
acc.push(res);
return acc;
}, {});
}, []);
return result;
};

Expand Down
13 changes: 7 additions & 6 deletions src/lib/formaters/toPlain.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// @flow

import lodash from 'lodash';

const plainer = (obj, parrent = '') => {
const result = Object.keys(obj).reduce((acc, key) => {
const currentObj = obj[key];
const result = lodash.flatten(obj).reduce((acc, currentObj) => {
if (currentObj.type === 'added') {
const value = currentObj.data instanceof Object ? 'complex value' : `value: '${currentObj.data}'`;
return `${acc}\nProperty '${parrent}${key}' was added with ${value}`;
return `${acc}\nProperty '${parrent}${currentObj.name}' was added with ${value}`;
} else if (currentObj.type === 'removed') {
return `${acc}\nProperty '${parrent}${key}' was removed`;
return `${acc}\nProperty '${parrent}${currentObj.name}' was removed`;
} else if (currentObj.type === 'updated') {
return `${acc}\nProperty '${parrent}${key}' was updated. From '${currentObj.previous}' to '${currentObj.data}'`;
return `${acc}\nProperty '${parrent}${currentObj.name}' was updated. From '${currentObj.previous}' to '${currentObj.data}'`;
} else if (currentObj.type === 'object') {
const newParrent = `${parrent}${key}.`;
const newParrent = `${parrent}${currentObj.name}.`;
return `${acc}${plainer(currentObj.data, newParrent)}`;
}
return acc;
Expand Down
56 changes: 33 additions & 23 deletions src/lib/formaters/toString.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
// @flow
/* eslint no-param-reassign: ["error", { "props": false }] */
/* eslint arrow-body-style: ["error", "always"] */

const getData = (data) => {
import lodash from 'lodash';

const getDataString = (data, countTab) => {
if (typeof data !== 'object') {
return data;
return ` ${data}`;
}
return Object.keys(data).reduce((acc, key) => {
acc[` ${key}`] = data[key];
return acc;
}, {});
const newCountTab = countTab + 1;
const tab = lodash.repeat(' ', newCountTab);
const result = Object.keys(data).reduce((acc, key) => {
return `${acc}${tab} ${key}:${getDataString(data[key], newCountTab + 1)}\n`;
}, ' {\n');
return `${result}${tab}}`;
};
const toString = (obj) => {
const result = Object.keys(obj).reduce((acc, key) => {
const currentObj = obj[key];

const getObjString = (obj, countTab) => {
const newCountTab = countTab + 1;
const tab = lodash.repeat(' ', newCountTab);
const result = lodash.flatten(obj).reduce((acc, currentObj) => {
if (currentObj.type === 'unchanged') {
return `${acc}${tab} ${currentObj.name}:${getDataString(currentObj.data, newCountTab)}\n`;
}
if (currentObj.type === 'added') {
return `${acc}${tab}+ ${currentObj.name}:${getDataString(currentObj.data, newCountTab)}\n`;
}
if (currentObj.type === 'removed') {
return `${acc}${tab}- ${currentObj.name}:${getDataString(currentObj.data, newCountTab)}\n`;
}
if (currentObj.type === 'object') {
return `${acc}${tab} ${currentObj.name}: ${getObjString(currentObj.data, newCountTab + 1)}\n`;
}
if (currentObj.type === 'updated') {
acc[`+ ${key}`] = currentObj.data;
acc[`- ${key}`] = currentObj.previous;
} else if (currentObj.type === 'added') {
acc[`+ ${key}`] = getData(currentObj.data);
} else if (currentObj.type === 'object') {
acc[` ${key}`] = toString(currentObj.data);
} else if (currentObj.type === 'removed') {
acc[`- ${key}`] = getData(currentObj.data);
} else if (currentObj.type === 'unchanged') {
acc[` ${key}`] = currentObj.data;
return `${acc}${tab}+ ${currentObj.name}:${getDataString(currentObj.data, newCountTab)}
${tab}- ${currentObj.name}:${getDataString(currentObj.previous, newCountTab)}\n`;
}
return acc;
}, {});
return result;
}, '{\n');
return `${result}${lodash.repeat(' ', countTab)}}`;
};

export default obj => JSON.stringify(toString(obj), null, 2).replace(/,/g, '');
export default (obj) => { return getObjString(obj, 0); };

44 changes: 0 additions & 44 deletions src/lib/gendiff.js

This file was deleted.

49 changes: 49 additions & 0 deletions src/lib/getDiff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// @flow
/* eslint no-use-before-define: ["error", { "functions": false }] */

import lodash from 'lodash';

const getComparedData = (data1, data2) => {
if (data1 === data2) {
return {
type: 'unchanged',
data: data1,
};
}
if (!data1) {
return {
type: 'added',
data: data2,
};
}
if (!data2) {
return {
type: 'removed',
data: data1,
};
}
if (typeof data1 === 'object') {
return {
type: 'object',
data: getDiff(data1, data2),
};
}
return {
type: 'updated',
data: data2,
previous: data1,
};
};

function getDiff(firstObject, secondObject) {
const diffUnion = lodash.union(lodash.keys(firstObject), lodash.keys(secondObject));
const diff = diffUnion.reduce((acc, key) => {
const res = getComparedData(firstObject[key], secondObject[key]);
res.name = key;
acc.push(res);
return acc;
}, []);
return diff;
}
export default getDiff;

0 comments on commit faa3da8

Please sign in to comment.