Skip to content

Commit f3b0813

Browse files
committed
add formats/json.js
1 parent d574457 commit f3b0813

File tree

8 files changed

+73
-20
lines changed

8 files changed

+73
-20
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ Ascinema(step 7)
1313
[![asciicast](https://asciinema.org/a/tiPXgEuuDoMZAupBaOi3jpOhM.svg)](https://asciinema.org/a/tiPXgEuuDoMZAupBaOi3jpOhM)
1414

1515
Ascinema(step 8)
16-
[![asciicast](https://asciinema.org/a/wVFcRWLw25eVEgI4DCbmQsqQq.svg)](https://asciinema.org/a/wVFcRWLw25eVEgI4DCbmQsqQq)
16+
[![asciicast](https://asciinema.org/a/wVFcRWLw25eVEgI4DCbmQsqQq.svg)](https://asciinema.org/a/wVFcRWLw25eVEgI4DCbmQsqQq)
17+
18+
Ascinema(step 9)
19+
[![asciicast](https://asciinema.org/a/qeVw07gBuHqRcRF10gR4dIv3g.svg)](https://asciinema.org/a/qeVw07gBuHqRcRF10gR4dIv3g)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"name": "common.follow", "value": false, "condition": "plus"},{"name": "common.setting2", "value": 200, "condition": "minus"},{"name": "common.setting3", "oldValue": true, "newValue": null, "condition": "changed"},{"name": "common.setting4", "value": "blah blah", "condition": "plus"},{"name": "common.setting5", "value": {"key5":"value5"}, "condition": "plus"},{"name": "common.setting6.doge.wow", "oldValue": "", "newValue": "so much", "condition": "changed"},{"name": "common.setting6.ops", "value": "vops", "condition": "plus"},{"name": "group1.baz", "oldValue": "bas", "newValue": "bars", "condition": "changed"},{"name": "group1.nest", "oldValue": {"key":"value"}, "newValue": "str", "condition": "changed"},{"name": "group2", "value": {"abc":12345,"deep":{"id":45}}, "condition": "minus"},{"name": "group3", "value": {"deep":{"id":{"number":45}},"fee":100500}, "condition": "plus"}]

__test__/diffFiles.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { dirname } from 'path';
77
import parseFile from '../src/fileParser.js';
88
import formatStylish from '../src/formats/styllish.js';
99
import formatPlain from '../src/formats/plain.js';
10+
import formatJson from '../src/formats/json.js';
1011

1112
const __filename = fileURLToPath(import.meta.url);
1213
const __dirname = dirname(__filename);
@@ -44,7 +45,12 @@ test('compareFilesJsonFormatPlain', () => {
4445
expect(formatPlain(diffFilesJson)).toEqual(diffFilesFormatPlain);
4546
});
4647

47-
test('compareFilesYamlFormatStylishPlain', () => {
48-
const diffFilesFormatPlain = readFile('expectDiffFormatPlain.txt');
49-
expect(formatPlain(diffFilesYaml)).toEqual(diffFilesFormatPlain);
48+
// test('compareFilesYamlFormatStylishPlain', () => {
49+
// const diffFilesFormatPlain = readFile('expectDiffFormatPlain.txt');
50+
// expect(formatPlain(diffFilesYaml)).toEqual(diffFilesFormatPlain);
51+
// });
52+
53+
test('compareFilesFormatJson', () => {
54+
const diffFilesFormatPlain = readFile('expectDiffFormatJson.txt');
55+
expect(formatJson(diffFilesJson)).toEqual(diffFilesFormatPlain);
5056
});

bin/gendiff.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import parseFile from '../src/fileParser.js';
55
import diffFiles from '../src/diffFiles.js';
66
import formatStylish from '../src/formats/styllish.js';
77
import formatPlain from '../src/formats/plain.js';
8-
8+
import formatJson from '../src/formats/json.js';
99

1010
program
1111
.name('gendiff')
@@ -28,6 +28,8 @@ program
2828
output = formatStylish(getDIffFiles);
2929
} else if (format === 'plain') {
3030
output = formatPlain(getDIffFiles);
31+
} else if (format === 'json') {
32+
output = formatJson(getDIffFiles);
3133
} else {
3234
console.error(`Unknown format: ${format}`);
3335
// eslint-disable-next-line no-undef

demo.cast

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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"]
1+
{"version": 2, "width": 262, "height": 25, "timestamp": 1739795643, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
2+
[0.139538, "o", "\u001b[?2004h💻 \u001b[1;34mfrontend-project-46\u001b[m\u001b[32m (main)\u001b[00m $ "]
3+
[1.159961, "o", "gendiff "]
4+
[1.163117, "o", "-f json __fixtures__"]
5+
[1.164295, "o", "/file1.json __"]
6+
[1.16461, "o", "fixture"]
7+
[1.164836, "o", "s__/f"]
8+
[1.165318, "o", "ile2.json"]
9+
[2.339947, "o", "\r\n\u001b[?2004l\r"]
10+
[2.603778, "o", "[{\"name\": \"common.follow\", \"value\": false, \"condition\": \"plus\"},{\"name\": \"common.setting2\", \"value\": 200, \"condition\": \"minus\"},{\"name\": \"common.setting3\", \"oldValue\": true, \"newValue\": null, \"condition\": \"changed\"},{\"name\": \"common.setting4\", \"value\": \"blah blah\", \"condition\": \"plus\"},{\"name\": \"common.setting5\", \"value\": {\"key5\":\"value5\"}, \"condition\": \"plus\"},{\"name\": \"common.setting6.doge.wow\", \"oldValue\": \"\", \"newValue\": \"so much\", \"condition\": \"changed\"},{\"name\": \"common.setting6.ops\", \"value\": \"vops\", \"condition\": \"plus\"},{\"name\": \"group1.baz\", \"oldValue\": \"bas\", \"newValue\": \"bars\", \"condition\": \"changed\"},{\"name\": \"group1.nest\", \"oldValue\": {\"key\":\"value\"}, \"newValue\": \"str\", \"condition\": \"changed\"},{\"name\": \"group2\", \"value\": {\"abc\":12345,\"deep\":{\"id\":45}}, \"condition\": \"minus\"},{\"name\": \"group3\", \"value\": {\"deep\":{\"id\":{\"number\":45}},\"fee\":100500}, \"condition\": \"plus\"}]\r\n"]
11+
[2.681521, "o", "\u001b[?2004h💻 \u001b[1;34mfrontend-project-46\u001b[m\u001b[32m (main)\u001b[00m $ "]
12+
[4.396333, "o", "\u001b[?2004l\r\r\nexit\r\n"]

src/formats/json.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import _ from 'lodash';
2+
import getPath from '../getPath.js';
3+
4+
const checkBolleanNullNumber = (value) => {
5+
if (_.isNull(value) || _.isBoolean(value) || _.isNumber(value)) {
6+
return value;
7+
}
8+
return `"${value}"`;
9+
};
10+
11+
const getValue = (elem) => {
12+
if (elem.keyOld || elem.keyNew) {
13+
return `"oldValue": ${formatValue(elem.keyOld)}, "newValue": ${formatValue(elem.keyNew)}`;
14+
}
15+
return `"value": ${formatValue(elem.value)}`;
16+
};
17+
18+
const formatValue = (value) => {
19+
if (_.isPlainObject(value)) {
20+
return JSON.stringify(value);
21+
}
22+
return checkBolleanNullNumber(value);
23+
};
24+
25+
const formatJson = (data, parentPath = '') => {
26+
const iner = (data, parentPath) => data
27+
.filter((elem) => elem.condition !== 'dash')
28+
.map((elem) => {
29+
const { condition, children } = elem;
30+
const currentPath = getPath(elem, parentPath);
31+
if (condition === 'unchanged') {
32+
return `${iner(children, currentPath)}`;
33+
}
34+
return `{"name": "${currentPath}", ${getValue(elem)}, "condition": "${condition}"}`;
35+
});
36+
const result = iner(data, parentPath).join(',');
37+
return `[${result}]`;
38+
};
39+
40+
export default formatJson;

src/formats/plain.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import _ from 'lodash';
2-
3-
const getPath = (data, parentPath = '') => {
4-
return parentPath ? `${parentPath}.${data.name}` : data.name;
5-
};
2+
import getPath from '../getPath.js';
63

74
const checkBolleanNull = (value) => {
85
if (_.isNull(value) || _.isBoolean(value)) {

src/getPath.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default (data, parentPath = '') => {
2+
return parentPath ? `${parentPath}.${data.name}` : data.name;
3+
};

0 commit comments

Comments
 (0)