Path: blob/trunk/third_party/closure/goog/utils/utils.js
4501 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Utility functions that were removed from goog.base but are8* still needed by the Selenium atoms. These provide compatibility shims for9* legacy Closure Library code.10*/1112goog.provide('goog.utils');131415/**16* Returns true if the specified value is not undefined.17*18* @param {?} val Variable to test.19* @return {boolean} Whether variable is defined.20* @deprecated Use `val !== undefined` instead.21*/22goog.utils.isDef = function(val) {23return val !== undefined;24};252627/**28* Returns true if the specified value is a string.29*30* @param {?} val Variable to test.31* @return {boolean} Whether variable is a string.32* @deprecated Use `typeof val === 'string'` instead.33*/34goog.utils.isString = function(val) {35return typeof val === 'string';36};373839/**40* Returns true if the specified value is a number.41*42* @param {?} val Variable to test.43* @return {boolean} Whether variable is a number.44* @deprecated Use `typeof val === 'number'` instead.45*/46goog.utils.isNumber = function(val) {47return typeof val === 'number';48};495051/**52* Returns true if the specified value is a boolean.53*54* @param {?} val Variable to test.55* @return {boolean} Whether variable is a boolean.56* @deprecated Use `typeof val === 'boolean'` instead.57*/58goog.utils.isBoolean = function(val) {59return typeof val === 'boolean';60};616263/**64* Returns true if the specified value is a function.65*66* @param {?} val Variable to test.67* @return {boolean} Whether variable is a function.68* @deprecated Use `typeof val === 'function'` instead.69*/70goog.utils.isFunction = function(val) {71return typeof val === 'function';72};737475/**76* Returns true if the specified value is null.77*78* @param {?} val Variable to test.79* @return {boolean} Whether variable is null.80* @deprecated Use `val === null` instead.81*/82goog.utils.isNull = function(val) {83return val === null;84};858687/**88* Returns true if the specified value is an object. This includes arrays and89* functions.90*91* @param {?} val Variable to test.92* @return {boolean} Whether variable is an object.93*/94goog.utils.isObject = function(val) {95var type = typeof val;96return type == 'object' && val != null || type == 'function';97};9899100/**101* Returns true if the specified value is an array.102*103* @param {?} val Variable to test.104* @return {boolean} Whether variable is an array.105* @deprecated Use `Array.isArray(val)` instead.106*/107goog.utils.isArray = function(val) {108return Array.isArray(val);109};110111112/**113* This is a "fixed" version of the typeof operator. It differs from the typeof114* operator in such a way that null returns 'null' and arrays return 'array'.115*116* @param {?} value The value to get the type of.117* @return {string} The name of the type.118*/119goog.utils.typeOf = function(value) {120var s = typeof value;121if (s == 'object') {122if (value) {123if (value instanceof Array) {124return 'array';125} else if (value instanceof Object) {126return s;127}128129var className = Object.prototype.toString.call(130/** @type {!Object} */ (value));131132if (className == '[object Window]') {133return 'object';134}135136if ((className == '[object Array]' ||137typeof value.length == 'number' &&138typeof value.splice != 'undefined' &&139typeof value.propertyIsEnumerable != 'undefined' &&140!value.propertyIsEnumerable('splice'))) {141return 'array';142}143144if ((className == '[object Function]' ||145typeof value.call != 'undefined' &&146typeof value.propertyIsEnumerable != 'undefined' &&147!value.propertyIsEnumerable('call'))) {148return 'function';149}150151} else {152return 'null';153}154155} else if (s == 'function' && typeof value.call == 'undefined') {156return 'object';157}158return s;159};160161162/**163* Returns true if the specified value is array-like. An array-like value has164* a numeric length property.165*166* @param {?} val Variable to test.167* @return {boolean} Whether variable is array-like.168*/169goog.utils.isArrayLike = function(val) {170var type = goog.utils.typeOf(val);171return type == 'array' || type == 'object' && typeof val.length == 'number';172};173174175/**176* Inherit the prototype methods from one constructor into another.177*178* @param {!Function} childCtor Child class.179* @param {!Function} parentCtor Parent class.180* @return {!Object} The prototype of the child class.181*/182goog.utils.inherits = function(childCtor, parentCtor) {183/** @constructor */184function tempCtor() {}185tempCtor.prototype = parentCtor.prototype;186childCtor.superClass_ = parentCtor.prototype;187childCtor.prototype = new tempCtor();188/** @override */189childCtor.prototype.constructor = childCtor;190191/**192* Calls superclass constructor/method.193*194* @param {!Object} me Should always be "this".195* @param {string} methodName The method name to call. Calling superclass196* constructor can be done with the special string 'constructor'.197* @param {...*} var_args The arguments to pass to superclass198* method/constructor.199* @return {*} The return value of the superclass method/constructor.200*/201childCtor.base = function(me, methodName, var_args) {202var args = new Array(arguments.length - 2);203for (var i = 2; i < arguments.length; i++) {204args[i - 2] = arguments[i];205}206return parentCtor.prototype[methodName].apply(me, args);207};208209return childCtor.prototype;210};211212213/**214* Adds a getInstance() static method that always returns the same instance.215*216* @param {!Function} ctor The constructor for the class.217*/218goog.utils.addSingletonGetter = function(ctor) {219ctor.instance_ = undefined;220ctor.getInstance = function() {221if (ctor.instance_) {222return ctor.instance_;223}224return ctor.instance_ = new ctor;225};226};227228229/**230* The property used to store the unique ID on objects.231* @private {string}232* @const233*/234goog.utils.UID_PROPERTY_ = 'closure_uid_' + ((Math.random() * 1e9) >>> 0);235236237/**238* Counter for unique IDs.239* @private {number}240*/241goog.utils.uidCounter_ = 0;242243244/**245* Gets a unique ID for an object. This mutates the object so that further calls246* with the same object as a parameter returns the same value.247*248* @param {Object} obj The object to get the unique ID for.249* @return {number} The unique ID for the object.250*/251goog.utils.getUid = function(obj) {252return obj[goog.utils.UID_PROPERTY_] ||253(obj[goog.utils.UID_PROPERTY_] = ++goog.utils.uidCounter_);254};255256257/**258* Whether the given object is already assigned a unique ID.259*260* @param {!Object} obj The object to check.261* @return {boolean} Whether there is an assigned unique id for the object.262*/263goog.utils.hasUid = function(obj) {264return !!obj[goog.utils.UID_PROPERTY_];265};266267268/**269* Removes the unique ID from an object.270*271* @param {Object} obj The object to remove the unique ID from.272*/273goog.utils.removeUid = function(obj) {274if (obj !== null && 'removeAttribute' in obj) {275obj.removeAttribute(goog.utils.UID_PROPERTY_);276}277278try {279delete obj[goog.utils.UID_PROPERTY_];280} catch (ex) {281}282};283284285/**286* An alias for Function.prototype.bind that works in older browsers.287*288* @param {?function(this:T, ...)} fn A function to partially apply.289* @param {T} selfObj Specifies the object which this should point to when the290* function is run.291* @param {...*} var_args Additional arguments that are partially applied to the292* function.293* @return {!Function} A partially-applied form of the function passed as an294* argument.295* @template T296*/297goog.utils.bind = function(fn, selfObj, var_args) {298if (arguments.length > 2) {299var boundArgs = Array.prototype.slice.call(arguments, 2);300return function() {301var newArgs = Array.prototype.slice.call(arguments);302Array.prototype.unshift.apply(newArgs, boundArgs);303return fn.apply(selfObj, newArgs);304};305} else {306return function() {307return fn.apply(selfObj, arguments);308};309}310};311312313/**314* Like goog.utils.bind(), except that a 'this object' is not required. Useful315* when the target function is already bound.316*317* @param {?function(...)} fn A function to partially apply.318* @param {...*} var_args Additional arguments that are partially applied to fn.319* @return {!Function} A partially-applied form of the function passed as an320* argument.321*/322goog.utils.partial = function(fn, var_args) {323var args = Array.prototype.slice.call(arguments, 1);324return function() {325var newArgs = args.slice();326newArgs.push.apply(newArgs, arguments);327return fn.apply(this, newArgs);328};329};330331332/**333* A function that always throws an error. Useful for defining abstract methods.334*335* @throws {Error} Always throws Error.336*/337goog.utils.abstractMethod = function() {338throw Error('unimplemented abstract method');339};340341342/**343* Returns the current time as a number of milliseconds since epoch.344*345* @return {number} Current time in milliseconds.346* @deprecated Use `Date.now()` instead.347*/348goog.utils.now = function() {349return Date.now();350};351352353