Redxnel/project-lvl2-s405

View on GitHub
src/renderers/renderPlain.js

Summary

Maintainability
A
0 mins
Test Coverage
import _ from 'lodash';

const setValue = (value) => {
  if (value instanceof Object) {
    return '[complex value]';
  }
  return typeof value === 'string' ? `'${value}'` : value;
};

const propertyActions = {
  added: (obj, path) => `Property '${path}${obj.name}' was added with value: ${setValue(obj.valueAfter)}`,
  updated: (obj, path) => `Property '${path}${obj.name}' was updated. From ${setValue(obj.valueBefore)} to ${setValue(obj.valueAfter)}`,
  removed: (obj, path) => `Property '${path}${obj.name}' was removed`,
  nested: (obj, path, func) => func(obj.children, `${path}${obj.name}.`),
  unchanged: () => [],
};

const render = (ast, name = '') => {
  const result = ast.reduce(
    (acc, node) => [...acc, (propertyActions[node.type](node, name, render))], [],
  );

  return _.flatten(result).join('\n');
};

export default render;