Skip to content

Commit 1689735

Browse files
committed
update src/formatters/plain.js
1 parent 4c3cdc2 commit 1689735

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/formatters/plain.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
const transformValue = (value) => {
1+
const stringify = (value) => {
22
if (typeof value === 'object' && value !== null) {
33
return '[complex value]';
44
}
55

6-
if (typeof value === 'string') {
7-
return `'${value}'`;
8-
}
9-
10-
return value;
6+
return typeof value === 'string' ? `'${value}'` : value;
117
};
128

139
const getPath = (keyPath) => {
@@ -17,29 +13,37 @@ const getPath = (keyPath) => {
1713
return newPath.join('.');
1814
};
1915

20-
const plain = (properties) => {
21-
const iter = (data, path = '') => {
22-
const result = data
23-
.flatMap((property) => {
24-
const newPath = getPath([path, property.key]);
25-
switch (property.type) {
16+
const plain = (tree) => {
17+
const iter = (node, path = '') => {
18+
const properties = node
19+
.filter(({ type }) => type !== 'unchanged')
20+
.map(({
21+
type,
22+
key,
23+
value,
24+
oldValue,
25+
newValue,
26+
children,
27+
}) => {
28+
const newPath = getPath([path, key]);
29+
switch (type) {
2630
case 'added':
27-
return `Property '${newPath}' was added with value: ${transformValue(property.value)}`;
31+
return `Property '${newPath}' was added with value: ${stringify(value)}`;
2832
case 'deleted':
2933
return `Property '${newPath}' was removed`;
3034
case 'changed':
31-
return `Property '${newPath}' was updated. From ${transformValue(property.oldValue)} to ${transformValue(property.newValue)}`;
35+
return `Property '${newPath}' was updated. From ${stringify(oldValue)} to ${stringify(newValue)}`;
3236
case 'nested':
33-
return iter(property.children, newPath);
37+
return iter(children, newPath);
3438
default:
35-
return [];
39+
throw new Error(`Such type - ${type} doesn't exist`);
3640
}
3741
});
3842

39-
return result.join('\n');
43+
return properties.join('\n');
4044
};
4145

42-
return iter(properties);
46+
return iter(tree);
4347
};
4448

4549
export default plain;

0 commit comments

Comments
 (0)