Switch over to more modern unix sharedmemory API (requested by fatbob)
[umurmur.git] / shm_utils / umurmurd-websocket / web / js / json.human.js
1 /*globals define*/
2 (function (root, factory) {
3     "use strict";
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'));
8     } else {
9         root.JsonHuman = factory(root.crel);
10     }
11 }(this, function (crel) {
12     "use strict";
13     var toString = Object.prototype.toString,
14         ARRAY = 1,
15         BOOL = 2,
16         INT = 3,
17         FLOAT = 4,
18         STRING = 5,
19         OBJECT = 6,
20         FUNCTION = 7,
21         UNK = 99;
22
23     function makePrefixer(prefix) {
24         return function (name) {
25             return prefix + "-" + name;
26         };
27     }
28
29     function getType(obj) {
30         var type = typeof obj;
31
32         if (type === "boolean") {
33             return BOOL;
34         } else if (type === "string") {
35             return STRING;
36         } else if (type === "number") {
37             return (obj % 1 === 0) ? INT : FLOAT;
38         } else if (type === "function") {
39             return FUNCTION;
40         } else if (toString.call(obj) === '[object Array]') {
41             return ARRAY;
42         } else if (obj === Object(obj)) {
43             return OBJECT;
44         } else {
45             return UNK;
46         }
47     }
48
49     function _format(data, prefixer) {
50         var result, container, key, keyNode, valNode,
51             isEmpty = true,
52             p = prefixer,
53             accum = [],
54             type = getType(data);
55
56         switch (type) {
57         case BOOL:
58             result = crel("span", {"class": p("type-bool")}, "" + data);
59             break;
60         case STRING:
61             if (data !== "") {
62                 result = crel("span", {"class": p("type-string")}, "");
63                 result.innerHTML = data
64                     .replace(/&/g, '&')
65                     .replace(/ /g, " ")
66                     .replace(/</g, '&lt;')
67                     .replace(/>/g, '&gt;')
68                     .replace(/[\r\n]/g, '<br/>')
69                     .replace(/"/g, '&quot;'); // ")
70             } else {
71                 result = crel("span",
72                          {"class": p("type-string") + " " + p("empty")},
73                          "(Empty Text)");
74             }
75             break;
76         case INT:
77             result = crel("span",
78                          {"class": p("type-int") + " " + p("type-number")},
79                           "" + data);
80             break;
81         case FLOAT:
82             result = crel("span",
83                          {"class": p("type-float") + " " + p("type-number")},
84                          "" + data);
85             break;
86         case OBJECT:
87             result = crel("table", {"class": p("type-object")});
88             for (key in data) {
89                 isEmpty = false;
90                 keyNode = crel("th",
91                          {"class": p("key") + " " + p("object-key")},
92                          "" + key);
93                 valNode = crel("td",
94                          {"class": p("value") + " " + p("object-value")},
95                          _format(data[key], p));
96                 result.appendChild(crel("tr", keyNode, valNode));
97             }
98
99             if (isEmpty) {
100                 result = crel("span",
101                          {"class": p("type-object") + " " + p("empty")},
102                          "(Empty Object)");
103             }
104             break;
105         case FUNCTION:
106             result = crel("span", {"class": p("type-function")}, "" + data);
107             break;
108         case ARRAY:
109             if (data.length > 0) {
110                 result = crel("table", {"class": p("type-array")});
111                 for (key = 0; key < data.length; key += 1) {
112                     keyNode = crel("th",
113                              {"class": p("key") + " " + p("array-key")},
114                              "" + key);
115                     valNode = crel("td",
116                              {"class": p("value") + " " + p("array-value")},
117                              _format(data[key], p));
118                     result.appendChild(crel("tr", keyNode, valNode));
119                 }
120             } else {
121                 result = crel("span",
122                          {"class": p("type-array") + " " + p("empty")},
123                          "(Empty List)");
124             }
125             break;
126         default:
127             result = crel("span", {"class": p("type-unk")},
128                           "" + data);
129             break;
130         }
131
132         return result;
133     }
134
135     function format(data, options) {
136         options = options || {};
137         var result,
138             prefixer = makePrefixer(options.prefix || "jh");
139
140         result = _format(data, prefixer);
141         result.className = result.className + " " + prefixer("root");
142
143         return result;
144     }
145
146     return {
147         format: format
148     };
149 }));