yui-lang.js revision 1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749
(function() {
/**
* Provides the language utilites and extensions used by the library
* @class Lang
* @static
*/
Y.Lang = Y.Lang || {};
var L = Y.Lang,
ARRAY = 'array',
BOOLEAN = 'boolean',
DATE = 'date',
ERROR = 'error',
FUNCTION = 'function',
NUMBER = 'number',
OBJECT = 'object',
REGEX = 'regexp',
STRING = 'string',
TOSTRING = Object.prototype.toString,
UNDEFINED = 'undefined',
TYPES = {
'undefined' : UNDEFINED,
'number' : NUMBER,
'boolean' : BOOLEAN,
'string' : STRING,
'[object Function]' : FUNCTION,
'[object RegExp]' : REGEX,
'[object Array]' : ARRAY,
'[object Date]' : DATE,
'[object Error]' : ERROR
};
/**
* Determines whether or not the provided item is an array.
* Returns false for array-like collections such as the
* function arguments collection or HTMLElement collection
* will return false. You can use @see Array.test if you
* want to
* @method isArray
* @static
* @param o The object to test
* @return {boolean} true if o is an array
*/
L.isArray = function(o) {
return L.type(o) === ARRAY;
};
/**
* Determines whether or not the provided item is a boolean
* @method isBoolean
* @static
* @param o The object to test
* @return {boolean} true if o is a boolean
*/
L.isBoolean = function(o) {
return typeof o === BOOLEAN;
};
/**
* Determines whether or not the provided item is a function
* Note: Internet Explorer thinks certain functions are objects:
*
* var obj = document.createElement("object");
* Y.Lang.isFunction(obj.getAttribute) // reports false in IE
*
* var input = document.createElement("input"); // append to body
* Y.Lang.isFunction(input.focus) // reports false in IE
*
* You will have to implement additional tests if these functions
* matter to you.
*
* @method isFunction
* @static
* @param o The object to test
* @return {boolean} true if o is a function
*/
L.isFunction = function(o) {
return L.type(o) === FUNCTION;
};
/**
* Determines whether or not the supplied item is a date instance
* @method isDate
* @static
* @param o The object to test
* @return {boolean} true if o is a date
*/
L.isDate = function(o) {
return o instanceof Date;
};
/**
* Determines whether or not the provided item is null
* @method isNull
* @static
* @param o The object to test
* @return {boolean} true if o is null
*/
L.isNull = function(o) {
return o === null;
};
/**
* Determines whether or not the provided item is a legal number
* @method isNumber
* @static
* @param o The object to test
* @return {boolean} true if o is a number
*/
L.isNumber = function(o) {
return typeof o === NUMBER && isFinite(o);
};
/**
* Determines whether or not the provided item is of type object
* or function
* @method isObject
* @static
* @param o The object to test
* @param failfn {boolean} fail if the input is a function
* @return {boolean} true if o is an object
*/
L.isObject = function(o, failfn) {
return (o && (typeof o === OBJECT || (!failfn && L.isFunction(o)))) || false;
};
/**
* Determines whether or not the provided item is a string
* @method isString
* @static
* @param o The object to test
* @return {boolean} true if o is a string
*/
L.isString = function(o) {
return typeof o === STRING;
};
/**
* Determines whether or not the provided item is undefined
* @method isUndefined
* @static
* @param o The object to test
* @return {boolean} true if o is undefined
*/
L.isUndefined = function(o) {
return typeof o === UNDEFINED;
};
/**
* Returns a string without any leading or trailing whitespace. If
* the input is not a string, the input will be returned untouched.
* @method trim
* @static
* @param s {string} the string to trim
* @return {string} the trimmed string
*/
L.trim = function(s){
try {
return s.replace(/^\s+|\s+$/g, "");
} catch(e) {
return s;
}
};
/**
* A convenience method for detecting a legitimate non-null value.
* Returns false for null/undefined/NaN, true for other values,
* including 0/false/''
* @method isValue
* @static
* @param o The item to test
* @return {boolean} true if it is not null/undefined/NaN || false
*/
L.isValue = function(o) {
var t = L.type(o);
return (t && t !== UNDEFINED) || false;
};
/**
* Returns a string representing the type of the item passed in.
* @method type
* @param o the item to test
* @return {string} the detected type
*/
L.type = function (o) {
return TYPES[typeof o] || TYPES[TOSTRING.call(o)] || (o ? 'object' : 'null');
};
})();