-
Notifications
You must be signed in to change notification settings - Fork 75
/
utils.js
169 lines (144 loc) · 3.88 KB
/
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
var utils = exports
/**
* Returns an indication of whether the argument is an array or not
*/
utils.isArray = function(a) {
return Object.prototype.toString.call(a) == '[object Array]';
}
/**
* Returns an indication of whether the argument is a Date or not
*/
utils.isDate = function(d) {
return Object.prototype.toString.call(d) == '[object Date]';
}
/**
* Does a deep clone of the object.
*/
utils.clone = function(obj) {
if(!obj || typeof obj == 'function' || utils.isDate(obj) || typeof obj != 'object') {
return obj;
}
var retVal, i;
if(utils.isArray(obj)){
retVal = [];
for(i = 0; i < obj.length; ++i){
retVal.push(utils.clone(obj[i]));
}
return retVal;
}
retVal = {};
for(i in obj){
if(!(i in retVal) || retVal[i] != obj[i]) {
retVal[i] = utils.clone(obj[i]);
}
}
return retVal;
}
/**
* Returns a wrappered version of the function
*/
utils.close = function(context, func, params) {
if (typeof params == 'undefined') {
return function() {
return func.apply(context, arguments);
};
} else {
return function() {
return func.apply(context, params);
};
}
}
/**
* Create a UUID
*/
utils.createUUID = function() {
return UUIDcreatePart(4) + '-' +
UUIDcreatePart(2) + '-' +
UUIDcreatePart(2) + '-' +
UUIDcreatePart(2) + '-' +
UUIDcreatePart(6);
}
/**
* Extends a child object from a parent object using classical inheritance
* pattern.
*/
utils.extend = (function() {
// proxy used to establish prototype chain
var F = function() {};
// extend Child from Parent
return function(Child, Parent) {
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.__super__ = Parent.prototype;
Child.prototype.constructor = Child;
};
}())
/**
* Alerts a message in any available way: alert or console.log.
*/
utils.alert = function(msg) {
if (alert) {
alert(msg);
} else if (console && console.log) {
console.log(msg);
}
}
/**
* Formats a string and arguments following it ala sprintf()
*
* format chars:
* %j - format arg as JSON
* %o - format arg as JSON
* %c - format arg as ''
* %% - replace with '%'
* any other char following % will format it's
* arg via toString().
*
* for rationale, see FireBug's Console API:
* http://getfirebug.com/wiki/index.php/Console_API
*/
utils.format = function(formatString /* ,... */) {
if (formatString == null) return ""
if (arguments.length == 1) return formatString.toString()
var pattern = /(.*?)%(.)(.*)/
var rest = formatString.toString()
var result = []
var args = [].slice.call(arguments,1)
while (args.length) {
var arg = args.shift()
var match = pattern.exec(rest)
if (!match) break
rest = match[3]
result.push(match[1])
if (match[2] == '%') {
result.push('%')
args.unshift(arg)
continue
}
result.push(formatted(arg, match[2]))
}
result.push(rest)
return result.join('')
}
//------------------------------------------------------------------------------
function UUIDcreatePart(length) {
var uuidpart = "";
for (var i=0; i<length; i++) {
var uuidchar = parseInt((Math.random() * 256), 10).toString(16);
if (uuidchar.length == 1) {
uuidchar = "0" + uuidchar;
}
uuidpart += uuidchar;
}
return uuidpart;
}
//------------------------------------------------------------------------------
function formatted(object, formatChar) {
switch(formatChar) {
case 'j':
case 'o': return JSON.stringify(object)
case 'c': return ''
}
if (null == object) return Object.prototype.toString.call(object)
return object.toString()
}