Skip to content

Commit

Permalink
refactored into much more transparent solution
Browse files Browse the repository at this point in the history
  • Loading branch information
lgandecki committed Feb 13, 2019
1 parent 247b673 commit 738f5a5
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/explain.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ function explainObject (obj) {
}
}

const forbidden = ['length', 'name', 'prototype']
const isSafeWithProxy = key => key && key !== 'constructor' && key.toString() !== 'Symbol(Symbol.toStringTag)'

const proxySafeCloneDeepWith = (thing, callback) =>
_.cloneDeepWith(thing, (val, key, obj, stack) => {
if (isSafeWithProxy(key)) {
return callback(val, key, obj, stack)
}
})

function explainChildren (thing) {
const explanations = []
const children = {}

Object.getOwnPropertyNames(thing).forEach((propName) => {
if (_.isFunction(thing[propName])) {
children[propName] = _.tap(explainFunction(thing[propName]), explanation => {
const children = proxySafeCloneDeepWith(thing, (val, key, obj, stack) => {
if (_.isFunction(val) && stack) {
return _.tap(explainFunction(val), (explanation) => {
if (explanation.isTestDouble) explanations.push(explanation)
})
} else if (!forbidden.includes(propName) && _.isObject(thing[propName])) {
const explained = explainChildren(thing[propName])
children[propName] = explained.children
explanations.push(...explained.explanations)
} else if (!forbidden.includes(propName)) {
children[propName] = thing[propName]
}
})
return { explanations, children }
Expand Down

0 comments on commit 738f5a5

Please sign in to comment.