1 | | |
2 | | |
3 | | |
4 | | |
5 | | |
6 | | |
7 | | |
8 | | |
9 | | |
10 | | |
11 | 1 | if (!global.$apollo) { |
12 | | |
13 | | |
14 | | |
15 | | /** |
16 | | * Extend an object with another object |
17 | | * @param {Object} obj object to be extended |
18 | | * @param {Object} ext extension object |
19 | | * @param {bool} override Overwrite existing properties in obj |
20 | | * @param {bool} deep Doing an deep extend (perform extend on every object property) |
21 | | * @return {Object} reference to obj |
22 | | */ |
23 | 1 | function $extend(obj, ext, override, deep) { |
24 | 0 | var key; |
25 | 0 | if (override) { |
26 | 0 | if (deep) |
27 | 0 | _overrideDeepExtend(obj, ext); |
28 | | else |
29 | 0 | for (key in ext) |
30 | 0 | obj[key] = ext[key]; |
31 | | } else { |
32 | 0 | if (deep) |
33 | 0 | _deepExtend(obj, ext); |
34 | | else |
35 | 0 | for (key in ext) |
36 | 0 | if (!(key in obj)) |
37 | 0 | obj[key] = ext[key]; |
38 | | } |
39 | 0 | return obj; |
40 | | } |
41 | | |
42 | 1 | function _overrideDeepExtend(obj, ext) { |
43 | 0 | for (var key in ext) |
44 | 0 | if (Object.isObjectStrict(obj[key]) && Object.isObjectStrict(ext[key])) |
45 | 0 | _overrideDeepExtend(obj[key], ext[key]); |
46 | | else |
47 | 0 | obj[key] = ext[key]; |
48 | | } |
49 | | |
50 | 1 | function _deepExtend(obj, ext) { |
51 | 0 | for (var key in ext) |
52 | 0 | if (Object.isObjectStrict(obj[key]) && Object.isObjectStrict(ext[key])) |
53 | 0 | _deepExtend(obj[key], ext[key]); |
54 | 0 | else if (!(key in obj)) |
55 | 0 | obj[key] = ext[key]; |
56 | | } |
57 | | |
58 | | /** |
59 | | * Define properties of an Object, Which usually used to extend prototype |
60 | | * of an object, as it will set properties as non-enumerable, and will |
61 | | * turn setValue(value) and getValue() functions to setter and getters. |
62 | | * Note: You should only use $define or Object.defineProperty on prototype, |
63 | | * or on a class' itself (to define static methods), instead of on instances |
64 | | * which could lead to severe performance issue. |
65 | | * @param {Object} object target object |
66 | | * @param {Object} prototype extension object |
67 | | * @param {bool} preserve preserve existing property |
68 | | * @return {Object} reference to object |
69 | | */ |
70 | 1 | function $define(object, prototype, preserve) { |
71 | 11 | Object.getOwnPropertyNames(prototype).forEach(function(key) { |
72 | 51 | if (preserve && (key in object)) |
73 | 2 | return; |
74 | 49 | var desc = Object.getOwnPropertyDescriptor(prototype, key); |
75 | 49 | if ('value' in desc) |
76 | 46 | desc.writable = true; |
77 | 49 | delete desc.enumerable; |
78 | 49 | delete desc.configurable; |
79 | 49 | Object.defineProperty(object, key, desc); |
80 | | }); |
81 | 11 | return object; |
82 | | } |
83 | | |
84 | | /** |
85 | | * Declare a Class. |
86 | | * @param {Function} fn constructor of the Class |
87 | | * @param {Object} prototype prototype of Class |
88 | | * @return {Function} reference to constructor |
89 | | */ |
90 | 1 | function $declare(fn, prototype) { |
91 | 0 | fn.prototype.constructor = fn; |
92 | 0 | $define(fn.prototype, prototype); |
93 | 0 | return fn; |
94 | | } |
95 | | |
96 | | /** |
97 | | * Inherit another Class to current Class |
98 | | * @param {Function} fn constructor of the Class |
99 | | * @param {Function} parent parent Class |
100 | | * @param {Object} prototype prototype of Class |
101 | | * @return {Function} reference to constructor |
102 | | */ |
103 | | |
104 | | |
105 | 1 | function $inherit(fn, parent, prototype) { |
106 | 0 | fn.prototype = { |
107 | | constructor: fn, |
108 | | __proto__: parent.prototype |
109 | | }; |
110 | 0 | if (prototype) |
111 | 0 | $define(fn.prototype, prototype); |
112 | 0 | return fn; |
113 | | } |
114 | | |
115 | | |
116 | | |
117 | | /** |
118 | | * Adding enumerations to a Class (both static and prototype). |
119 | | * @param {Function} fn constructor of the Class |
120 | | * @param {Object} values object holding all enumerates want to define |
121 | | * @return {Function} reference to constructor |
122 | | */ |
123 | 1 | function $defenum(fn, values) { |
124 | 0 | $define(fn, values); |
125 | 0 | $define(fn.prototype, values); |
126 | 0 | return fn; |
127 | | } |
128 | | |
129 | | /** |
130 | | * Format a string with given pattern. |
131 | | * @param {string} str pattern |
132 | | * @return {string} formatted string |
133 | | */ |
134 | | |
135 | | |
136 | 1 | var $format = require('util').format; |
137 | | |
138 | | |
139 | | |
140 | | /** |
141 | | * Making an Error instance with given format and parameters. |
142 | | * Note: this is a helper function works like util.format(), |
143 | | * apart from it returns an Error object instead of string. |
144 | | * @return {Error} generated Error instance |
145 | | */ |
146 | 1 | function $error() { |
147 | 1 | return new Error($format.apply(null, arguments)); |
148 | | } |
149 | | |
150 | | |
151 | | /** |
152 | | * Generate a deep copy of an Object with its primitive typed |
153 | | * fields (exclude functions). |
154 | | * @param {mixed} obj source object |
155 | | * @return {mixed} cloned object |
156 | | */ |
157 | 1 | function $valueCopy(obj) { |
158 | 0 | var res; |
159 | 0 | if (Array.isArray(obj)) { |
160 | 0 | res = obj.slice(0); |
161 | 0 | for (var i = 0; i < res.length; i++) |
162 | 0 | if (Object.isObject(res[i])) |
163 | 0 | res[i] = $valueCopy(res[i]); |
164 | 0 | } else if (Object.isObjectStrict(obj)) { |
165 | 0 | res = {}; |
166 | 0 | for (var key in obj) |
167 | 0 | res[key] = $valueCopy(obj[key]); |
168 | 0 | } else if (Function.isFunction(obj)) { |
169 | 0 | return undefined; |
170 | | } else { |
171 | 0 | return obj; |
172 | | } |
173 | 0 | return res; |
174 | | } |
175 | | |
176 | | /** |
177 | | * Generates a copy of an Object. |
178 | | * @param {Mixed} org source object |
179 | | * @param {bool} deep perform a deep clone |
180 | | * @return {Mixed} cloned object |
181 | | */ |
182 | 1 | function $clone(obj, deep) { |
183 | 0 | var res; |
184 | 0 | var _deep = deep === true || deep - 1; |
185 | 0 | if (Array.isArray(obj)) { |
186 | 0 | res = obj.slice(0); |
187 | 0 | if (deep) |
188 | 0 | for (var i = 0; i < res.length; i++) |
189 | 0 | if (Object.isObject(res[i])) |
190 | 0 | res[i] = $clone(res[i], _deep); |
191 | 0 | } else if (Object.isObjectStrict(obj)) { |
192 | 0 | res = {}; |
193 | 0 | for (var key in obj) |
194 | 0 | res[key] = obj[key]; |
195 | 0 | if (deep) |
196 | 0 | for (var key in obj) |
197 | 0 | if (Object.isObject(res[key])) |
198 | 0 | res[key] = $clone(res[key], _deep); |
199 | | } else { |
200 | 0 | return obj; |
201 | | } |
202 | 0 | return res; |
203 | | } |
204 | | |
205 | | /** |
206 | | * Return default value of an undefined variable. |
207 | | * @param {Mixed} val value |
208 | | * @param {Mixed} def default value |
209 | | * @return {Mixed} |
210 | | */ |
211 | 1 | function $default(val, def) { |
212 | 5 | return val === undefined ? def : val; |
213 | | } |
214 | | |
215 | | /** |
216 | | * Wrap an object with given Class. |
217 | | * Note: it will call Class.__wrap method to do custom wrapping. |
218 | | * @param {Object} obj object to be wrapped |
219 | | * @param {Function} Type wrapping Class |
220 | | * @return {Object} wrapped object |
221 | | */ |
222 | | |
223 | | |
224 | 1 | function $wrap(obj, Type) { |
225 | 2 | obj.__proto__ = Type.prototype; |
226 | 2 | if (Type.__wrap) |
227 | 2 | Type.__wrap(obj); |
228 | 2 | return obj; |
229 | | } |
230 | | |
231 | | /** |
232 | | * Removing prototype chain from a given object. |
233 | | * @param {Object} object object to be stripped |
234 | | * @return {Object} object stripped |
235 | | */ |
236 | 1 | function $strip(object) { |
237 | 1 | object.__proto__ = Object.prototype; |
238 | 1 | return object; |
239 | | } |
240 | | |
241 | | /** |
242 | | * Use Object.prototype.toString to determine an element's type |
243 | | * This method provide more stricter strategy on type detection, |
244 | | * can be worked with typeof. |
245 | | * @param {Mixed} obj Variable |
246 | | * @return {String} type of the variable, like typeof, |
247 | | * but with better precision. |
248 | | */ |
249 | 1 | function $typeof(obj) { |
250 | 16 | var type = Object.prototype.toString.call(obj); |
251 | 16 | return type.substring(8, type.length - 1).toLowerCase(); |
252 | | } |
253 | | |
254 | | |
255 | | |
256 | 1 | $define(global, { |
257 | | $extend: $extend, |
258 | | $define: $define, |
259 | | $declare: $declare, |
260 | | $inherit: $inherit, |
261 | | $defenum: $defenum, |
262 | | $format: $format, |
263 | | $error: $error, |
264 | | $valueCopy: $valueCopy, |
265 | | $clone: $clone, |
266 | | $default: $default, |
267 | | $wrap: $wrap |
268 | | |
269 | | , |
270 | | $apollo: require('./package').version, |
271 | | $strip: $strip, |
272 | | $typeof: $typeof |
273 | | |
274 | | }); |
275 | | |
276 | 1 | $define(String.prototype, { |
277 | | /** |
278 | | * Repeat current string for given times. |
279 | | * @param {int} times Times to repeat |
280 | | * @return {string} result |
281 | | */ |
282 | | repeat: function(times) { |
283 | 8 | var res = ''; |
284 | 8 | for (var i = 0; i < times; i++) |
285 | 27 | res += this; |
286 | 8 | return res; |
287 | | }, |
288 | | /** |
289 | | * Padding this to given length with specified char from left. |
290 | | * @param {char} ch padding char |
291 | | * @param {int} length desired length |
292 | | * @return {string} result |
293 | | */ |
294 | | paddingLeft: function(ch, length) { |
295 | 3 | if (this.length < length) |
296 | 2 | return ch.repeat(length - this.length) + this; |
297 | 1 | return this; |
298 | | }, |
299 | | /** |
300 | | * Padding this to given length with specified char from right. |
301 | | * @param {char} ch padding char |
302 | | * @param {int} length desired length |
303 | | * @return {string} result |
304 | | */ |
305 | | paddingRight: function(ch, length) { |
306 | 3 | if (this.length < length) |
307 | 2 | return this + ch.repeat(length - this.length); |
308 | 1 | return this; |
309 | | }, |
310 | | /** |
311 | | * Tests if this string starts with the given one. |
312 | | * @param {string} str string to test with |
313 | | * @param {number} pos optional, position to start compare, defaults |
314 | | * to 0 |
315 | | * @return {bool} result |
316 | | */ |
317 | | startsWith: function(str, pos) { |
318 | 10 | if (str === null || str === undefined || str.length === 0) |
319 | 6 | return true; |
320 | 4 | return this.substr(pos || 0, str.length) === str; |
321 | | }, |
322 | | /** |
323 | | * Tests if this string ends with the given one. |
324 | | * @param {string} str string to test with |
325 | | * @param {number} len optional, pretend this string is of given length, |
326 | | * defaults to actual length |
327 | | * @return {bool} result |
328 | | */ |
329 | | endsWith: function(str, len) { |
330 | 10 | if (str === null || str === undefined || str.length === 0) |
331 | 6 | return true; |
332 | 4 | return this.substr((len || this.length) - str.length, str.length) === str; |
333 | | }, |
334 | | /** |
335 | | * Return a string in it's title form. |
336 | | * @return {string} string in title case |
337 | | * Note: if a word containing upper case, nothing |
338 | | * will be done. |
339 | | */ |
340 | | toTitleCase: function() { |
341 | 11 | return this.replace(/\b([a-z])(['a-z]*)\b/g, function(all, letter, rest) { |
342 | 16 | return letter.toUpperCase() + rest; |
343 | | }); |
344 | | }, |
345 | | /** |
346 | | * Trim whitespaces at the begining of the string |
347 | | * @return {string} trimmed string |
348 | | */ |
349 | | trimLeft: function() { |
350 | 0 | return this.replace(/^\s+/, ''); |
351 | | }, |
352 | | /** |
353 | | * Trim whitespaces at the ending of the string |
354 | | * @return {string} trimmed string |
355 | | */ |
356 | | trimRight: function() { |
357 | 0 | return this.replace(/\s+$/, ''); |
358 | | } |
359 | | }, true); |
360 | | |
361 | 1 | $define(Number.prototype, { |
362 | | /** |
363 | | * Clamp current value to the given range [lb, ub] |
364 | | * @param {number} lb lower bound |
365 | | * @param {number} ub upper bound |
366 | | * @return {number} result |
367 | | */ |
368 | | clamp: function(lb, ub) { |
369 | 4 | var rtn = Number(this); |
370 | 4 | if (lb !== undefined && rtn < lb) |
371 | 1 | rtn = lb; |
372 | 4 | if (ub !== undefined && rtn > ub) |
373 | 1 | rtn = ub; |
374 | 4 | return rtn; |
375 | | }, |
376 | | /** |
377 | | * Shortcut to Math.floor(this) |
378 | | * @return {number} Math.floor(this) |
379 | | */ |
380 | | floor: function() { |
381 | 2 | return Math.floor(this); |
382 | | }, |
383 | | /** |
384 | | * Shortcut to Math.ceil(this) |
385 | | * @return {number} Math.ceil(this) |
386 | | */ |
387 | | ceil: function() { |
388 | 2 | return Math.ceil(this); |
389 | | }, |
390 | | /** |
391 | | * Shortcut to Math.round(this) with additional parameters |
392 | | * @param {number} decimals number of decimal digits to round up to |
393 | | * @return {number} rounded number |
394 | | */ |
395 | | round: function(decimals) { |
396 | 4 | if (decimals) { |
397 | 2 | var unit = Math.pow(10, decimals); |
398 | 2 | return Math.round(this * unit) / unit; |
399 | | } |
400 | 2 | return Math.round(this); |
401 | | }, |
402 | | /** |
403 | | * Get the thousands separated number |
404 | | * @param {number} decimals number of decimal digits to remain |
405 | | * @param {string} separator separator |
406 | | * @return {string} separated number |
407 | | */ |
408 | | toGroup: function(decimals, separator) { |
409 | | |
410 | 12 | decimals = decimals || 0; |
411 | | |
412 | 12 | if (this > -1000 && this < 1000) |
413 | 3 | return this.toFixed(decimals); |
414 | | |
415 | 9 | separator = separator || ','; |
416 | | |
417 | 9 | var sign = this < 0 ? '-' : ''; |
418 | 9 | var tmp = Math.abs(this).toFixed(decimals); |
419 | | |
420 | 9 | var intPart, decimalPart; |
421 | 9 | if (decimals > 0) { |
422 | 2 | intPart = tmp.substr(0, tmp.length - decimals - 1); |
423 | 2 | decimalPart = tmp.substr(tmp.length - decimals - 1); |
424 | | } else { |
425 | 7 | intPart = tmp; |
426 | 7 | decimalPart = ''; |
427 | | } |
428 | | |
429 | 9 | var res = ''; |
430 | 9 | for (var pos = 0, len = intPart.length % 3 || 3; |
431 | | pos < intPart.length; pos += len, len = 3) { |
432 | 27 | if (res !== '') |
433 | 18 | res += separator; |
434 | 27 | res += intPart.substr(pos, len); |
435 | | } |
436 | 9 | return sign + res + decimalPart; |
437 | | |
438 | | } |
439 | | }); |
440 | | |
441 | 1 | $define(Array.prototype, { |
442 | | /** |
443 | | * get minimum value in this array |
444 | | * @return {Mixed} minimal value |
445 | | */ |
446 | | min: function() { |
447 | 3 | var res = this[0]; |
448 | 3 | for (var i = 1; i < this.length; i++) |
449 | 5 | if (this[i] < res) |
450 | 1 | res = this[i]; |
451 | 3 | return res; |
452 | | }, |
453 | | /** |
454 | | * get maximum value in this array |
455 | | * @return {Mixed} maximum value |
456 | | */ |
457 | | max: function() { |
458 | 3 | var res = this[0]; |
459 | 3 | for (var i = 1; i < this.length; i++) |
460 | 5 | if (this[i] > res) |
461 | 4 | res = this[i]; |
462 | 3 | return res; |
463 | | }, |
464 | | /** |
465 | | * Push a value iif it's not in this array, and return value's index. |
466 | | * @param {Mixed} val new value |
467 | | * @return {int} index of the value |
468 | | * Note: This only works with primitive typed elements, which can be found |
469 | | * with Array#indexOf(). |
470 | | */ |
471 | | add: function(val) { |
472 | 3 | var index = this.indexOf(val); |
473 | 3 | if (index === -1) |
474 | 2 | return this.push(val) - 1; |
475 | 1 | return index; |
476 | | }, |
477 | | /** |
478 | | * Find a value in the array and remove it. |
479 | | * @param {Mixed} val value to remove |
480 | | * @return {Array} this |
481 | | * Note: This only works with primitive typed elements, which can be found |
482 | | * with Array#indexOf(). |
483 | | */ |
484 | | remove: function(val) { |
485 | 4 | var index = this.indexOf(val); |
486 | 4 | if (index > -1) { |
487 | | // Shift copy elements instead of Array#splice() for better performance. |
488 | | // http://jsperf.com/fast-array-splice/18 |
489 | 3 | while (++index < this.length) |
490 | 4 | this[index - 1] = this[index]; |
491 | 3 | this.pop(); |
492 | | } |
493 | 4 | return this; |
494 | | }, |
495 | | /** |
496 | | * Rotate this array (n->0, n+1->1, ...) |
497 | | * @param {int} n the offset |
498 | | * @return {Array} this |
499 | | */ |
500 | | rotate: function(n) { |
501 | 10 | if (n < 0) |
502 | 2 | n = n % this.length + this.length; |
503 | 10 | n %= this.length; |
504 | 10 | var middle = n; |
505 | 10 | var next = n; |
506 | 10 | var first = 0; |
507 | 10 | while (first < this.length) { |
508 | 44 | var t = this[first]; |
509 | 44 | this[first] = this[next]; |
510 | 44 | this[next] = t; |
511 | 44 | first++; |
512 | 44 | next++; |
513 | 73 | if (next == this.length) next = middle; |
514 | 22 | else if (first == middle) middle = next; |
515 | | } |
516 | 10 | return this; |
517 | | }, |
518 | | /** |
519 | | * get last element in this array |
520 | | * Note: It's not a reference when returning a non-object! |
521 | | * @return {Mixed} last element |
522 | | */ |
523 | | get back() { |
524 | 2 | return this[this.length - 1]; |
525 | | }, |
526 | | /** |
527 | | * get first element in this array |
528 | | * Note: It's not a reference when returning a non-object! |
529 | | * @return {Mixed} first element |
530 | | */ |
531 | | get front() { |
532 | 3 | return this[0]; |
533 | | }, |
534 | | /** |
535 | | * Flattern a array with sub arrays. |
536 | | * @param {bool} deep if continue to flatten sub arrays |
537 | | * @return {Array} flattened array. |
538 | | */ |
539 | | flatten: function(deep) { |
540 | 6 | var res = []; |
541 | 6 | if (!deep) |
542 | 2 | return res.concat.apply(res, this); |
543 | 4 | for (var i = 0; i < this.length; i++) |
544 | 7 | if (Array.isArray(this[i])) |
545 | 3 | res.push.apply(res, this[i].flatten(true)); |
546 | | else |
547 | 4 | res.push(this[i]); |
548 | 4 | return res; |
549 | | }, |
550 | | /** |
551 | | * Return unique elements in the array |
552 | | * @return {Array} |
553 | | */ |
554 | | unique: function() { |
555 | 3 | var res = []; |
556 | 3 | var dict = {}; |
557 | 3 | for (var i = 0; i < this.length; ++i) { |
558 | 8 | var key = this[i].toString(); |
559 | 8 | if (dict.hasOwnProperty(key)) |
560 | 4 | continue; |
561 | 4 | dict[key] = true; |
562 | 4 | res.push(this[i]); |
563 | | } |
564 | 3 | return res; |
565 | | }, |
566 | | /** |
567 | | * shuffle elements in the array in-place |
568 | | * @return {Array} |
569 | | */ |
570 | | shuffle: function() { |
571 | 10000 | for (var n = this.length; n > 0; n--) { |
572 | 30000 | var idx = Math.floor(n * Math.random()); |
573 | 30000 | if (idx != n - 1) { |
574 | 11598 | var tmp = this[idx]; |
575 | 11598 | this[idx] = this[n - 1]; |
576 | 11598 | this[n - 1] = tmp; |
577 | | } |
578 | | } |
579 | 10000 | return this; |
580 | | } |
581 | | }); |
582 | | |
583 | | /** |
584 | | * Forward declaring prototype functions to Array's static |
585 | | * methods. |
586 | | */ |
587 | 1 | if (Array.map === undefined) |
588 | 1 | ['forEach', 'every', 'some', 'filter', 'map', 'reduce', 'reduceRight', 'slice'] |
589 | | .forEach(function(method) { |
590 | 8 | var fn = Array.prototype[method]; |
591 | 8 | Object.defineProperty(Array, method, { |
592 | | value: function(a, b, c) { |
593 | 25 | return fn.call(a, b, c); |
594 | | } |
595 | | }); |
596 | | }); |
597 | | |
598 | 1 | if (String.trim === undefined) |
599 | 1 | ['trim', 'trimLeft', 'trimRight'] |
600 | | .forEach(function(method) { |
601 | 3 | var fn = String.prototype[method]; |
602 | 3 | Object.defineProperty(String, method, { |
603 | | value: function(a) { |
604 | 3 | return fn.call(a); |
605 | | } |
606 | | }); |
607 | | }); |
608 | | |
609 | 1 | $define(Object, { |
610 | | /** |
611 | | * Determine if an object is empty |
612 | | * @param {Object} obj object to test |
613 | | * @return {bool} object is empty |
614 | | */ |
615 | | isEmpty: function(obj) { |
616 | 7 | if (!obj) |
617 | 4 | return true; |
618 | 3 | for (var key in obj) |
619 | 1 | return false; |
620 | 2 | return true; |
621 | | }, |
622 | | /** |
623 | | * Get values of an object, like Object.keys(). |
624 | | * @param {Object} obj object to extract |
625 | | * @return {Array} values in the object |
626 | | */ |
627 | | values: function(obj) { |
628 | 5 | return Object.keys(obj).map(function(k) { |
629 | 7 | return obj[k]; |
630 | | }); |
631 | | }, |
632 | | /** |
633 | | * Vague but fast isObject test |
634 | | * Note: new String(), function, array, etc will return true |
635 | | * @param {Mixed} obj object to test |
636 | | * @return {bool} true if obj is an object and not null |
637 | | */ |
638 | | isObject: function(obj) { |
639 | | /** |
640 | | * Known fastest way to test, the order of the test |
641 | | * following: http://jsperf.com/typeof-vs-bool. |
642 | | */ |
643 | 14 | return obj && typeof obj === 'object'; |
644 | | }, |
645 | | /** |
646 | | * Strict isObject test, only pure Object will return true |
647 | | * Note: only {} will return true |
648 | | * @param {Mixed} obj object to test |
649 | | * @return {bool} true if obj is strictly an object |
650 | | */ |
651 | | isObjectStrict: function(obj) { |
652 | 20 | return Object.prototype.toString.call(obj) === '[object Object]'; |
653 | | } |
654 | | |
655 | | , |
656 | | /** |
657 | | * project $object with projectiong, same behaviour with mongodb projection |
658 | | * @param {Object} object target object |
659 | | * @param {Object} projection An object mapping fields to values |
660 | | * @param {Boolean} deep if true, go deep for sub objects |
661 | | * @param {Boolean} keep if true, keep undefined field of this |
662 | | * @return {Object} projected object |
663 | | */ |
664 | | project: function(object, projection, deep, keep) { |
665 | 14 | if (!Object.isObject(projection)) |
666 | 0 | return object; |
667 | 14 | var res = {}; |
668 | 14 | Object.keys(projection).forEach(function(key) { |
669 | 15 | var proj = projection[key]; |
670 | 15 | if (proj) { |
671 | 14 | var obj = object[key]; |
672 | 14 | if (deep && Object.isObjectStrict(obj) && Object.isObjectStrict(proj)) { |
673 | 7 | res[key] = Object.project(obj, projection[key], deep, keep); |
674 | | } else { |
675 | 7 | if (keep) |
676 | 1 | res[key] = obj; |
677 | 6 | else if (obj !== undefined) |
678 | 4 | res[key] = obj; |
679 | | } |
680 | | } |
681 | | }); |
682 | 14 | return res; |
683 | | }, |
684 | | Transformer: function(mapping) { |
685 | 1 | var expr = []; |
686 | 1 | expr.push('exec=function (object) {'); |
687 | 1 | expr.push('var res = {};'); |
688 | 1 | (function loop(lhv, mapping) { |
689 | 2 | Object.keys(mapping).forEach(function(key) { |
690 | 5 | var source = mapping[key]; |
691 | 6 | if (/\W/.test(key)) key = '["' + key + '"]'; |
692 | 4 | else key = '.' + key; |
693 | | |
694 | | |
695 | 5 | var target = lhv + key; |
696 | 5 | if ($typeof(source) == 'object') { |
697 | 1 | expr.push(target + ' = {};'); |
698 | 1 | return loop(target, source); |
699 | | } |
700 | | |
701 | 4 | if (true === source) |
702 | 1 | source = 'object' + key; |
703 | 3 | else if ($typeof(source) == 'string') |
704 | 2 | source = 'object' + source; |
705 | 1 | else if ($typeof(source) == 'function') |
706 | 1 | source = '('+source.toString()+')(object)'; |
707 | 4 | expr.push(target + ' = ' + source + ';'); |
708 | | }); |
709 | | })('res', mapping); |
710 | 1 | expr.push('return res;'); |
711 | 1 | expr.push('}'); |
712 | 1 | this.exec = eval(expr.join('')); |
713 | | } |
714 | | |
715 | | }); |
716 | | |
717 | 1 | $define(Function, { |
718 | | /** |
719 | | * Test if an object is a function |
720 | | * @param {Mixed} obj object to test |
721 | | * @return {bool} true if so |
722 | | */ |
723 | | isFunction: function(obj) { |
724 | 11 | return typeof obj === 'function'; |
725 | | } |
726 | | }); |
727 | | |
728 | 1 | $define(Date, { |
729 | | /** |
730 | | * Cast a value to Date |
731 | | * @param {Mixed} obj object to cast |
732 | | * @return {Date} casted value |
733 | | */ |
734 | | cast: function(obj) { |
735 | 0 | if (obj instanceof Date) |
736 | 0 | return obj; |
737 | 0 | if (typeof obj === 'string') |
738 | 0 | obj = Date.parse(obj); |
739 | 0 | if (typeof obj === 'number') { |
740 | 0 | if (isNaN(obj)) |
741 | 0 | return null; |
742 | 0 | obj = new Date(obj); |
743 | 0 | if (isNaN(obj.valueOf())) |
744 | 0 | return null; |
745 | 0 | return obj; |
746 | | } |
747 | 0 | return null; |
748 | | }, |
749 | | /** |
750 | | * Determine if an object is a Date |
751 | | * @param {Object} object to test |
752 | | * @return {bool} true iif it's a date. |
753 | | */ |
754 | | isDate: function(obj) { |
755 | 0 | obj = Date.cast(obj); |
756 | 0 | if (obj) |
757 | 0 | return obj >= 0 && obj < 2147483647000; |
758 | 0 | return false; |
759 | | } |
760 | | }); |
761 | | |
762 | 1 | $define(Boolean, { |
763 | | /** |
764 | | * Cast a value to bool |
765 | | * @param {Object} obj object to cast |
766 | | * @return {bool} casted value |
767 | | */ |
768 | | cast: function(obj) { |
769 | 18 | if (obj === true || obj === false) |
770 | 2 | return obj; |
771 | 16 | if (typeof obj === 'string') |
772 | 10 | return (/^(true|yes|ok|y|on)$/i).test(obj); |
773 | 6 | return Boolean(obj); |
774 | | } |
775 | | }); |
776 | | |
777 | 1 | $define(RegExp, { |
778 | | /** |
779 | | * Escape a string to work within a regular expression |
780 | | * @param {string} str string to escape |
781 | | * @return {strign} escaped string |
782 | | */ |
783 | | escape: function(str) { |
784 | 16 | return str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); |
785 | | } |
786 | | }); |
787 | | |
788 | | |
789 | | |
790 | | |
791 | 1 | $define(global, { |
792 | | $utils: { |
793 | | encodeRLE: function(str) { |
794 | 13 | return str.replace(/([^0-9a-z])\1+/g, function(all, cap) { |
795 | 10 | var replacement = all.length.toString(36) + cap; |
796 | 10 | if (replacement.length < all.length) |
797 | 3 | return replacement; |
798 | 7 | return all; |
799 | | }); |
800 | | }, |
801 | | decodeRLE: function(str) { |
802 | 13 | return str.replace(/([0-9a-z]+)([^0-9a-z])/g, function(all, count, cap) { |
803 | 4 | return cap.repeat(parseInt(count, 36)); |
804 | | }); |
805 | | } |
806 | | } |
807 | | }); |
808 | | |
809 | | |
810 | | |
811 | | |
812 | | |
813 | | } |
814 | | |
815 | | |
816 | | |
817 | | |