-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
dump.js
250 lines (227 loc) · 5.38 KB
/
dump.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const { red, cyan, blue, black, green, yellow } = require('kleur');
/**
* Generate structured information about one or more objects that
* includes each item type and value.
*
* @author Zeeshan Ahmad <ziishaned@gmail.com>
*/
class Dump {
constructor() {
this.spaces = ' '.repeat(4);
}
/**
* Iterate over each property of the provided object and make the output.
*
* @param {*} obj
* @param {string|null} tab
* @return {string}
*/
start(obj, tab = '') {
let dump = '';
const isArray = this.getType(obj) === 'array';
if (isArray) {
dump = `${black.bold('array')} (size=${obj.length}) [\n`;
} else {
dump = `${black.bold('object')} (size=${Object.keys(obj).length}) {\n`;
}
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
let val = '';
const param = obj[key];
const paramType = this.getType(param);
switch (paramType) {
case 'array':
case 'object':
val = this.start(param, `${tab}${this.spaces}`);
break;
case 'boolean':
val = param ? `boolean ${cyan('true')}` : `boolean ${cyan('false')}`;
break;
case 'string':
val = `string ${red(`"${param}"`)} (length=${param.length})`;
break;
case 'null':
val = `${blue('null')}`;
break;
case 'number':
val = Number.isInteger(param) ? `int ${green(param)}` : `float ${yellow(param)}`;
break;
case 'function':
val = 'function () {}';
break;
case 'regexp':
val = `${blue(param)}`;
break;
default:
val = param;
break;
}
dump += this.makeArrowString(paramType, tab, key, val);
}
}
dump = `${dump.substring(0, dump.length - 2)}\n${tab}`;
return isArray ? `${dump}]` : `${dump}}`;
}
/**
* Make the arrow string.
*
* @param {string} paramType
* @param {string} tab
* @param {string} key
* @param {*} val
* @return {string}
*/
makeArrowString(paramType, tab, key, val) {
if (paramType === 'array') {
if (typeof key === 'string') {
return `${tab}${this.spaces}'${key}' => ${val},\n`;
}
return `${tab}${this.spaces}[${key}] => ${val},\n`;
} else {
if (Number.isInteger(parseInt(key))) {
return `${tab}${this.spaces}[${key}] => ${val},\n`;
}
return `${tab}${this.spaces}'${key}' => ${val},\n`;
}
}
/**
* Get the type of the passed variable.
*
* @param {*} obj
* @return {string}
*/
getType(obj) {
if (this.isNull(obj)) {
return 'null';
} else if (this.isUndefined(obj)) {
return 'undefined';
} else if (this.isFunction(obj)) {
return 'function';
} else if (this.isString(obj)) {
return 'string';
} else if (this.isArray(obj)) {
return 'array';
} else if (this.isBoolean(obj)) {
return 'boolean';
} else if (this.isDate(obj)) {
return 'date';
} else if (this.isHTML(obj)) {
return 'html';
} else if (this.isObject(obj)) {
return 'object';
} else if (this.isRegExp(obj)) {
return 'regexp';
} else if (this.isNumber(obj)) {
return 'number';
}
}
/**
* Check if variable type is null.
*
* @param {*} obj
* @return {boolean}
*/
isNull(obj) {
return obj === null;
}
/**
* Check if variable type is undefined.
*
* @param {*} obj
* @return {boolean}
*/
isUndefined(obj) {
return obj === undefined;
}
/**
* Check if variable type is function.
*
* @param {*} obj
* @return {boolean}
*/
isFunction(obj) {
return typeof obj === 'function' ? this.objectToString(obj).match(/Function/) !== null : false;
}
/**
* Check if variable type is string.
*
* @param {*} obj
* @return {boolean}
*/
isString(obj) {
return typeof obj === 'string' ? true : typeof obj === 'object' ? this.objectToString(obj).match(/string/i) !== null : false;
}
/**
* Check if variable type is array.
*
* @param {*} obj
* @return {boolean}
*/
isArray(obj) {
return typeof obj === 'object' ? this.objectToString(obj).match(/array/i) !== null || obj.length !== undefined : false;
}
/**
* Check if variable type is boolean.
*
* @param {*} obj
* @return {boolean}
*/
isBoolean(obj) {
return typeof obj === 'boolean' ? true : typeof obj === 'object' ? this.objectToString(obj).match(/boolean/i) !== null : false;
}
/**
* Check if variable type is date.
*
* @param {*} obj
* @return {boolean}
*/
isDate(obj) {
return typeof obj === 'object' ? this.objectToString(obj).match(/date/i) !== null : false;
}
/**
* Check if variable type is html.
*
* @param {*} obj
* @return {boolean}
*/
isHTML(obj) {
return typeof obj === 'object' ? this.objectToString(obj).match(/html/i) !== null : false;
}
/**
* Check if variable type is object.
*
* @param {*} obj
* @return {boolean}
*/
isObject(obj) {
return typeof obj === 'object' ? this.objectToString(obj).match(/object/i) !== null : false;
}
/**
* Check if variable type is regular expression.
*
* @param {*} obj
* @return {boolean}
*/
isRegExp(obj) {
return typeof obj === 'object' ? this.objectToString(obj).match(/regexp/i) !== null : false;
}
/**
* Check if variable type is number.
*
* @param {*} obj
* @return {boolean}
*/
isNumber(obj) {
return typeof obj === 'number' ? true : typeof obj === 'object' ? this.objectToString(obj).match(/Number/) !== null : false;
}
/**
* Get the constructor name from passed object.
*
* @param {*} obj
* @return {string}
*/
objectToString(obj) {
return obj.constructor.toString();
}
}
module.exports = Dump;