UNPKG

gen-diff

Version:

utility for searching differences between config files

30 lines (26 loc) 1.05 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; const toJson = ast => ast.reduce((acc, item) => { const getNode = node => { switch (node.type) { case 'added': return { [node.key]: { newValue: node.newValue } }; case 'removed': return { [node.key]: { oldValue: node.oldValue } }; case 'updated': return { [node.key]: { newValue: node.newValue, oldValue: node.oldValue } }; case 'unchanged': if (node.children) { return { [node.key]: toJson(node.children) }; } return { [node.key]: node.oldValue }; default: return { key: 'unknown' }; } }; return _extends({}, acc, getNode(item)); }, {}); exports.default = ast => JSON.stringify(toJson(ast), null, 4);