-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.common.js
104 lines (91 loc) · 1.94 KB
/
Core.common.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
"use strict"
// requires Core.<env>.js
/* Polyfills */
if (!Function.prototype.bind) {
Function.prototype.bind = function(scope) {
var self = this;
return function() {
return self.apply(scope, arguments);
};
};
}
/**
* Performs a shallow copy of all properties of arguments 2 to n (the source objects) to argument 1 (the destination object), and return argument 1.
*/
Core.apply = function(dest) {
if (arguments.length < 2) {
return dest;
}
var sources = Array.prototype.slice.call(arguments, 1);
sources.forEach(function(src) {
for (var p in src) {
if (src.hasOwnProperty(p)) {
dest[p] = src[p];
}
}
});
return dest;
};
Core.stringify = function(o, d) {
var depth = d || 0;
if (o === undefined) {
return "undefined";
}
if (o === null) {
return "null";
}
if (typeof o === 'object') {
if (typeof o.length === 'number' && typeof o.join === 'function') {
return '[' + o.join(', ') + ']';
}
else {
var props = [];
for (var p in o) {
var val = o[p];
if (typeof o[p] !== 'function') {
props.push(p + ': ' + Core.stringify(val, depth + 1));
}
}
if (props.length === 0) {
return '{}';
}
var tabs = [];
for (var i = 0; i < depth; i++) {
tabs.push('\t');
}
tabs = tabs.join('');
return ['{\n', tabs, '\t', props.join(', '), '\n', tabs, '}'].join('');
}
}
return o.toString();
};
/**
* Same as Core.log() but also prints an additional line.
*/
Core.logln = function() {
Core.log.apply(undefined, arguments);
Core.log('\n');
};
/**
* A fake resource class.
*/
Core.Resource = (function() {
var R = function(path, resourceId) {
this.path = path;
this.resourceId = resourceId;
};
R.prototype = {
get: function(callback) {
Core.delay(function() {
callback.call(undefined, {
success: true,
data: {
name: this.resourceId,
description: "Description of " + this.resourceId
}
});
}.bind(this), 1000);
}
};
return R;
})();