1 | exports.condenseArray = function(items) {
|
2 | var currentTag
|
3 | , newTag
|
4 | , currentTagCompressed = null
|
5 | , output = []
|
6 | ;
|
7 |
|
8 | items.forEach(function(item) {
|
9 | if(typeof item === 'string') {
|
10 | if(currentTag === '$text') {
|
11 | currentTagCompressed += item;
|
12 | } else {
|
13 | if(currentTagCompressed !== null) {output.push(currentTagCompressed);}
|
14 | currentTagCompressed = item;
|
15 | currentTag = '$text';
|
16 | }
|
17 | } else {
|
18 | newTag = item.$name;
|
19 | if(newTag === currentTag) {
|
20 | currentTagCompressed.push(item);
|
21 | } else {
|
22 | if(currentTagCompressed !== null) {output.push(currentTagCompressed);}
|
23 | currentTagCompressed = [item];
|
24 | currentTag = newTag;
|
25 | }
|
26 | }
|
27 | });
|
28 | if(currentTagCompressed !== null) {output.push(currentTagCompressed);}
|
29 | return output;
|
30 | };
|
31 |
|
32 | function shallowClone(obj) {
|
33 | var output = {}, key;
|
34 | if (obj === null || typeof obj !== 'object') {return obj;}
|
35 | for(key in obj) {output[key] = obj[key];}
|
36 | return output;
|
37 | }
|
38 |
|
39 | exports.simplifyNode = function(node, dropName) {
|
40 | if(!exports.isSomething(node)) {return null;}
|
41 | if (node === null || typeof node !== 'object') {return shallowClone(node);}
|
42 | if (node.constructor === Array) {return node.length === 1 ? node[0]: node;}
|
43 |
|
44 | var output = {}
|
45 | , keys = Object.keys(node)
|
46 | ;
|
47 |
|
48 | dropName = !!dropName;
|
49 |
|
50 | keys.forEach(function(key) {
|
51 | var value = node[key]
|
52 | , markup
|
53 | ;
|
54 |
|
55 | if(key === '$markup') {
|
56 | markup = simplifyAll(value);
|
57 | if(markup.length > 0) {output.$markup = markup;}
|
58 | } else if(!(dropName && key === '$name') && exports.isSomething(value)) {
|
59 | if(value.constructor === Array && value.length === 1) {
|
60 | output[key] = value[0];
|
61 | } else {
|
62 | output[key] = value;
|
63 | }
|
64 | }
|
65 | });
|
66 |
|
67 | keys = Object.keys(output);
|
68 | if(keys.length === 1) {
|
69 | output = shallowClone(output[keys[0]]);
|
70 | } else if(keys.length === 2 && !dropName && output.hasOwnProperty('$attrs')) {
|
71 | output = shallowClone(output.$attrs);
|
72 | output.$name = node.$name;
|
73 | }
|
74 | return output;
|
75 | };
|
76 |
|
77 | exports.shouldObjectifyMarkup = function(items) {
|
78 | var currentTag
|
79 | , foundTags = {}
|
80 | , shouldObjectify = true
|
81 | ;
|
82 | items.forEach(function(item){
|
83 | if(typeof item === 'string') {
|
84 | currentTag = '$text';
|
85 | } else {
|
86 | currentTag = item[0].$name;
|
87 | }
|
88 |
|
89 | if(foundTags[currentTag]) {
|
90 | shouldObjectify = false;
|
91 | return false;
|
92 | }
|
93 | foundTags[currentTag] = true;
|
94 | });
|
95 | return shouldObjectify;
|
96 | };
|
97 |
|
98 | exports.moosh = function(item1, item2) {
|
99 | if(item1 === undefined) {
|
100 | if(item2 !== null && typeof item2 === 'object' && item2.constructor === Array && item2.length === 1) {
|
101 | return item2[0];
|
102 | }
|
103 | return item2;
|
104 | }
|
105 |
|
106 | if(item1 !== null && typeof item1 === 'object' && item1.constructor === Array) {
|
107 | if(item2 !== null && typeof item2 === 'object' && item2.constructor === Array) {
|
108 | return item1.concat(item2);
|
109 | }
|
110 | item1.push(item2);
|
111 | return item1;
|
112 | }
|
113 |
|
114 | if(item2 !== null && typeof item2 === 'object' && item2.constructor === Array) {
|
115 | return [item1].concat(item2);
|
116 | }
|
117 |
|
118 | return [item1, item2];
|
119 | };
|
120 |
|
121 | function simplifyAll(input, dropName) {
|
122 | var output = [];
|
123 | if(input !== null && typeof input === 'object' && input.constructor === Array) {
|
124 | input.forEach(function(item){
|
125 | output.push(exports.simplifyNode(item, dropName));
|
126 | });
|
127 | return output;
|
128 | }
|
129 | return exports.simplifyNode(input, dropName);
|
130 | }
|
131 |
|
132 | exports.objectifyMarkup = function(node, dontSimplify) {
|
133 | var key
|
134 | , output = {}
|
135 | ;
|
136 |
|
137 | dontSimplify = !!dontSimplify;
|
138 |
|
139 | Object.keys(node).forEach(function(key){
|
140 | if(key !== '$markup') {
|
141 | output[key] = node[key];
|
142 | }
|
143 | });
|
144 |
|
145 | if(node.$markup) {
|
146 | node.$markup.forEach(function(item){
|
147 | if(typeof item === 'string') {
|
148 | output.$text = exports.moosh(output.$text, item);
|
149 | } else if(typeof item === 'object') {
|
150 | if(item.constructor === Array) {
|
151 | key = item[0].$name;
|
152 | } else {
|
153 | key = item.$name;
|
154 | }
|
155 | if(!dontSimplify) {
|
156 | item = simplifyAll(item, true);
|
157 | }
|
158 | output[key] = exports.moosh(output[key], item);
|
159 | }
|
160 | });
|
161 | }
|
162 | return output;
|
163 | };
|
164 |
|
165 | exports.isSomething = function(n) {
|
166 | if(n === undefined || n === null) {
|
167 | return false;
|
168 | }
|
169 |
|
170 | if(n.constructor === Array && n.length === 0) {
|
171 | return false;
|
172 | }
|
173 |
|
174 | if(typeof n === 'object' && Object.keys(n).length === 0) {
|
175 | return false;
|
176 | }
|
177 |
|
178 | if(typeof n === 'string' && n.length === 0) {
|
179 | return false;
|
180 | }
|
181 |
|
182 | return true;
|
183 | };
|
184 |
|