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