# Yahoo! UI Library

### yui 3.0.0b1

Yahoo! UI Library > yui > yui-lang.js (source view)
Search:
Filters Show Private Show Protected Show Deprecated

    (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',
    NULL      = 'null',
    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
    },

    TRIMREGEX = /^\s+|\s+$/g,
    EMPTYSTRING = '';

    /**
     * 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;
        return L.type(o) === 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(TRIMREGEX, EMPTYSTRING);
        } 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);
        switch (t) {
            case NUMBER:
                return isFinite(o);
            case NULL:
            case UNDEFINED:
                return false;
            default:
                return !!(t);
        }
    };

    /**
     * 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);
    };

    })();

Copyright © 2009 Yahoo! Inc. All rights reserved.