Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/utils/utils.js
4501 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Utility functions that were removed from goog.base but are
9
* still needed by the Selenium atoms. These provide compatibility shims for
10
* legacy Closure Library code.
11
*/
12
13
goog.provide('goog.utils');
14
15
16
/**
17
* Returns true if the specified value is not undefined.
18
*
19
* @param {?} val Variable to test.
20
* @return {boolean} Whether variable is defined.
21
* @deprecated Use `val !== undefined` instead.
22
*/
23
goog.utils.isDef = function(val) {
24
return val !== undefined;
25
};
26
27
28
/**
29
* Returns true if the specified value is a string.
30
*
31
* @param {?} val Variable to test.
32
* @return {boolean} Whether variable is a string.
33
* @deprecated Use `typeof val === 'string'` instead.
34
*/
35
goog.utils.isString = function(val) {
36
return typeof val === 'string';
37
};
38
39
40
/**
41
* Returns true if the specified value is a number.
42
*
43
* @param {?} val Variable to test.
44
* @return {boolean} Whether variable is a number.
45
* @deprecated Use `typeof val === 'number'` instead.
46
*/
47
goog.utils.isNumber = function(val) {
48
return typeof val === 'number';
49
};
50
51
52
/**
53
* Returns true if the specified value is a boolean.
54
*
55
* @param {?} val Variable to test.
56
* @return {boolean} Whether variable is a boolean.
57
* @deprecated Use `typeof val === 'boolean'` instead.
58
*/
59
goog.utils.isBoolean = function(val) {
60
return typeof val === 'boolean';
61
};
62
63
64
/**
65
* Returns true if the specified value is a function.
66
*
67
* @param {?} val Variable to test.
68
* @return {boolean} Whether variable is a function.
69
* @deprecated Use `typeof val === 'function'` instead.
70
*/
71
goog.utils.isFunction = function(val) {
72
return typeof val === 'function';
73
};
74
75
76
/**
77
* Returns true if the specified value is null.
78
*
79
* @param {?} val Variable to test.
80
* @return {boolean} Whether variable is null.
81
* @deprecated Use `val === null` instead.
82
*/
83
goog.utils.isNull = function(val) {
84
return val === null;
85
};
86
87
88
/**
89
* Returns true if the specified value is an object. This includes arrays and
90
* functions.
91
*
92
* @param {?} val Variable to test.
93
* @return {boolean} Whether variable is an object.
94
*/
95
goog.utils.isObject = function(val) {
96
var type = typeof val;
97
return type == 'object' && val != null || type == 'function';
98
};
99
100
101
/**
102
* Returns true if the specified value is an array.
103
*
104
* @param {?} val Variable to test.
105
* @return {boolean} Whether variable is an array.
106
* @deprecated Use `Array.isArray(val)` instead.
107
*/
108
goog.utils.isArray = function(val) {
109
return Array.isArray(val);
110
};
111
112
113
/**
114
* This is a "fixed" version of the typeof operator. It differs from the typeof
115
* operator in such a way that null returns 'null' and arrays return 'array'.
116
*
117
* @param {?} value The value to get the type of.
118
* @return {string} The name of the type.
119
*/
120
goog.utils.typeOf = function(value) {
121
var s = typeof value;
122
if (s == 'object') {
123
if (value) {
124
if (value instanceof Array) {
125
return 'array';
126
} else if (value instanceof Object) {
127
return s;
128
}
129
130
var className = Object.prototype.toString.call(
131
/** @type {!Object} */ (value));
132
133
if (className == '[object Window]') {
134
return 'object';
135
}
136
137
if ((className == '[object Array]' ||
138
typeof value.length == 'number' &&
139
typeof value.splice != 'undefined' &&
140
typeof value.propertyIsEnumerable != 'undefined' &&
141
!value.propertyIsEnumerable('splice'))) {
142
return 'array';
143
}
144
145
if ((className == '[object Function]' ||
146
typeof value.call != 'undefined' &&
147
typeof value.propertyIsEnumerable != 'undefined' &&
148
!value.propertyIsEnumerable('call'))) {
149
return 'function';
150
}
151
152
} else {
153
return 'null';
154
}
155
156
} else if (s == 'function' && typeof value.call == 'undefined') {
157
return 'object';
158
}
159
return s;
160
};
161
162
163
/**
164
* Returns true if the specified value is array-like. An array-like value has
165
* a numeric length property.
166
*
167
* @param {?} val Variable to test.
168
* @return {boolean} Whether variable is array-like.
169
*/
170
goog.utils.isArrayLike = function(val) {
171
var type = goog.utils.typeOf(val);
172
return type == 'array' || type == 'object' && typeof val.length == 'number';
173
};
174
175
176
/**
177
* Inherit the prototype methods from one constructor into another.
178
*
179
* @param {!Function} childCtor Child class.
180
* @param {!Function} parentCtor Parent class.
181
* @return {!Object} The prototype of the child class.
182
*/
183
goog.utils.inherits = function(childCtor, parentCtor) {
184
/** @constructor */
185
function tempCtor() {}
186
tempCtor.prototype = parentCtor.prototype;
187
childCtor.superClass_ = parentCtor.prototype;
188
childCtor.prototype = new tempCtor();
189
/** @override */
190
childCtor.prototype.constructor = childCtor;
191
192
/**
193
* Calls superclass constructor/method.
194
*
195
* @param {!Object} me Should always be "this".
196
* @param {string} methodName The method name to call. Calling superclass
197
* constructor can be done with the special string 'constructor'.
198
* @param {...*} var_args The arguments to pass to superclass
199
* method/constructor.
200
* @return {*} The return value of the superclass method/constructor.
201
*/
202
childCtor.base = function(me, methodName, var_args) {
203
var args = new Array(arguments.length - 2);
204
for (var i = 2; i < arguments.length; i++) {
205
args[i - 2] = arguments[i];
206
}
207
return parentCtor.prototype[methodName].apply(me, args);
208
};
209
210
return childCtor.prototype;
211
};
212
213
214
/**
215
* Adds a getInstance() static method that always returns the same instance.
216
*
217
* @param {!Function} ctor The constructor for the class.
218
*/
219
goog.utils.addSingletonGetter = function(ctor) {
220
ctor.instance_ = undefined;
221
ctor.getInstance = function() {
222
if (ctor.instance_) {
223
return ctor.instance_;
224
}
225
return ctor.instance_ = new ctor;
226
};
227
};
228
229
230
/**
231
* The property used to store the unique ID on objects.
232
* @private {string}
233
* @const
234
*/
235
goog.utils.UID_PROPERTY_ = 'closure_uid_' + ((Math.random() * 1e9) >>> 0);
236
237
238
/**
239
* Counter for unique IDs.
240
* @private {number}
241
*/
242
goog.utils.uidCounter_ = 0;
243
244
245
/**
246
* Gets a unique ID for an object. This mutates the object so that further calls
247
* with the same object as a parameter returns the same value.
248
*
249
* @param {Object} obj The object to get the unique ID for.
250
* @return {number} The unique ID for the object.
251
*/
252
goog.utils.getUid = function(obj) {
253
return obj[goog.utils.UID_PROPERTY_] ||
254
(obj[goog.utils.UID_PROPERTY_] = ++goog.utils.uidCounter_);
255
};
256
257
258
/**
259
* Whether the given object is already assigned a unique ID.
260
*
261
* @param {!Object} obj The object to check.
262
* @return {boolean} Whether there is an assigned unique id for the object.
263
*/
264
goog.utils.hasUid = function(obj) {
265
return !!obj[goog.utils.UID_PROPERTY_];
266
};
267
268
269
/**
270
* Removes the unique ID from an object.
271
*
272
* @param {Object} obj The object to remove the unique ID from.
273
*/
274
goog.utils.removeUid = function(obj) {
275
if (obj !== null && 'removeAttribute' in obj) {
276
obj.removeAttribute(goog.utils.UID_PROPERTY_);
277
}
278
279
try {
280
delete obj[goog.utils.UID_PROPERTY_];
281
} catch (ex) {
282
}
283
};
284
285
286
/**
287
* An alias for Function.prototype.bind that works in older browsers.
288
*
289
* @param {?function(this:T, ...)} fn A function to partially apply.
290
* @param {T} selfObj Specifies the object which this should point to when the
291
* function is run.
292
* @param {...*} var_args Additional arguments that are partially applied to the
293
* function.
294
* @return {!Function} A partially-applied form of the function passed as an
295
* argument.
296
* @template T
297
*/
298
goog.utils.bind = function(fn, selfObj, var_args) {
299
if (arguments.length > 2) {
300
var boundArgs = Array.prototype.slice.call(arguments, 2);
301
return function() {
302
var newArgs = Array.prototype.slice.call(arguments);
303
Array.prototype.unshift.apply(newArgs, boundArgs);
304
return fn.apply(selfObj, newArgs);
305
};
306
} else {
307
return function() {
308
return fn.apply(selfObj, arguments);
309
};
310
}
311
};
312
313
314
/**
315
* Like goog.utils.bind(), except that a 'this object' is not required. Useful
316
* when the target function is already bound.
317
*
318
* @param {?function(...)} fn A function to partially apply.
319
* @param {...*} var_args Additional arguments that are partially applied to fn.
320
* @return {!Function} A partially-applied form of the function passed as an
321
* argument.
322
*/
323
goog.utils.partial = function(fn, var_args) {
324
var args = Array.prototype.slice.call(arguments, 1);
325
return function() {
326
var newArgs = args.slice();
327
newArgs.push.apply(newArgs, arguments);
328
return fn.apply(this, newArgs);
329
};
330
};
331
332
333
/**
334
* A function that always throws an error. Useful for defining abstract methods.
335
*
336
* @throws {Error} Always throws Error.
337
*/
338
goog.utils.abstractMethod = function() {
339
throw Error('unimplemented abstract method');
340
};
341
342
343
/**
344
* Returns the current time as a number of milliseconds since epoch.
345
*
346
* @return {number} Current time in milliseconds.
347
* @deprecated Use `Date.now()` instead.
348
*/
349
goog.utils.now = function() {
350
return Date.now();
351
};
352
353