betajs/betajs-scoped

View on GitHub
src/main/helper.js

Summary

Maintainability
B
5 hrs
Test Coverage
/*::
declare module Helper {
    declare function extend<A, B>(a: A, b: B): A & B;
}
*/

var Helper = (function () {  
/** 
 * This helper module provides auxiliary functions for the Scoped system.
 * 
 * @module Helper
 * @access private
 */
return { 
        
    /**
     * Attached a context to a function.
     * 
     * @param {object} obj context for the function
     * @param {function} func function
     * 
     * @return function with attached context
     */
    method: function (obj, func) {
        return function () {
            return func.apply(obj, arguments);
        };
    },

    
    /**
     * Extend a base object with all attributes of a second object.
     * 
     * @param {object} base base object
     * @param {object} overwrite second object
     * 
     * @return {object} extended base object
     */
    extend: function (base, overwrite) {
        base = base || {};
        overwrite = overwrite || {};
        for (var key in overwrite)
            base[key] = overwrite[key];
        return base;
    },
    
    
    /**
     * Returns the type of an object, particulary returning 'array' for arrays.
     * 
     * @param obj object in question
     * 
     * @return {string} type of object
     */
    typeOf: function (obj) {
        return Object.prototype.toString.call(obj) === '[object Array]' ? "array" : typeof obj;
    },
    
    
    /**
     * Returns whether an object is null, undefined, an empty array or an empty object.
     * 
     * @param obj object in question
     * 
     * @return true if object is empty
     */
    isEmpty: function (obj) {
        if (obj === null || typeof obj === "undefined")
            return true;
        if (this.typeOf(obj) == "array")
            return obj.length === 0;
        if (typeof obj !== "object")
            return false;
        for (var key in obj)
            return false;
        return true;
    },
    
    
    /**
     * Matches function arguments against some pattern.
     * 
     * @param {array} args function arguments
     * @param {object} pattern typed pattern
     * 
     * @return {object} matched arguments as associative array 
     */    
    matchArgs: function (args, pattern) {
        var i = 0;
        var result = {};
        for (var key in pattern) {
            if (pattern[key] === true || this.typeOf(args[i]) == pattern[key]) {
                result[key] = args[i];
                i++;
            } else if (this.typeOf(args[i]) == "undefined")
                i++;
        }
        return result;
    },
    
    
    /**
     * Stringifies a value as JSON and functions to string representations.
     * 
     * @param value value to be stringified
     * 
     * @return stringified value
     */
    stringify: function (value) {
        if (this.typeOf(value) == "function")
            return "" + value;
        return JSON.stringify(value);
    }    

    
};}).call(this);