2 (function (root, factory) {
4 if (typeof define === 'function' && define.amd) {
5 define(['crel'], factory);
6 } else if (typeof module !== 'undefined' && module.exports) {
7 module.exports = factory(require('../lib/crel'));
9 root.JsonHuman = factory(root.crel);
11 }(this, function (crel) {
13 var toString = Object.prototype.toString,
23 function makePrefixer(prefix) {
24 return function (name) {
25 return prefix + "-" + name;
29 function getType(obj) {
30 var type = typeof obj;
32 if (type === "boolean") {
34 } else if (type === "string") {
36 } else if (type === "number") {
37 return (obj % 1 === 0) ? INT : FLOAT;
38 } else if (type === "function") {
40 } else if (toString.call(obj) === '[object Array]') {
42 } else if (obj === Object(obj)) {
49 function _format(data, prefixer) {
50 var result, container, key, keyNode, valNode,
58 result = crel("span", {"class": p("type-bool")}, "" + data);
62 result = crel("span", {"class": p("type-string")}, "");
63 result.innerHTML = data
64 .replace(/&/g, '&')
65 .replace(/ /g, " ")
66 .replace(/</g, '<')
67 .replace(/>/g, '>')
68 .replace(/[\r\n]/g, '<br/>')
69 .replace(/"/g, '"'); // ")
72 {"class": p("type-string") + " " + p("empty")},
78 {"class": p("type-int") + " " + p("type-number")},
83 {"class": p("type-float") + " " + p("type-number")},
87 result = crel("table", {"class": p("type-object")});
91 {"class": p("key") + " " + p("object-key")},
94 {"class": p("value") + " " + p("object-value")},
95 _format(data[key], p));
96 result.appendChild(crel("tr", keyNode, valNode));
100 result = crel("span",
101 {"class": p("type-object") + " " + p("empty")},
106 result = crel("span", {"class": p("type-function")}, "" + data);
109 if (data.length > 0) {
110 result = crel("table", {"class": p("type-array")});
111 for (key = 0; key < data.length; key += 1) {
113 {"class": p("key") + " " + p("array-key")},
116 {"class": p("value") + " " + p("array-value")},
117 _format(data[key], p));
118 result.appendChild(crel("tr", keyNode, valNode));
121 result = crel("span",
122 {"class": p("type-array") + " " + p("empty")},
127 result = crel("span", {"class": p("type-unk")},
135 function format(data, options) {
136 options = options || {};
138 prefixer = makePrefixer(options.prefix || "jh");
140 result = _format(data, prefixer);
141 result.className = result.className + " " + prefixer("root");