Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (44 loc) · 1.66 KB
/
Copy pathindex.js
File metadata and controls
49 lines (44 loc) · 1.66 KB
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
var inputElement = document.getElementById('input')
var outputElement = document.getElementById('output')
var previewElement = document.getElementById('preview')
var inputJSON = {}
var outputHTML = ''
inputElement.addEventListener('keyup', function () {
covert(inputElement.value)
outputElement.value = outputHTML
previewElement.innerHTML = outputHTML
})
// Conversion controller
function covert(input) {
try {
inputJSON = JSON.parse(input)
} catch (e) {
return e
}
if (inputJSON) {
outputHTML = `<details><summary>Object</summary>`
recursiveCreateHTMLString(inputJSON)
outputHTML += `</details>`
}
}
// Actually does the html string creation (recursively)
function recursiveCreateHTMLString(object) {
for (key in object) {
let peak = JSON.stringify(object[key])
if (peak.length > 75) {
peak = peak.substring(0, 74) + "...";
}
outputHTML += `<blockquote><details><summary><span><b>${key}</b> <var> - ${peak}</var></span></summary>`
if (typeof object[key] === 'object') {
recursiveCreateHTMLString(object[key])
} else {
outputHTML += `<blockquote><code>${JSON.stringify(object[key]) || 'null'}</code></blockquote>`
}
outputHTML += `</details></blockquote>`
}
}
// Run initial example
inputElement.value = '{ "fruits": { "apple": "red", "orange": "orange", "banana": "yellow" }, "animals": { "cat": "small", "whale": "big", "cow": "medium", "cat types": ["grumpy cats", "orange cats", "hungry cats", "Garfield"] } }'
covert(inputElement.value)
outputElement.value = outputHTML
previewElement.innerHTML = outputHTML