Skip to content

Commit

Permalink
remake plain.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaManukyan committed May 17, 2024
1 parent ed40b60 commit 6656df5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 92 deletions.
3 changes: 1 addition & 2 deletions __tests__/gendiff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ Property 'common.setting6.ops' was added with value: 'vops'
Property 'group1.baz' was updated. From 'bas' to 'bars'
Property 'group1.nest' was updated. From [complex value] to 'str'
Property 'group2' was removed
Property 'group3' was added with value: [complex value]
`;
Property 'group3' was added with value: [complex value]`;

const answerJSON = '[{"key":"common","type":"nested","value":[{"key":"follow","type":"added","value":false},{"key":"setting1","type":"unchanged","value":"Value 1"},{"key":"setting2","type":"removed","value":200},{"key":"setting3","type":"changed","value":null,"prevValue":true},{"key":"setting4","type":"added","value":"blah blah"},{"key":"setting5","type":"added","value":{"key5":"value5"}},{"key":"setting6","type":"nested","value":[{"key":"doge","type":"nested","value":[{"key":"wow","type":"changed","value":"so much","prevValue":""}]},{"key":"key","type":"unchanged","value":"value"},{"key":"ops","type":"added","value":"vops"}]}]},{"key":"group1","type":"nested","value":[{"key":"baz","type":"changed","value":"bars","prevValue":"bas"},{"key":"foo","type":"unchanged","value":"bar"},{"key":"nest","type":"changed","value":"str","prevValue":{"key":"value"}}]},{"key":"group2","type":"removed","value":{"abc":12345,"deep":{"id":45}}},{"key":"group3","type":"added","value":{"deep":{"id":{"number":45}},"fee":100500}}]';

Expand Down
87 changes: 31 additions & 56 deletions formatters/plain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,37 @@ function stringify(value) {
}

function plain(diff, path = '') {
const result = diff.map((element) => {
const currentPath = `${path}${element.key}.`;
switch (element.type) {
case 'nested':
return plain(element.value, currentPath);
case 'added':
if (_.isPlainObject(element.value)) {
return `Property '${currentPath.slice(0, -1)}' was added with value: [complex value]\n`;
}
return `Property '${currentPath.slice(0, -1)}' was added with value: ${stringify(element.value)}\n`;

case 'removed':
return `Property '${currentPath.slice(0, -1)}' was removed\n`;
case 'changed':
if (_.isPlainObject(element.value)) {
return `Property '${currentPath.slice(0, -1)}' was updated. From ${element.prevValue} to [complex value]\n`;
}
if (_.isPlainObject(element.prevValue)) {
return `Property '${currentPath.slice(0, -1)}' was updated. From [complex value] to ${stringify(element.value)}\n`;
}
return `Property '${currentPath.slice(0, -1)}' was updated. From ${stringify(element.prevValue)} to ${stringify(element.value)}\n`;
case 'unchanged':
// return `Property '${currentPath.slice(0, -1)}' was unchanged.`;
return '';
default:
throw new Error(`Element type ${element.type} doesn't exist`);
}
});
return result.join('');
}
/*
let result = res;
diff.forEach((element) => {
const currentPath = `${path}${element.key}.`;
if (element.type === 'nested') {
result = plain(element.value, currentPath, result);
} else if (element.type === 'added') {
if (_.isPlainObject(element.value)) {
result += `Property '${currentPath.slice(0, -1)}' was added with value: [complex value]\n`;
} else {
result += `Property '${currentPath.slice(0, -1)}' was added with value: ${stringify(element.value)}\n`;
}
} else if (element.type === 'removed') {
result += `Property '${currentPath.slice(0, -1)}' was removed\n`;
} else if (element.type === 'changed') {
if (_.isPlainObject(element.value)) {
result += `Property '${currentPath.slice(0, -1)}' was updated. From ${element.prevValue} to [complex value]\n`;
} else if (_.isPlainObject(element.prevValue)) {
result += `Property '${currentPath.slice(0, -1)}' was updated. From [complex value] to ${stringify(element.value)}\n`;
} else {
result += `Property '${currentPath.slice(0, -1)}' was updated. From ${stringify(element.prevValue)} to ${stringify(element.value)}\n`;
function innerFunction(difference, pathDepth = '') {
const result = difference.map((element) => {
const currentPath = `${pathDepth}${element.key}.`;
switch (element.type) {
case 'nested':
return innerFunction(element.value, currentPath);
case 'added':
if (_.isPlainObject(element.value)) {
return `Property '${currentPath.slice(0, -1)}' was added with value: [complex value]\n`;
}
return `Property '${currentPath.slice(0, -1)}' was added with value: ${stringify(element.value)}\n`;
case 'removed':
return `Property '${currentPath.slice(0, -1)}' was removed\n`;
case 'changed':
if (_.isPlainObject(element.value)) {
return `Property '${currentPath.slice(0, -1)}' was updated. From ${element.prevValue} to [complex value]\n`;
}
if (_.isPlainObject(element.prevValue)) {
return `Property '${currentPath.slice(0, -1)}' was updated. From [complex value] to ${stringify(element.value)}\n`;
}
return `Property '${currentPath.slice(0, -1)}' was updated. From ${stringify(element.prevValue)} to ${stringify(element.value)}\n`;
case 'unchanged':
return '';
default:
throw new Error(`Element type ${element.type} doesn't exist`);
}
}
});
*/
// return result;
// }
});
return result.join('');
}
const result = innerFunction(diff, path);
return result.slice(0, -1);
}

export default plain;
34 changes: 0 additions & 34 deletions formatters/stylish.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function stringifyValue(value, levelOfDepth) {

function stylish(list) {
function innerFunc(listOfDifference, levelOfDepth) {
// let res = '';
const countedSpaces = ' '.repeat(4 * levelOfDepth);
const result = listOfDifference.map((element) => {
switch (element.type) {
Expand All @@ -40,38 +39,5 @@ function stylish(list) {
result += innerFunc(list, 1);
return `${result}\n}`;
}
/*
listOfDifference.forEach((element) => {
switch (element.type) {
case 'nested':
res += `${countedSpaces}${element.key}: {\n`;
res += `${innerFunc(stringifyValue(element.value, levelOfDepth), levelOfDepth + 1)}`;
res += `${countedSpaces}}\n`;
break;
case 'added':
res += `${countedSpaces.slice(0, -2)}+ ${element.key}: ${stringifyValue(element.value, levelOfDepth)}\n`;
break;
case 'unchanged':
res += `${countedSpaces.slice(0, -2)} ${element.key}: ${stringifyValue(element.value, levelOfDepth)}\n`;
break;
case 'removed':
res += `${countedSpaces.slice(0, -2)}- ${element.key}: ${stringifyValue(element.value, levelOfDepth)}\n`;
break;
case 'changed':
res += `${countedSpaces.slice(0, -2)}- ${element.key}: ${stringifyValue(element.prevValue, levelOfDepth)}\n`;
res += `${countedSpaces.slice(0, -2)}+ ${element.key}: ${stringifyValue(element.value, levelOfDepth)}\n`;
break;
default:
throw new Error(`Element type ${element.type} doesn't exist`);
}
});
return res;
}
let result = '';
result += '{\n';
result += innerFunc(list, 1);
return `${result.slice(0, -1)}\n}`;
}
*/

export default stylish;

0 comments on commit 6656df5

Please sign in to comment.