Skip to content

Commit d574457

Browse files
committed
add formatPlain
1 parent f614bc5 commit d574457

File tree

7 files changed

+115
-39
lines changed

7 files changed

+115
-39
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ Ascinema(step 6)
1010
[![asciicast](https://asciinema.org/a/HxkkOQiIYxm2vO2JyCDFUd5sn.svg)](https://asciinema.org/a/HxkkOQiIYxm2vO2JyCDFUd5sn)
1111

1212
Ascinema(step 7)
13-
[![asciicast](https://asciinema.org/a/tiPXgEuuDoMZAupBaOi3jpOhM.svg)](https://asciinema.org/a/tiPXgEuuDoMZAupBaOi3jpOhM)
13+
[![asciicast](https://asciinema.org/a/tiPXgEuuDoMZAupBaOi3jpOhM.svg)](https://asciinema.org/a/tiPXgEuuDoMZAupBaOi3jpOhM)
14+
15+
Ascinema(step 8)
16+
[![asciicast](https://asciinema.org/a/wVFcRWLw25eVEgI4DCbmQsqQq.svg)](https://asciinema.org/a/wVFcRWLw25eVEgI4DCbmQsqQq)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Property 'common.follow' was added with value: false
2+
Property 'common.setting2' was removed
3+
Property 'common.setting3' was updated. From true to null
4+
Property 'common.setting4' was added with value: 'blah blah'
5+
Property 'common.setting5' was added with value: [complex value]
6+
Property 'common.setting6.doge.wow' was updated. From '' to 'so much'
7+
Property 'common.setting6.ops' was added with value: 'vops'
8+
Property 'group1.baz' was updated. From 'bas' to 'bars'
9+
Property 'group1.nest' was updated. From [complex value] to 'str'
10+
Property 'group2' was removed
11+
Property 'group3' was added with value: [complex value]

__test__/diffFiles.test.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { fileURLToPath } from 'url';
66
import { dirname } from 'path';
77
import parseFile from '../src/fileParser.js';
88
import formatStylish from '../src/formats/styllish.js';
9+
import formatPlain from '../src/formats/plain.js';
910

1011
const __filename = fileURLToPath(import.meta.url);
1112
const __dirname = dirname(__filename);
@@ -15,20 +16,35 @@ const getFixturePath = (filename) =>
1516
const readFile = (filename) =>
1617
fs.readFileSync(getFixturePath(filename), 'utf-8');
1718

18-
const normalizedString = (str) => str.replace(/\r\n/g, '\n');
19+
const prepareFiles = (file1, file2) => {
20+
const parsedFile1 = parseFile(getFixturePath(file1));
21+
const parsedFile2 = parseFile(getFixturePath(file2));
22+
return compareFiles(parsedFile1, parsedFile2);
23+
};
1924

20-
test('compareFilesJson', () => {
21-
const diffFiles = readFile('expectDiffNestedStrucrure.txt');
22-
const file1 = parseFile(getFixturePath('file1.json'));
23-
const file2 = parseFile(getFixturePath('file2.json'));
24-
const getDIffFiles = compareFiles(file1, file2);
25-
expect(formatStylish(getDIffFiles)).toEqual(normalizedString(diffFiles));
25+
let diffFilesJson;
26+
let diffFilesYaml;
27+
beforeEach(() => {
28+
diffFilesJson = prepareFiles('file1.json', 'file2.json');
29+
diffFilesYaml = prepareFiles('file1.yaml', 'file2.yaml');
2630
});
2731

28-
test('compareFilesYaml', () => {
29-
const diffFiles = readFile('expectDiffNestedStrucrure.txt');
30-
const file1 = parseFile(getFixturePath('file1.yaml'));
31-
const file2 = parseFile(getFixturePath('file2.yaml'));
32-
const getDIffFiles = compareFiles(file1, file2);
33-
expect(formatStylish(getDIffFiles)).toEqual(normalizedString(diffFiles));
32+
test('compareFilesJsonFormatStylish', () => {
33+
const diffFilesFormatStylish = readFile('expectDiffFormatStylish.txt');
34+
expect(formatStylish(diffFilesJson)).toEqual(diffFilesFormatStylish);
35+
});
36+
37+
test('compareFilesYamlFormatStylish', () => {
38+
const diffFilesFormatStylish = readFile('expectDiffFormatStylish.txt');
39+
expect(formatStylish(diffFilesYaml)).toEqual(diffFilesFormatStylish);
40+
});
41+
42+
test('compareFilesJsonFormatPlain', () => {
43+
const diffFilesFormatPlain = readFile('expectDiffFormatPlain.txt');
44+
expect(formatPlain(diffFilesJson)).toEqual(diffFilesFormatPlain);
45+
});
46+
47+
test('compareFilesYamlFormatStylishPlain', () => {
48+
const diffFilesFormatPlain = readFile('expectDiffFormatPlain.txt');
49+
expect(formatPlain(diffFilesYaml)).toEqual(diffFilesFormatPlain);
3450
});

bin/gendiff.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { program } from 'commander';
44
import parseFile from '../src/fileParser.js';
55
import diffFiles from '../src/diffFiles.js';
66
import formatStylish from '../src/formats/styllish.js';
7+
import formatPlain from '../src/formats/plain.js';
8+
79

810
program
911
.name('gendiff')
@@ -24,6 +26,8 @@ program
2426
let output;
2527
if (format === 'stylish') {
2628
output = formatStylish(getDIffFiles);
29+
} else if (format === 'plain') {
30+
output = formatPlain(getDIffFiles);
2731
} else {
2832
console.error(`Unknown format: ${format}`);
2933
// eslint-disable-next-line no-undef

demo.cast

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
{"version": 2, "width": 225, "height": 26, "timestamp": 1739350136, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
2-
[0.117885, "o", "\u001b[?2004h💻 \u001b[1;34mfrontend-project-46\u001b[m\u001b[32m (main)\u001b[00m $ "]
3-
[0.916059, "o", "gendiff __fixt"]
4-
[0.916776, "o", "ures__/file1.json __"]
5-
[0.917413, "o", "fixtures__"]
6-
[0.922213, "o", "/file2.json"]
7-
[2.047204, "o", "\r\n\u001b[?2004l\r"]
8-
[2.282718, "o", "{\r\n common: {\r\n + follow: false\r\n setting1: Value 1\r\n - setting2: 200\r\n - setting3: true\r\n + setting3: null\r\n + setting4: blah blah\r\n + setting5: {\r\n key5: value5\r\n }\r\n setting6: {\r\n doge: {\r\n - wow: \r\n + wow: so much\r\n }\r\n key: value\r\n + ops: vops\r\n }\r\n }\r\n group1: {\r\n - baz: bas\r\n + baz: bars\r\n foo: bar\r\n - nest: {\r\n key: value\r\n }\r\n + nest: str\r\n }\r\n - group2: {\r\n abc: 12345\r\n deep: {\r\n id: 45\r\n }\r\n }\r\n + group3: {\r\n deep: {\r\n id: {\r\n number: 45\r\n }\r\n }\r\n fee: 100500\r\n }\r\n}\r\n"]
9-
[2.357678, "o", "\u001b[?2004h💻 \u001b[1;34mfrontend-project-46\u001b[m\u001b[32m (main)\u001b[00m $ "]
10-
[8.336829, "o", "gendiff -f blabla __fi"]
11-
[8.337143, "o", "xtures_"]
12-
[8.337615, "o", "_/file1.json __"]
13-
[8.34266, "o", "fixtures"]
14-
[8.343041, "o", "__/file2.json"]
15-
[9.840874, "o", "\r\n\u001b[?2004l\r"]
16-
[10.077252, "o", "Unknown format: blabla\r\n"]
17-
[10.151201, "o", "\u001b[?2004h💻 \u001b[1;34mfrontend-project-46\u001b[m\u001b[32m (main)\u001b[00m $ "]
18-
[14.028885, "o", "gendiff -f styli"]
19-
[14.029331, "o", "sh __fixtures__/file1.json _"]
20-
[14.033403, "o", "_fixtur"]
21-
[14.033639, "o", "es__/file2.json"]
22-
[15.203751, "o", "\r\n\u001b[?2004l\r"]
23-
[15.441187, "o", "{\r\n common: {\r\n + follow: false\r\n setting1: Value 1\r\n - setting2: 200\r\n - setting3: true\r\n + setting3: null\r\n + setting4: blah blah\r\n + setting5: {\r\n key5: value5\r\n }\r\n setting6: {\r\n doge: {\r\n - wow: \r\n + wow: so much\r\n }\r\n key: value\r\n + ops: vops\r\n }\r\n }\r\n group1: {\r\n - baz: bas\r\n + baz: bars\r\n foo: bar\r\n - nest: {\r\n key: value\r\n }\r\n + nest: str\r\n }\r\n - group2: {\r\n abc: 12345\r\n deep: {\r\n id: 45\r\n }\r\n }\r\n + group3: {\r\n deep: {\r\n id: {\r\n number: 45\r\n }\r\n }\r\n fee: 100500\r\n }\r\n}\r\n"]
24-
[15.511331, "o", "\u001b[?2004h💻 \u001b[1;34mfrontend-project-46\u001b[m\u001b[32m (main)\u001b[00m $ "]
25-
[18.692786, "o", "\u001b[?2004l\r\r\nexit\r\n"]
1+
{"version": 2, "width": 225, "height": 14, "timestamp": 1739443045, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
2+
[0.131436, "o", "\u001b[?2004h💻 \u001b[1;34mfrontend-project-46\u001b[m\u001b[32m (main)\u001b[00m $ "]
3+
[2.188625, "o", "gendiff "]
4+
[2.18965, "o", "-f plain"]
5+
[2.1915, "o", " __fixtures__"]
6+
[2.194056, "o", "/file1.json __fixtur"]
7+
[2.196402, "o", "es__/file2.json"]
8+
[3.021137, "o", "\r\n\u001b[?2004l\r"]
9+
[3.269762, "o", "Property 'common.follow' was added with value: false\r\nProperty 'common.setting2' was removed\r\nProperty 'common.setting3' was updated. From true to null\r\nProperty 'common.setting4' was added with value: 'blah blah'\r\nProperty 'common.setting5' was added with value: [complex value]\r\nProperty 'common.setting6.doge.wow' was updated. From '' to 'so much'\r\nProperty 'common.setting6.ops' was added with value: 'vops'\r\nProperty 'group1.baz' was updated. From 'bas' to 'bars'\r\nProperty 'group1.nest' was updated. From [complex value] to 'str'\r\nProperty 'group2' was removed\r\nProperty 'group3' was added with value: [complex value]\r\n"]
10+
[3.348678, "o", "\u001b[?2004h💻 \u001b[1;34mfrontend-project-46\u001b[m\u001b[32m (main)\u001b[00m $ "]
11+
[5.078226, "o", "\u001b[?2004l\r\r\nexit\r\n"]

src/formats/plain.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import _ from 'lodash';
2+
3+
const getPath = (data, parentPath = '') => {
4+
return parentPath ? `${parentPath}.${data.name}` : data.name;
5+
};
6+
7+
const checkBolleanNull = (value) => {
8+
if (_.isNull(value) || _.isBoolean(value)) {
9+
return value;
10+
}
11+
return `'${value}'`;
12+
};
13+
14+
const getValue = (data) => {
15+
if (data.condition === 'changed') {
16+
return `${
17+
_.isObject(data.keyOld)
18+
? `[complex value]`
19+
: `${checkBolleanNull(data.keyOld)}`
20+
} to ${
21+
_.isObject(data.keyNew)
22+
? `[complex value]`
23+
: `${checkBolleanNull(data.keyNew)}`
24+
}`;
25+
} else if (_.isObject(data.value)) {
26+
return `[complex value]`;
27+
}
28+
29+
return checkBolleanNull(data.value);
30+
};
31+
32+
const formatPlain = (data, parentPath = '') => {
33+
const lines = data.map((elem) => {
34+
const { condition, children } = elem;
35+
const currentPath = getPath(elem, parentPath);
36+
37+
switch (condition) {
38+
case 'plus':
39+
return `Property '${currentPath}' was added with value: ${getValue(
40+
elem
41+
)}`;
42+
case 'minus':
43+
return `Property '${currentPath}' was removed`;
44+
case 'changed':
45+
return `Property '${currentPath}' was updated. From ${getValue(elem)}`;
46+
case 'unchanged':
47+
return `${formatPlain(children, currentPath)}`;
48+
default:
49+
return '';
50+
}
51+
});
52+
const result = _.compact(lines);
53+
return result.join('\n');
54+
};
55+
56+
export default formatPlain;

0 commit comments

Comments
 (0)