src/formatters/plain.js
import _ from 'lodash';
const getPath = (nodeNames) => nodeNames.flat().join('.');
const checkVal = (value) => {
switch (typeof value) {
case 'object': {
return !value ? 'null' : '[complex value]';
}
case 'string': {
return `'${value}'`;
}
default: {
return `${value}`;
}
}
};
export function makePlainDiff(tree) {
const iter = (node, path) => node.map((child) => {
const currentPath = getPath([path, child.key]);
switch (child.type) {
case 'nested': {
return iter(child.children, currentPath);
}
case 'added': {
return `Property '${currentPath}' was added with value: ${checkVal(child.value)}`;
}
case 'removed': {
return `Property '${currentPath}' was removed`;
}
case 'changed': {
return `Property '${currentPath}' was updated. From ${checkVal(child.value)} to ${checkVal(child.value2)}`;
}
case 'unchanged': {
return null;
}
default: {
throw Error('Uncorrect data');
}
}
});
return iter(tree.children, []);
}
export default function makePlain(data) {
const result = makePlainDiff(data);
const flatten = _.flattenDeep(result);
const filtered = flatten.filter((el) => el);
return filtered.join('\n');
}