forked from tapquo/Lungo.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLungo.Core.js
executable file
·222 lines (201 loc) · 6.28 KB
/
Lungo.Core.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
/**
* Contains all the common functions used in Lungo.
*
* @namespace LUNGO
* @class Core
*
* @author Javier Jimenez Villar <[email protected]> || @soyjavi
* @author Guillermo Pascual <[email protected]> || @pasku1
*/
LUNGO.Core = (function(lng, $$, undefined) {
var ARRAY_PROTO = Array.prototype;
var HASHTAG_CHARACTER = '#';
/**
* Console system to display messages when you are in debug mode.
*
* @method log
*
* @param {number} Severity based in (1)Log, (2)Warn, (>2)Error
* @param {string} Message to show in console
*/
var log = function(severity, message) {
if (!lng.Core.isMobile()) {
console[(severity === 1) ? 'log' : (severity === 2) ? 'warn' : 'error'](message);
} else {
// @todo : send to the server
}
};
/**
* Executes callbacks based on the parameters received.
*
* @method execute
*
* @param {Function} callback to execute
*/
var execute = function() {
var args = toArray(arguments);
var callback = args.shift();
if (toType(callback) === 'function') {
callback.apply(null, args);
}
};
/**
* Creates a new function that, when called, itself calls this function in
* the context of the provided this value, with a given sequence of arguments
* preceding any provided when the new function was called.
*
* @method bind
*
* @param {object} object to which the 'this' can refer in the new function when the new function is called.
* @param {Function} method A function object.
*/
var bind = function(object, method) {
return function() {
return method.apply(object, toArray(arguments));
};
};
/**
* Copy from any number of objects and mix them all into a new object.
* The implementation is simple; just loop through arguments and
* copy every property of every object passed to the function.
*
* @method mix
*
* @param {object} arguments to mix them all into a new object.
* @return {object} child a new object with all the objects from the arguments mixed.
*/
var mix = function() {
var child = child || {};
for (var arg = 0, len = arguments.length; arg < len; arg++) {
var argument = arguments[arg];
for (var prop in argument) {
if (isOwnProperty(argument, prop)) {
child[prop] = argument[prop];
}
}
}
return child;
};
/**
* Every object descended from Object inherits the hasOwnProperty method.
* This method can be used to determine whether an object has the specified property
* as a direct property of that object.
*
* @param {object} object to test for a property's existence inside itself.
* @param {string} property the name of the property to test.
* @return {boolean} indicating whether the object has the specified property.
*/
var isOwnProperty = function(object, property) {
return $$.isOwnProperty(object, property);
};
/**
* Determine the internal JavaScript [[Class]] of an object.
*
* @param {object} obj to get the real type of itself.
* @return {string} with the internal JavaScript [[Class]] of itself.
*/
var toType = function(obj) {
return $$.toType(obj);
};
/**
* Convert an array-like object into a true JavaScript array.
*
* @param {object} obj Any object to turn into a native Array.
* @return {object} The object is now a plain array.
*/
var toArray = function(obj) {
return ARRAY_PROTO.slice.call(obj, 0);
};
/**
* Determine if the current environment is a mobile environment
*
* @method isMobile
*
* @return {boolean} true if is mobile environment, false if not.
*/
var isMobile = function() {
return $$.isMobile();
};
/**
* Returns information of execute environment
*
* @method environment
*
* @return {object} Environment information
*/
var environment = function() {
return $$.environment();
};
/**
* Returns a ordered list of objects by a property
*
* @method orderByProperty
*
* @param {list} List of objects
* @param {string} Name of property
* @param {string} Type of order: asc (ascendent) or desc (descendent)
* @return {list} Ordered list
*/
var orderByProperty = function(data, property, order) {
var order_operator = (order === 'desc') ? -1 : 1;
return data.sort(function(a, b) {
return (a[property] < b[property]) ? - order_operator :
(a[property] > b[property])
?
order_operator : 0;
}
);
};
/**
* Returns a correct URL using hashtag character
*
* @method parseUrl
*
* @param {string} Url
* @return {string} Url parsed
*/
var parseUrl = function(href) {
var href_hashtag = href.lastIndexOf(HASHTAG_CHARACTER);
if (href_hashtag > 0) {
href = href.substring(href_hashtag);
} else if (href_hashtag === -1) {
href = HASHTAG_CHARACTER + href ;
}
return href;
};
/**
* Returns a Object in a list by a property value
*
* @method objectInListByProperty
*
* @param {list} List of objects
* @param {string} Name of property
* @param {var} Value for comparision
* @return {object} Instance of object founded (if exists)
*/
var findByProperty = function(list, property, value) {
var search = null;
for (var i = 0, len = list.length; i < len; i++) {
var element = list[i];
if (element[property] == value) {
search = element;
break;
}
};
return search;
};
return {
log: log,
execute: execute,
bind: bind,
mix: mix,
isOwnProperty: isOwnProperty,
toType: toType,
toArray: toArray,
isMobile: isMobile,
environment: environment,
orderByProperty: orderByProperty,
parseUrl: parseUrl,
findByProperty: findByProperty
};
})(LUNGO, Quo);