Skip to content

Commit 221d675

Browse files
committed
fix name
1 parent 130b268 commit 221d675

File tree

7 files changed

+67
-66
lines changed

7 files changed

+67
-66
lines changed

__test__/diffFiles.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'fs';
33
import path from 'path';
44
import { fileURLToPath } from 'url';
55
import { dirname } from 'path';
6-
import { chooseFormat } from '../src/formats/index.js';
6+
import { chooseFormat } from '../src/formatters/index.js';
77

88
const __filename = fileURLToPath(import.meta.url);
99
const __dirname = dirname(__filename);

bin/gendiff.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
import { program } from 'commander';
4-
import { chooseFormat } from '../src/formats/index.js';
4+
import { chooseFormat } from '../src/formatters/index.js';
55
program
66
.name('gendiff')
77
.description('Compares two configuration files and shows a difference.')
@@ -13,7 +13,7 @@ program
1313
.argument('<filepath1>', 'path to file 1')
1414
.argument('<filepath2>', 'path to file 2')
1515
.action((filePath1, filePath2, options) => {
16-
chooseFormat(filePath1, filePath2, options.format);
16+
console.log(chooseFormat(filePath1, filePath2, options.format));
1717
});
1818

1919
// eslint-disable-next-line no-undef

src/formats/plain.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/formats/index.js renamed to src/formatters/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ export const chooseFormat = (filePath1, filePath2, format = 'stylish') => {
2020
console.error(`Unknown format: ${format}`);
2121
}
2222

23-
console.log(output);
2423
return output;
2524
};

src/formats/json.js renamed to src/formatters/json.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import _ from 'lodash';
22
import getPath from '../getPath.js';
33

4-
const checkBolleanNullNumber = (value) => {
5-
if (_.isNull(value) || _.isBoolean(value) || _.isNumber(value)) {
6-
return value;
7-
}
8-
return `"${value}"`;
9-
};
10-
114
const getValue = (elem) => {
125
if (elem.keyOld || elem.keyNew) {
136
return `"oldValue": ${formatValue(elem.keyOld)}, "newValue": ${formatValue(elem.keyNew)}`;
@@ -16,10 +9,12 @@ const getValue = (elem) => {
169
};
1710

1811
const formatValue = (value) => {
19-
if (_.isPlainObject(value)) {
12+
if (_.isNull(value) || _.isBoolean(value) || _.isNumber(value)) {
13+
return value;
14+
}else if (_.isPlainObject(value)) {
2015
return JSON.stringify(value);
2116
}
22-
return checkBolleanNullNumber(value);
17+
return `"${value}"`;
2318
};
2419

2520
const formatJson = (data, parentPath = '') => {

src/formatters/plain.js

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

0 commit comments

Comments
 (0)