-
Notifications
You must be signed in to change notification settings - Fork 1
/
API.Utils.js
226 lines (208 loc) · 8.24 KB
/
API.Utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Copyright (c) 2014 by Center Open Middleware. All Rights Reserved.
* Titanium Appcelerator 3.3.0GA
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
'use strict';
var Utils = function (APIReferences) {
var Yaast = {
"API" : APIReferences
};
/** It provides several generic useful methods
* @alias API.Utils
* @namespace */
var _self = {};
/** 1 level merge object. (Priority obj2)
* @method
* @return {object} */
_self.mergeObject_old = function mergeObject_old(obj1, obj2) {
var result = null;
var key;
if (obj1 !== null && obj2 !== null) {
// Clone obj1 in result
result = {};
for (key in obj1) {
result[key] = obj1[key];
}
// Merge result with obj2
for (key in obj2) {
result[key] = obj2[key];
}
}
return result;
};
/** Deep fusion object . Without modifying the parameters. (Priority obj2)
* @method
* @return {object} */
_self.mergeObject= function mergeObject(obj1, obj2) {
if(typeof obj1 !== 'object' || typeof obj2 !== 'object' || obj1 === null || typeof obj2 == null || (Array.isArray(obj1) && !Array.isArray(obj2)) || (!Array.isArray(obj2) && Array.isArray(obj1))) {
Ti.API.warn('Impossible to merge this params');
return null;
}
var clon = _self.clone(obj1);
var result = _self.recMerge(clon, obj2);
return result;
};
/** Clone the object
* @method
* @return {object} */
_self.clone = function clone(obj1) {
var result;
if (obj1 == null) {
return obj1;
} else if (Array.isArray(obj1)) {
result = [];
} else if (typeof obj1 === 'object') {
result = {};
} else {
return obj1;
}
for (var key in obj1) {
result[key] = _self.clone(obj1[key]);
}
return result;
};
/** Deep merge in obj1 object. (Priority obj2)
* @method
* @return {object} */
_self.recMerge = function recMerge(obj1, obj2) {
if (Array.isArray(obj2) && Array.isArray(obj1)) {
// Merge Arrays
var i;
for (i = 0; i < obj2.length; i ++) {
if (typeof obj2[i] === 'object' && typeof obj1[i] === 'object') {
obj1[i] = _self.recMerge(obj1[i], obj2[i]);
} else {
obj1[i] = obj2[i];
}
}
} else if (Array.isArray(obj2)) {
// Priority obj2
obj1 = obj2;
} else {
// object casej
for (var p in obj2) {
if(obj1.hasOwnProperty(p)){
if (typeof obj2[p] === 'object' && typeof obj1[p] === 'object') {
obj1[p] = _self.recMerge(obj1[p], obj2[p]);
} else {
obj1[p] = obj2[p];
}
} else {
obj1[p] = obj2[p];
}
}
}
return obj1;
};
/* Batería de pruebas del método mergeObject. (Para ejecutarlas puedes meterlas en app.js, por ejemplo)
Ti.API.info('***********Prueba MERGE**************');
Ti.API.info('\n**Pruebas Simples:');
Ti.API.info('merge(12, {a:[2,3,4]}): ' + JSON.stringify(Yaast["MergeObject"](12, {a:[2,3,4]})));
Ti.API.info('merge({a:12}, 2): ' + JSON.stringify(Yaast["MergeObject"]({a:12}, 2)));
Ti.API.info('merge([], 2): ' + JSON.stringify(Yaast["MergeObject"]([], 2)));
Ti.API.info('merge(null, {pepe:2}): ' + JSON.stringify(Yaast["MergeObject"](null, {pepe:2})));
Ti.API.info('merge({}, {pepe:2}): ' + JSON.stringify(Yaast["MergeObject"]({}, {pepe:2})));
Ti.API.info('merge({pepe:2}, {}): ' + JSON.stringify(Yaast["MergeObject"]({pepe:2}, {})));
Ti.API.info('merge({pepe:2}, {pepe:69}): ' + JSON.stringify(Yaast["MergeObject"]({pepe:2}, {pepe:69})));
Ti.API.info('merge({a:2}, {b:69}): ' + JSON.stringify(Yaast["MergeObject"]({a:2}, {b:69})));
Ti.API.info('\n**Pruebas multinivel:');
Ti.API.info('merge({a:"muete!!"}, {a:{b:1,c:2,d:3}}): ' + JSON.stringify(Yaast["MergeObject"]({a:"muete!!"}, {a:{b:1,c:2,d:3}})));
Ti.API.info('merge({a:{b:1,c:2,d:3}}, {a:"muete!!"}): ' + JSON.stringify(Yaast["MergeObject"]({a:{b:1,c:2,d:3}}, {a:"muete!!"})));
Ti.API.info('merge({a:{b:1,c:2,d:{d1:[], d2: 69, d3: "trol"}}}, {a:{}}): ' + JSON.stringify(Yaast["MergeObject"]({a:{b:1,c:2,d:{d1:[], d2: 69, d3: "trol"}}}, {a:{}})));
Ti.API.info('merge({a:{}}, {a:{b:1,c:2,d:{d1:[], d2: 69, d3: "trol"}}}): ' + JSON.stringify(Yaast["MergeObject"]({a:{}}, {a:{b:1,c:2,d:{d1:[], d2: 69, d3: "trol"}}})));
Ti.API.info('merge({a:{e:"muete!!"}}, {a:{b:1,c:2,d:3}}): ' + JSON.stringify(Yaast["MergeObject"]({a:{e:"muete!!"}}, {a:{b:1,c:2,d:3}})));
Ti.API.info('merge({a:{e:"muete!!"}}, {a:{b:1,c:2,d:3}}): ' + JSON.stringify(Yaast["MergeObject"]({a:{b:1,c:2,d:3}}, {a:{e:"muete!!"}})));
Ti.API.info('merge({a:{e:"muete!!"}}, {a:{b:1,c:2,d:3, e:"susto"}}): ' + JSON.stringify(Yaast["MergeObject"]({a:{e:"muete!!"}}, {a:{b:1,c:2,d:3, e:"susto"}})));
Ti.API.info('merge({a:{b:1,c:2,d:3, e:"susto"}}, {a:{e:"muete!!"}}): ' + JSON.stringify(Yaast["MergeObject"]({a:{b:1,c:2,d:3, e:"susto"}}, {a:{e:"muete!!"}})));
Ti.API.info('\n**Pruebas arrays:');
Ti.API.info('merge({a:"muete!!"}, []): ' + JSON.stringify(Yaast["MergeObject"]({a:"muete!!"}, [])));
Ti.API.info('merge([], {a:"muete!!"}): ' + JSON.stringify(Yaast["MergeObject"]([], {a:"muete!!"})));
Ti.API.info('merge([1,2,3,4], []): ' + JSON.stringify(Yaast["MergeObject"]([1,2,3,4], [])));
Ti.API.info('merge([], [1,2,3,4]): ' + JSON.stringify(Yaast["MergeObject"]([], [1,2,3,4])));
Ti.API.info('merge([11,22,33,44,55], [1,2,3,4]): ' + JSON.stringify(Yaast["MergeObject"]([11,22,33,44,55], [1,2,3,4])));
Ti.API.info('merge([1,2,3,4], [11,22,33,44,55]): ' + JSON.stringify(Yaast["MergeObject"]([1,2,3,4], [11,22,33,44,55])));
Ti.API.info('merge([1,2,3,4], [{a:"tuano"}, 22,33]): ' + JSON.stringify(Yaast["MergeObject"]([1,2,3,4], [{a:"tuano"}, 22,33])));
Ti.API.info('merge([{a:"tuano"}, 22,33], [1,2,3,4]): ' + JSON.stringify(Yaast["MergeObject"]([{a:"tuano"}, 22,33], [1,2,3,4])));
Ti.API.info('merge([1,2,3,4], [{a:"tuano"}, 22,33,44,55]): ' + JSON.stringify(Yaast["MergeObject"]([1,2,3,4], [{a:"tuano"}, 22,33,44,55])));
Ti.API.info('merge([{a:"tuano"}, 22,33,44,55], [1,2,3,4]): ' + JSON.stringify(Yaast["MergeObject"]([{a:"tuano"}, 22,33,44,55], [1,2,3,4])));
Ti.API.info('\n**Prueba Complejas 1:');
Ti.API.info('obj1:' + JSON.stringify(objetoP1));
Ti.API.info('obj2:' + JSON.stringify(objetoP2));
var pruebamerge = Yaast["MergeObject"](objetoP1,objetoP2);
Ti.API.info('mezcla obj1 y obj2:' + JSON.stringify(pruebamerge));
Ti.API.info('\n**Prueba Compleja 2 (templates for Titanium ListView):');
var template1 = {
childTemplates: [
{
type: 'Ti.UI.Label',
bindId: 'icon',
properties: {
layout: 'vertical',
left: 10,
font: {
fontFamily: Yaast.FontAwesome.getFontFamily()
},
textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER
}
}, {
type: 'Ti.UI.Label',
bindId: 'title',
properties: {
layout: 'horizontal',
color: '#FFFFFF',
font: {
fontFamily:'Default',
fontSize: 22
},
left: 40,
height: 44,
textAlign: Ti.UI.TEXT_ALIGNMENT_LEFT
}
}, {
type: 'Ti.UI.Label',
bindId: 'id',
properties: {
visible: false
}
}
],
properties: {
backgroundColor: '#2B3E50',
selectedBackgroundColor: 858585,
},
events: {}
};
Ti.API.info('template1:' + JSON.stringify(template1));
var template2 = {
childTemplates: [
{
properties: {
layout: 'horizontal',
height: 44,
color: '#5679a4',
font: {
fontSize: 22
},
}
},
{},
{
properties: {
color: 'SOGTULAPDT'
}
}
],
properties: {
backgroundColor: '#FFFFFF',
top: 24
}
};
Ti.API.info('template2:' + JSON.stringify(template2));
var pruebatemplates = Yaast["MergeObject"](template1,template2);
Ti.API.info('mezcla template1 y template2:' + JSON.stringify(pruebatemplates));
*/
return _self;
};
module.exports = Utils;