-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
utils.js
164 lines (115 loc) · 3.69 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
/*
* typeahead.js
* https://github.com/twitter/typeahead.js
* Copyright 2013-2014 Twitter, Inc. and other contributors; Licensed MIT
*/
var _ = (function() {
'use strict';
return {
isMsie: function() {
// from https://github.com/ded/bowser/blob/master/bowser.js
return (/(msie|trident)/i).test(navigator.userAgent) ?
navigator.userAgent.match(/(msie |rv:)(\d+(.\d+)?)/i)[2] : false;
},
isBlankString: function(str) { return !str || /^\s*$/.test(str); },
// http://stackoverflow.com/a/6969486
escapeRegExChars: function(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
},
isString: function(obj) { return typeof obj === 'string'; },
isNumber: function(obj) { return typeof obj === 'number'; },
isArray: $.isArray,
isFunction: $.isFunction,
isObject: $.isPlainObject,
isUndefined: function(obj) { return typeof obj === 'undefined'; },
isElement: function(obj) { return !!(obj && obj.nodeType === 1); },
isJQuery: function(obj) { return obj instanceof $; },
toStr: function toStr(s) {
return (_.isUndefined(s) || s === null) ? '' : s + '';
},
bind: $.proxy,
each: function(collection, cb) {
// stupid argument order for jQuery.each
$.each(collection, reverseArgs);
function reverseArgs(index, value) { return cb(value, index); }
},
map: $.map,
filter: $.grep,
every: function(obj, test) {
var result = true;
if (!obj) { return result; }
$.each(obj, function(key, val) {
if (!(result = test.call(null, val, key, obj))) {
return false;
}
});
return !!result;
},
some: function(obj, test) {
var result = false;
if (!obj) { return result; }
$.each(obj, function(key, val) {
if (result = test.call(null, val, key, obj)) {
return false;
}
});
return !!result;
},
mixin: $.extend,
identity: function(x) { return x; },
clone: function(obj) { return $.extend(true, {}, obj); },
getIdGenerator: function() {
var counter = 0;
return function() { return counter++; };
},
templatify: function templatify(obj) {
return $.isFunction(obj) ? obj : template;
function template() { return String(obj); }
},
defer: function(fn) { setTimeout(fn, 0); },
debounce: function(func, wait, immediate) {
var timeout, result;
return function() {
var context = this, args = arguments, later, callNow;
later = function() {
timeout = null;
if (!immediate) { result = func.apply(context, args); }
};
callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) { result = func.apply(context, args); }
return result;
};
},
throttle: function(func, wait) {
var context, args, timeout, result, previous, later;
previous = 0;
later = function() {
previous = new Date();
timeout = null;
result = func.apply(context, args);
};
return function() {
var now = new Date(),
remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
}
else if (!timeout) {
timeout = setTimeout(later, remaining);
}
return result;
};
},
stringify: function(val) {
return _.isString(val) ? val : JSON.stringify(val);
},
noop: function() {}
};
})();