Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | 1x 1x 1x 1x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 145x 145x 145x 145x 145x 145x 143x 141x 141x 148x 148x 147x 147x 147x 147x 147x 147x 7x 7x 6x 6x 2x 4x 4x 4x 4x 162x 156x 156x 156x 156x 156x 160x 160x 160x 160x 160x 1x | 'use strict'
const { diff } = require('deep-diff')
const { set, get, map, omit, startCase, camelCase, compact, concat, isEmpty } = require('lodash')
const OPERATION = { N: 'Created', D: 'Deleted', E: 'Edited', A: 'Array' }
let changes = []
class DiffHelper {
/**
* @author welington Monteiro
* @param { Object } [diffParams.old] - old json
* @param { Object } [diffParams.current] - current json
* @param { Object } [diffParams] - params {old, current}
* @param {{}} schema - paths schema mapped
* @param { Object } [options] - options params
* @param { Array<String> } [options.omitPaths] - options omitPaths params
* @param { String } [options.changeTransform.label] - options changesToView.label {label: 'otherPathName'}
* @param { String } [options.changeTransform.to] - options changesToView.to {to: 'otherPathName'}
* @param { String } [options.changeTransform.from] - options changesToView.from {from: 'otherPathName'}
* @param { String } [options.changeTransform.ops] - options changesToView.ops {ops: 'otherPathName'}
* @param { String } [options.changeTransform.path] - options changesToView.path {path: 'otherPathName'}
* @return { Array } - list of diffs changes
*/
static getDiff (diffParams = {}, schema = {}, options = {}) {
let { old = {}, current = {} } = diffParams
const omitPaths = get(options, 'omitPaths', [])
const defaultOmitPaths = ['_id']
old = omit(old, concat(omitPaths, defaultOmitPaths))
current = omit(current, concat(omitPaths, defaultOmitPaths))
changes = []
const differences = diff(old, current)
DiffHelper._mapItems(differences, schema, options)
return compact(changes)
}
/**
* @author Welington Monteiro
* @param { Array } diffs - deep diffs between old and current
* @param { Schema } schema - paths schema mapped
* @param { Object } [options] - options params
* @return {void}
* @private
*/
static _mapItems (diffs, schema, options) {
map(diffs, _addItem)
function _addItem (item) {
const to = get(item, 'rhs', get(item, 'item.rhs', ''))
const path = item.path.join('.')
const from = get(item, 'lhs', get(item, 'item.lhs', ''))
let ops = OPERATION[get(item, 'kind', '')]
if (ops === 'Array') ops = OPERATION[get(item, 'item.kind', '')]
if (typeof to === 'object') return DiffHelper._objectMapped({ path, item: to, ops, schema }, 'to', options)
if (typeof from === 'object') return DiffHelper._objectMapped({ path, item: from, ops, schema }, 'from', options)
const label = DiffHelper._getLabel(path, schema)
changes.push(DiffHelper._changeTransformToView({ to, path, from, ops, label }, options))
}
}
/**
* @author Welington Monteiro
* @param { Object } change - object params
* @param { Object } [options] - options change transform to view
* @return {Object} newChange - object new transformed
*/
static _changeTransformToView (change = {}, options = {}) {
const newChange = {}
if (isEmpty(change)) return null
set(newChange, `${get(options, 'changeTransform.to') || 'to'}`, get(change, 'to'))
set(newChange, `${get(options, 'changeTransform.from') || 'from'}`, get(change, 'from'))
set(newChange, `${get(options, 'changeTransform.path') || 'path'}`, get(change, 'path'))
set(newChange, `${get(options, 'changeTransform.ops') || 'ops'}`, get(change, 'ops'))
set(newChange, `${get(options, 'changeTransform.label') || 'label'}`, get(change, 'label'))
return newChange
}
/**
* @author Welington Monteiro
* @param { Object } [params] - item to mapped fields
* @param { Object } [params.item] - item to mapped fields
* @param { String } [params.path] - path fields
* @param { String } [params.ops] - ops mapped
* @param { Object } [params.schema] - paths schema mapped
* @param [params] - params mapped
* @param { String } fieldItem - name field item mapped to object
* @param { Object } [options] - options change transform to view
* @private
*/
static _objectMapped (params = {}, fieldItem, options) {
const { item = '', path, ops, schema = {} } = params
map(item, (value, field) => {
const newPath = `${path}.${field}`
if (typeof value === 'object') {
return DiffHelper._objectMapped({ path: newPath, item: value, ops, schema }, fieldItem, options)
}
const change = { from: '', to: '' }
const label = DiffHelper._getLabel(newPath, schema)
change[`${fieldItem}`] = value
changes.push(DiffHelper._changeTransformToView({ ...change, path: newPath, ops, label }, options))
})
}
/**
* @author Welington Monteiro
* @param { String } str - string name to capitalize
* @return {string} name field capitalized
* @private
*/
static _capitalizedKey (str = '') {
return startCase(camelCase(str))
}
/**
* @author Welington Monteiro
* @param { String } path - path object
* @param { Object } schema - paths schema mapped
* @return { string } return result
* @private
*/
static _getLabel (path = '', schema = {}) {
const paths = compact(path.replace(/[0-9]/ig, '').split('.'))
const lastPath = paths[paths.length - 1]
const label = DiffHelper._getCustomLabel(paths, schema)
const nameCapitalized = DiffHelper._capitalizedKey(lastPath)
return (label || nameCapitalized)
}
/**
* @author Welington Monteiro
* @param { Array } pathsArray - flatten paths schema
* @param { Object } schema - Schemas mapped paths
* @return {string} customLabel - return name custom label
*/
static _getCustomLabel (pathsArray = [], schema = {}) {
const flattenPath = pathsArray.join('.')
const paths = !isEmpty(schema.paths) ? schema.paths[flattenPath] : ''
const singleNestedPaths = !isEmpty(schema.singleNestedPaths) ? schema.singleNestedPaths[flattenPath] : ''
const subpaths = !isEmpty(schema.subpaths) ? schema.subpaths[flattenPath] : ''
return get(paths, 'options._label_', get(singleNestedPaths, 'options._label_', get(subpaths, 'options._label_')))
}
}
module.exports = DiffHelper
|