!function (name, context, definition) { // Export ArgueJS as module, or __ as global, if not using module loaders. if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') { module.exports = definition(); } else if (typeof define === 'function' && typeof define.amd === 'object') { define(function () { return definition(); }); } else { var oldValue = context[name]; context[name] = definition(); context[name].noConflict = function(){ context[name] = oldValue; return this; } } }('__', this, function () { var isArray = Array.isArray || function(obj){ // Util function to check if an object is actually an Array. return toString.call(obj) == '[object Array]'; } var __ = function(signature, upperArguments) { // The ArgueJS constructor method. var input; if (!__.belongs(signature, Object)) // If 'signature' is not defined or is not an object, goes away. throw new Error("parameter 'signature' waiting for Object argument but received " + __.typeof(signature)); if (upperArguments !== undefined && !__.belongs(upperArguments, Array) && !__.belongs(upperArguments, 'Arguments')) // If 'upperArguments' is defined it must be a valid arguments list throw new Error("parameter 'upperArguments' waiting for Array or Arguments argument but received " + __.typeof(upperArguments)); try{ // Assumes upperArguments as the passed arguments or try to infer from the caller function. input = upperArguments || arguments.callee.caller.arguments; } catch(e){ throw new Error('It is not possible to infer arguments in strict mode. See http://github.com/zvictor/ArgueJs#propagating-arguments for alternatives.'); } // Makes a copy of the input, transforming Arguments (if is the case) into Array. // See more at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array#Array_generic_methods input = Array.prototype.slice.call( input ); var paramSum = 0; for (var name in signature) { if (!signature.hasOwnProperty(name)) continue; // We just count how many parameters we have to evaluate... paramSum++; } if(input.length > paramSum) // Ops, someone really likes to talk here! throw new Error("Too many arguments"); // As we can have optional arguments, // there are many ways to consider the same call to a function. // A call (1, 3) to range([Number], Number, [Number]) // could be validated as (1, 3), (1, 3, undefined) or (undefined, 1, 3). // In order to determine which one has the best fit to the given signature, // we need to generate all of them. var expansion = [input]; // Here we start to expand the passed arguments. // We start with just the given input... var pivotIndex = -1; for (var name in signature) { if (!signature.hasOwnProperty(name)) continue; // ... and for each parameter... if(!isNaN(parseFloat(name)) && isFinite(name)) throw new Error("NameError: a parameter name can not be numeric"); pivotIndex++; var optional = isArray(signature[name]); var type = (optional) ? signature[name][0] : signature[name]; if(type === undefined) throw new TypeError("unsupported parameter type "+type); var copy = expansion.slice(0); // (we make a copy here to avoid infinity loop) for(var i=0; i