/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: * ******************************************************************* * Project: Function Enchant * * Version: 1.01 * * Author: RabiatoR * ******************************************************************* * JavaScript "Function" object functionnaly enchant library * Includes: * Func Function().bind( [Mixed scope] ); * Func Function().checker( Array arguments_types [, Number arguments_min] ); * * Changelog: * [1.01] * Syntax reformatted. * [1.0] * First release. * * */ /**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**/ /* * Syntax: Function().bind( [Mixed scope] ); * Return: Function; *****************************************************************/ if( typeof( Function.prototype.bind ) == 'undefined' ){ Function.prototype.bind = function( scope ){ var self = this; return function(){ return self.apply( scope, arguments ); } } } /* * Syntax: Function().checker( Array arguments_types [, Number arguments_min] ); * Return: Function; * Commnt: Allowed types( "Any", [String,Number,"Function",Object,Date], String, Number, "Function", Object, Date ); *****************************************************************/ if( typeof( Function.prototype.checker ) == 'undefined' ){ Function.prototype.checker = function( arguments_types, arguments_min ){ var self = this; var args = []; var argc = arguments_min; if( typeof( argc ) == 'undefined' ){ argc = null; }else{ argc = parseInt( argc, 10 ); if( isNaN( argc ) || argc < 0 ){ argc = null; } } for( var k in arguments_types ){ var type = arguments_types[k]; if( typeof( type ) == "function" ){ type = arguments_types[k].toString().match( /function\s*([\w\$]*)\s*\(/ )[1]; }else if( typeof( type ) == "object" && type instanceof Array ){ type = []; for( var _k in arguments_types[k] ){ type[type.length] = arguments_types[k][_k]; if( typeof( type[type.length-1] ) == "function" ){ type[type.length-1] = arguments_types[k][_k].toString().match( /function\s*([\w\$]*)\s*\(/ )[1]; } } } if( typeof( type ) == "string" ){ type = type.toLowerCase(); }else{ for( var _k in type ){ type[_k] = type[_k].toLowerCase(); } } args[args.length] = type; } return function(){ if( argc !== null ){ if( arguments.length != argc ){ return false; } } for( var i = 0, l = args.length; i < l; i++ ){ if( typeof( args[i] ) == 'string' ){ if( args[i] != 'any' && typeof( arguments[i] ) != args[i] ){ return false; } }else if( typeof( args[i] ) == 'object' && args[i] instanceof Array ){ var success = false; for( var j = 0, m = args[i].length; j < m; j++ ){ if( typeof( arguments[i] ) == args[i][j] ){ success = true; break; } } if( !success ){ return false; } } } return self.apply( this, arguments ); } } }