-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlace.js
63 lines (54 loc) · 1.66 KB
/
unlace.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
/**
* unlace
* Copyright(c) 2017 Thai-Duong Nguyen
* MIT Licensed
*/
/**
* JavaScript stringifier.
* @module unlace
*/
/**
* Stringify JavaScript expressions while facilitating deep inspection of JavaScript objects.
* @param {*} data - JavaScript expression to be stringified.
* @param {string} - mode - Medium of screen render. Accepted: 'string', console', and 'DOM'
* @return {string} - Stringified JavaScript expression.
*/
function unlace(data, mode = 'string', spaces = '', seen = []) {
if (data === null) return 'null';
if (data === undefined) return 'undefined';
let newline;
let spacer;
let padding;
switch (mode) {
case 'console':
newline = '\n';
spacer = ' ';
padding = '';
break;
case 'DOM':
newline = '<br>';
spacer = '  ';
padding = '';
break;
case 'string':
newline = '';
spacer = '';
padding = ' ';
break;
default:
throw new Error("2nd argument must be either 'string', 'console', or 'DOM'");
}
if (typeof data === 'string') return `'${data}'`;
if (typeof data === 'object') {
if (seen.includes(data)) return '[circular reference]';
const newSeen = [...seen, data];
if (Array.isArray(data)) return `[${newline}${data.map(e => `${spaces + spacer}${unlace(e, mode, spaces + spacer, newSeen)}`).join(`, ${newline}`) + newline + spaces}]`;
return `{${padding}${newline}${Object.keys(data).map(key => `${spaces + spacer}${key}: ${unlace(data[key], mode, spaces + spacer, newSeen)}`).join(`, ${newline}`)}${newline + spaces + padding}}`;
}
return data.toString();
}
/**
* Module exports.
* @public
*/
module.exports = unlace;