PHP's var_dump in JavaScript

Here’s what our current JavaScript equivalent to PHP's var_dump looks like.

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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
module.exports = function var_dump () { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/var_dump/
// original by: Brett Zamir (http://brett-zamir.me)
// improved by: Zahlii
// improved by: Brett Zamir (http://brett-zamir.me)
// note 1: For returning a string, use var_export() with the second argument set to true
// test: skip-all
// example 1: var_dump(1)
// returns 1: 'int(1)'

var echo = require('../strings/echo')
var output = ''
var padChar = ' '
var padVal = 4
var lgth = 0
var i = 0

var _getFuncName = function (fn) {
var name = (/\W*function\s+([\w\$]+)\s*\(/)
.exec(fn)
if (!name) {
return '(Anonymous)'
}
return name[1]
}

var _repeatChar = function (len, padChar) {
var str = ''
for (var i = 0; i < len; i++) {
str += padChar
}
return str
}
var _getInnerVal = function (val, thickPad) {
var ret = ''
if (val === null) {
ret = 'NULL'
} else if (typeof val === 'boolean') {
ret = 'bool(' + val + ')'
} else if (typeof val === 'string') {
ret = 'string(' + val.length + ') "' + val + '"'
} else if (typeof val === 'number') {
if (parseFloat(val) === parseInt(val, 10)) {
ret = 'int(' + val + ')'
} else {
ret = 'float(' + val + ')'
}
} else if (typeof val === 'undefined') {
// The remaining are not PHP behavior because these values
// only exist in this exact form in JavaScript
ret = 'undefined'
} else if (typeof val === 'function') {
var funcLines = val.toString()
.split('\n')
ret = ''
for (var i = 0, fll = funcLines.length; i < fll; i++) {
ret += (i !== 0 ? '\n' + thickPad : '') + funcLines[i]
}
} else if (val instanceof Date) {
ret = 'Date(' + val + ')'
} else if (val instanceof RegExp) {
ret = 'RegExp(' + val + ')'
} else if (val.nodeName) {
// Different than PHP's DOMElement
switch (val.nodeType) {
case 1:
if (typeof val.namespaceURI === 'undefined' ||
val.namespaceURI === 'http://www.w3.org/1999/xhtml') {
// Undefined namespace could be plain XML, but namespaceURI not widely supported
ret = 'HTMLElement("' + val.nodeName + '")'
} else {
ret = 'XML Element("' + val.nodeName + '")'
}
break
case 2:
ret = 'ATTRIBUTE_NODE(' + val.nodeName + ')'
break
case 3:
ret = 'TEXT_NODE(' + val.nodeValue + ')'
break
case 4:
ret = 'CDATA_SECTION_NODE(' + val.nodeValue + ')'
break
case 5:
ret = 'ENTITY_REFERENCE_NODE'
break
case 6:
ret = 'ENTITY_NODE'
break
case 7:
ret = 'PROCESSING_INSTRUCTION_NODE(' + val.nodeName + ':' + val.nodeValue + ')'
break
case 8:
ret = 'COMMENT_NODE(' + val.nodeValue + ')'
break
case 9:
ret = 'DOCUMENT_NODE'
break
case 10:
ret = 'DOCUMENT_TYPE_NODE'
break
case 11:
ret = 'DOCUMENT_FRAGMENT_NODE'
break
case 12:
ret = 'NOTATION_NODE'
break
}
}
return ret
}

var _formatArray = function (obj, curDepth, padVal, padChar) {
if (curDepth > 0) {
curDepth++
}

var basePad = _repeatChar(padVal * (curDepth - 1), padChar)
var thickPad = _repeatChar(padVal * (curDepth + 1), padChar)
var str = ''
var val = ''

if (typeof obj === 'object' && obj !== null) {
if (obj.constructor && _getFuncName(obj.constructor) === 'LOCUTUS_Resource') {
return obj.var_dump()
}
lgth = 0
for (var someProp in obj) {
if (obj.hasOwnProperty(someProp)) {
lgth++
}
}
str += 'array(' + lgth + ') {\n'
for (var key in obj) {
var objVal = obj[key]
if (typeof objVal === 'object' &&
objVal !== null &&
!(objVal instanceof Date) &&
!(objVal instanceof RegExp) &&
!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)

// Not how PHP does it, but helps us test:
return output
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/var/var_dump'). You could also require the var module in full so that you could access var.var_dump instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

  • For returning a string, use var_export() with the second argument set to true

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1var_dump(1)'int(1)'

« More PHP var functions