Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c0c3c9f
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var type = function(doc) { | ||
if (doc === 'null') return 'null'; | ||
if (Array.isArray(doc)) return 'array'; | ||
if (typeof doc === 'string' && /https?:/.test(doc)) return 'link'; | ||
|
||
return typeof doc; | ||
}; | ||
|
||
var escape = function(str) { | ||
return str.replace(/</g, '<').replace(/>/g, '>'); | ||
}; | ||
|
||
module.exports = function(doc) { | ||
var indent = ''; | ||
|
||
var forEach = function(list, start, end, fn) { | ||
if (!list.length) return start+' '+end; | ||
|
||
var out = start+'\n'; | ||
|
||
indent += ' '; | ||
list.forEach(function(key, i) { | ||
out += indent+fn(key)+(i < list.length-1 ? ',' : '')+'\n'; | ||
}); | ||
indent = indent.slice(0, -4); | ||
|
||
return out + indent+end; | ||
}; | ||
|
||
var visit = function(obj) { | ||
if (obj === undefined) return ''; | ||
|
||
switch (type(obj)) { | ||
case 'boolean': | ||
return '<span class="bool">'+obj+'</span>'; | ||
|
||
case 'number': | ||
return '<span class="number">'+obj+'</span>'; | ||
|
||
case 'null': | ||
return '<span class="null">null</span>\n'; | ||
|
||
case 'string': | ||
return '<span class="string">"'+escape(obj)+'"</span>'; | ||
|
||
case 'link': | ||
return '<span class="string">"<a href="'+escape(obj)+'">'+escape(obj)+'</a>"</span>'; | ||
|
||
case 'array': | ||
return forEach(obj, '[', ']', visit); | ||
|
||
case 'object': | ||
var keys = Object.keys(obj).filter(function(key) { | ||
return obj[key] !== undefined; | ||
}); | ||
|
||
return forEach(keys, '{', '}', function(key) { | ||
return '<span class="key">'+key + ':</span> '+visit(obj[key]); | ||
}); | ||
} | ||
|
||
return ''; | ||
}; | ||
|
||
return '<pre>'+visit(doc)+'</pre>'; | ||
}; |