1 | // Generated by CoffeeScript 1.12.7
|
---|
2 | var object, self,
|
---|
3 | hasProp = {}.hasOwnProperty;
|
---|
4 |
|
---|
5 | object = require('utila').object;
|
---|
6 |
|
---|
7 | module.exports = self = {
|
---|
8 | sanitize: function(val) {
|
---|
9 | return self._toChildren(val);
|
---|
10 | },
|
---|
11 | _toChildren: function(val) {
|
---|
12 | var ref;
|
---|
13 | if (object.isBareObject(val)) {
|
---|
14 | return self._objectToChildren(val);
|
---|
15 | } else if (Array.isArray(val)) {
|
---|
16 | return self._arrayToChildren(val);
|
---|
17 | } else if (val === null || typeof val === 'undefined') {
|
---|
18 | return [];
|
---|
19 | } else if ((ref = typeof val) === 'string' || ref === 'number') {
|
---|
20 | return [String(val)];
|
---|
21 | } else {
|
---|
22 | throw Error("not a valid child node: `" + val);
|
---|
23 | }
|
---|
24 | },
|
---|
25 | _objectToChildren: function(o) {
|
---|
26 | var a, cur, key, val;
|
---|
27 | a = [];
|
---|
28 | for (key in o) {
|
---|
29 | if (!hasProp.call(o, key)) continue;
|
---|
30 | val = o[key];
|
---|
31 | cur = {};
|
---|
32 | cur[key] = self.sanitize(val);
|
---|
33 | a.push(cur);
|
---|
34 | }
|
---|
35 | return a;
|
---|
36 | },
|
---|
37 | _arrayToChildren: function(a) {
|
---|
38 | var i, len, ret, v;
|
---|
39 | ret = [];
|
---|
40 | for (i = 0, len = a.length; i < len; i++) {
|
---|
41 | v = a[i];
|
---|
42 | ret.push(self._toNode(v));
|
---|
43 | }
|
---|
44 | return ret;
|
---|
45 | },
|
---|
46 | _toNode: function(o) {
|
---|
47 | var key, keys, obj, ref;
|
---|
48 | if ((ref = typeof o) === 'string' || ref === 'number') {
|
---|
49 | return String(o);
|
---|
50 | } else if (object.isBareObject(o)) {
|
---|
51 | keys = Object.keys(o);
|
---|
52 | if (keys.length !== 1) {
|
---|
53 | throw Error("a node must only have one key as tag name");
|
---|
54 | }
|
---|
55 | key = keys[0];
|
---|
56 | obj = {};
|
---|
57 | obj[key] = self._toChildren(o[key]);
|
---|
58 | return obj;
|
---|
59 | } else {
|
---|
60 | throw Error("not a valid node: `" + o + "`");
|
---|
61 | }
|
---|
62 | }
|
---|
63 | };
|
---|