-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·109 lines (100 loc) · 4.83 KB
/
index.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
(function() {
if(typeof document === "undefined") {
// node.js
var jsdom = require('jsdom');
document = jsdom.jsdom("<html><body><h1>Hello</h1></body></html>", {
features: {
// QuerySelector must be turned on on the specific document we're creating
QuerySelector: true
}
});
}
})();
module.exports = {
toHtmlDom: function ( json ) {
function prettyPrint( json, root ) {
var type = typeof json;
switch(type) {
case 'string' : var d = document.createElement('span');
d.appendChild(document.createTextNode(json));
return d;
break;
case 'number' :
case 'undefined' :
case 'boolean' :
var span = document.createElement('span');
span.appendChild(document.createTextNode(json));
return span;
break;
default:
if (Array.isArray(json)) {
json = json.map(prettyPrint, root);
var arrayOpening = document.createTextNode('['),
arrayClosing = document.createTextNode(']'),
containerDiv = document.createElement('div');
containerDiv.appendChild(json.reduce(function(prev, cur) {
var comma = document.createTextNode(','),
br = document.createElement('br');
prev.appendChild(comma);
prev.appendChild(br);
prev.appendChild(cur);
return prev;
}));
containerDiv.style.marginLeft = "30px";
var documentFrag = document.createElement('div');
documentFrag.appendChild(arrayOpening);
documentFrag.appendChild(containerDiv);
documentFrag.appendChild(arrayClosing);
return documentFrag;
}
else {
//none of the above so it is the object
if (json !== null) {
var keys = Object.keys(json);
var htmlArray = [],
div = null,
br = document.createElement('br'),
span2 = null,
colon = null,
strong = null;
for(var i = 0, len = keys.length; i < len; i++) {
var k = keys[i];
if(k.indexOf(' ') !== -1) {
k = '"' + k + '"';
}
div = document.createElement('div'),
strong = document.createElement('strong');
strong.textContent = k;
div.style.marginLeft = "30px";
colon = document.createTextNode(': ');
div.appendChild(strong).appendChild(colon);
div.appendChild(prettyPrint(json[keys[i]], div));
if((len - i) !== 1) {
div.appendChild(document.createTextNode(','));
}
htmlArray.push(div);
}
var openingBracket = document.createElement('span');
openingBracket.appendChild(document.createTextNode('{'));
var documentDiv = document.createElement('div');
documentDiv.appendChild(openingBracket);
htmlArray.forEach(function(docFrag) {
documentDiv.appendChild(docFrag);
});
var closingBracket = document.createElement('span');
closingBracket.appendChild(document.createTextNode('}'));
documentDiv.appendChild(closingBracket);
return documentDiv;
}
return root;
}
}
}
var root = document.createElement('div');
var html = prettyPrint(json, root);
return html;
},
toHtmlString: function( json ) {
return this.toHtmlDom(json).innerHTML;
}
};