-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
274 lines (246 loc) · 6.36 KB
/
helpers.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*jshint camelcase:false */
/**
* @fileoverview generic function helpers.
*/
goog.provide('ssd.helpers');
goog.require('goog.array');
goog.require('goog.object');
goog.require('goog.json');
/**
* The noop function
*/
ssd.noop = function(){};
/** @const {string} the back pipe key */
ssd.BACKPIPE_KEY = 'backPipe';
/**
* Will add a key in the eventObj that is a function
* that any listener can call and define new value on the
* provided param.
*
* This cannot be async.
*
* @param {Object} eventObj
* @param {*} data Any type of data.
* @return {Object} The event object
*/
ssd.eventBackPipe = function(eventObj, data) {
eventObj[ssd.BACKPIPE_KEY] = function(fn, optSelf) {
data = fn.call(optSelf, data);
};
return function() {
return data;
};
};
/**
* Wrapper for goog.array.find
* Will search each element of an array and
* match the object key 'key' with 'value'
* On Match we will return the element content
*
* e.g. var ind = ssd.arFind(ar, 'userId', userIdvar);
*
* @param {array} ar The array
* @param {string} key The object key we will query
* @param {mixed} value The value we are looking for
* @return {array|null} The first array element that passes the test, or null if no element is found.
*/
ssd.arFind = function (ar, key, value)
{
var g = goog;
// check if we have an array
if (!g.isArray(ar)) {
// not an array, force it into one
ar = g.object.getValues(ar);
}
return g.array.find(ar, function(el){
if (el[key] === value) return true;
return false;
});
}; // method arFind
/**
* Wrapper for goog.array.findIndex
* Will search each element of an array and
* match the object key 'key' with 'value'
* On Match we will return the element index
*
* e.g. var ind = ssd.arFindIndex(ar, 'userId', userIdvar);
*
* @param {array} ar The array
* @param {string} key The object key we will query
* @param {mixed} value The value we are looking for
* @return {number} -1 for fail. The index of the first array element that passes the test, or -1 if no element is found.
*/
ssd.arFindIndex = function (ar, key, value)
{
if (!goog.isArray(ar)) return -1;
return goog.array.findIndex(ar, function(el){
if (el[key] === value) return true;
return false;
});
}; // method arFindIndex
/**
* Wrapper for goog.array.removeIf
* Will search each element of an array
* and if it finds a match for the object key
* we provided it, it then removes this element
* from the array
*
* @param {array} ar The array
* @param {string} key The object key we will query
* @param {mixed} value The value we are looking for
* @return boolean True if an element was removed.
*/
ssd.arRemove = function (ar, key, value)
{
if (!goog.isArray(ar)) return false;
return goog.array.removeIf(ar, function(el){
if (el[key] === value) return true;
return false;
});
}; // method ssd.arRemove
/**
* Determines if the given object is a valid
* jQuery array or the jQuery function
*
* @param {*} ar The object we want to examine
* @return boolean
*/
ssd.isjQ = function (ar) {
/** @preserveTry */
try {
if (goog.isFunction(ar)) {
return ar === jQuery;
}
return ar instanceof jQuery;
} catch(ex) {
return false;
}
}; // method ssd.isjQ
/**
* Decode a URI string
*
* @param {string} str
* @return {string}
*/
ssd.decURI = function(str){
if (goog.isNull(str)) { return ''; }
var ret;
try {
ret = decodeURIComponent(str);
} catch(ex){
return str;
}
return ret;
};
/**
* Encode a URI string
*
* @param {string}
* @return {string}
*/
ssd.encURI = function(str) {
if (goog.isNull(str)) return '';
var ret;
try {
ret = encodeURIComponent(str);
} catch(ex){
return str;
}
return ret;
};
/**
* Decode html Entities
*
* @param {string} str
* @return {string}
*/
ssd.decEnt = function(str) {
if (goog.isNull(str)) { return ''; }
var ret;
try {
ret = goog.string.unescapeEntities(str);
} catch(ex){
return str;
}
return ret;
};
/**
* Encode html Entities
*
* @param {string} str
* @return {string}
*/
ssd.encEnt = function(str) {
if (goog.isNull(str)) { return ''; }
var ret;
try {
ret = goog.string.htmlEscape(str);
} catch(ex){
return str;
}
return ret;
};
/**
* Will return the current domain name of the site
* e.g. ssd.local, ssd.com ...
*
* WARNING: Requires goog.Uri that is not declared in this file!
*
* @return {string}
*/
ssd.getDomain = function() {
return new goog.Uri(document.location.href).getDomain();
};
/**
* Read a page's GET URL variables and return them as an associative array.
* From: http://snipplr.com/view/799/get-url-variables/
*
* @return {Array}
*/
ssd.getUrlVars = function() {
var vars = [], hash;
var hashes = window.location.href.slice(
window.location.href.indexOf('?') + 1).split('&');
for(var i = 0, len = hashes.length; i < len; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
};
/**
* Checks if a value (needle) is within the provided other parameters
*
* e.g. if (ssd.inValue('a', 'b', 'c', 'z')) is false...
*
* @param {mixed} needle Value we want to look for
* @param {...*=} optVarArgs Additional arguments that are used to compare
* our needle value against
* @return {boolean}
*/
ssd.inValue = function (needle, optVarArgs) {
var len = arguments.length;
var haystack = [];
for (var start = 1; start < len ; start++) {
haystack.push(arguments[start]);
}
if (-1 === haystack.indexOf(needle))
return false;
return true;
};
/**
* For traditional methods that don't return promises but only accept callbacks
* use this helper to get a deferred resolved when the callback is invoked.
*
* It is expected that the callback returns a promise.
*
* @param {when.Deferred} defer A deferred.
* @param {Function:when.Promise} cb The callback, must return a promise.
* @param {Object=} optSelf context for cb.
* @return {Function} A Function that will resolve resolver.
*/
ssd.cb2promise = function(defer, cb, optSelf) {
return function() {
cb.apply(optSelf, arguments).then(defer.resolve, defer.reject);
};
};