forked from canonn-science/Signals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty-print-json.js
48 lines (43 loc) · 2.31 KB
/
pretty-print-json.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
// pretty-print-json ~ MIT License
const prettyPrintJson = {
version: '[VERSION]',
toHtml: (thing) => {
const htmlEntities = (string) => {
// Makes text displayable in browsers
return string
.replace(/&/g, '&')
.replace(/\\"/g, '\"')
.replace(/</g, '<')
.replace(/>/g, '>');
};
const replacer = (match, p1, p2, p3, p4) => {
// Converts the four parenthesized capture groups (indent, key, value, end) into HTML
const part = { indent: p1, key: p2, value: p3, end: p4 };
const key = '<span class=json-key>';
const val = '<span class=json-value>';
const bool = '<span class=json-boolean>';
const str = '<span class=json-string>';
const isBool = ['true', 'false'].includes(part.value);
const valSpan = /^"/.test(part.value) ? str : isBool ? bool : val;
const findName = /"([\w]+)": |(.*): /;
const indentHtml = part.indent || '';
const keyHtml = part.key ? key + part.key.replace(findName, '$1$2') + '</span>: ' : '';
const valueHtml = part.value ? valSpan + part.value + '</span>' : '';
const endHtml = part.end || '';
return indentHtml + keyHtml + valueHtml + endHtml;
};
const jsonLine = /^( *)("[^"]+": )?("[^"]*"|[\w.+-]*)?([{}[\],]*)?$/mg;
// Regex parses each line of the JSON string into four parts:
// Capture group Part Description ' "active": true,'
// ------------------ ---------- --------------------------- --------------------
// ( *) p1: indent Spaces for indentation ' '
// ("[^"]+": ) p2: key Key name '"active": '
// ("[^"]*"|[\w.+-]*) p3: value Key value 'true'
// ([{}[\],]*) p4: end Line termination characters ','
return htmlEntities(JSON.stringify(thing, null, 3)).replace(jsonLine, replacer);
}
};
if (typeof module === 'object')
module.exports = prettyPrintJson; //node module loading system (CommonJS)
if (typeof window === 'object')
window.prettyPrintJson = prettyPrintJson; //support both global and window property