module.exports = functionvar_dump () {
const echo = require('../strings/echo')
let output = ''
const padChar = ' '
const padVal = 4
let lgth = 0
let i = 0
const _getFuncName = function (fn) {
const name = (/\W*function\s+([\w$]+)\s*\(/)
.exec(fn)
if (!name) {
return'(Anonymous)'
}
return name[1]
}
const _repeatChar = function (len, padChar) {
let str = ''
for (let i = 0; i < len; i++) {
str += padChar
}
return str
}
const _getInnerVal = function (val, thickPad) {
let ret = ''
if (val === null) {
ret = 'NULL'
} elseif (typeof val === 'boolean') {
ret = 'bool(' + val + ')'
} elseif (typeof val === 'string') {
ret = 'string(' + val.length + ') "' + val + '"'
} elseif (typeof val === 'number') {
if (parseFloat(val) === parseInt(val, 10)) {
ret = 'int(' + val + ')'
} else {
ret = 'float(' + val + ')'
}
} elseif (typeof val === 'undefined') {
ret = 'undefined'
} elseif (typeof val === 'function') {
const funcLines = val.toString()
.split('\n')
ret = ''
for (let i = 0, fll = funcLines.length; i < fll; i++) {
ret += (i !== 0 ? '\n' + thickPad : '') + funcLines[i]
}
} elseif (val instanceofDate) {
ret = 'Date(' + val + ')'
} elseif (val instanceofRegExp) {
ret = 'RegExp(' + val + ')'
} elseif (val.nodeName) {
switch (val.nodeType) {
case1:
if (typeof val.namespaceURI === 'undefined' ||
val.namespaceURI === 'https://www.w3.org/1999/xhtml') {
ret = 'HTMLElement("' + val.nodeName + '")'
} else {
ret = 'XML Element("' + val.nodeName + '")'
}
break
case2:
ret = 'ATTRIBUTE_NODE(' + val.nodeName + ')'
break
case3:
ret = 'TEXT_NODE(' + val.nodeValue + ')'
break
case4:
ret = 'CDATA_SECTION_NODE(' + val.nodeValue + ')'
break
case5:
ret = 'ENTITY_REFERENCE_NODE'
break
case6:
ret = 'ENTITY_NODE'
break
case7:
ret = 'PROCESSING_INSTRUCTION_NODE(' + val.nodeName + ':' + val.nodeValue + ')'
break
case8:
ret = 'COMMENT_NODE(' + val.nodeValue + ')'
break
case9:
ret = 'DOCUMENT_NODE'
break
case10:
ret = 'DOCUMENT_TYPE_NODE'
break
case11:
ret = 'DOCUMENT_FRAGMENT_NODE'
break
case12:
ret = 'NOTATION_NODE'
break
}
}
return ret
}
var _formatArray = function (obj, curDepth, padVal, padChar) {
if (curDepth > 0) {
curDepth++
}
const basePad = _repeatChar(padVal * (curDepth - 1), padChar)
const thickPad = _repeatChar(padVal * (curDepth + 1), padChar)
let str = ''
let val = ''
if (typeof obj === 'object' && obj !== null) {
if (obj.constructor && _getFuncName(obj.constructor) === 'LOCUTUS_Resource') {
return obj.var_dump()
}
lgth = 0
for (const someProp in obj) {
if (obj.hasOwnProperty(someProp)) {
lgth++
}
}
str += 'array(' + lgth + ') {\n'
for (const key in obj) {
const objVal = obj[key]
if (typeof objVal === 'object' &&
objVal !== null &&
!(objVal instanceofDate) &&
!(objVal instanceofRegExp) &&
!objVal.nodeName) {
str += thickPad
str += '['
str += key
str += '] =>\n'
str += thickPad
str += _formatArray(objVal, curDepth + 1, padVal, padChar)
} else {
val = _getInnerVal(objVal, thickPad)
str += thickPad
str += '['
str += key
str += '] =>\n'
str += thickPad
str += val
str += '\n'
}
}
str += basePad + '}\n'
} else {
str = _getInnerVal(obj, thickPad)
}
return str
}
output = _formatArray(arguments[0], 0, padVal, padChar)
for (i = 1; i < arguments.length; i++) {
output += '\n' + _formatArray(arguments[i], 0, padVal, padChar)
}
echo(output)
return output
}