function set(name, value){ if(value == undefined){ value = name; } ... }
function send(data){ if(arguments[1] instanceof Function){ var callback = arguments[1]; var options = null; } else if(arguments[2] instanceof Function){ var callback = arguments[2]; var options = arguments[1]; } ... }
function send(){ function __send(id, data){ ... } if(arguments[0] instanceof Array){ var pack = arguments[0]; var callback = arguments[1]; for(var i=0; i<pack.length; i++){ __send.apply(this, pack[i]); } } }
function send(){ function __send(){ var args = new argSchema(arguments); if(args.checkin('number id', 'object data', 'opt bool newArg', 'opt function callback')){ // args.id, args.data, args.newArg, args.callback ... } ... } var args = new argSchema(arguments); if(args.checkin('array pack', 'opt bool newArg', 'opt function callback')){ for(var i=0; i<pack.length; i++){ __send.apply(this, args.pack[i]); } } else if(args.checkin('number id', 'object data', 'opt bool newArg', 'opt function callback')){ __send(args.id, args.data, args.newArg, args.callback); } }
var is = function( type, obj ) { return Object.prototype.toString.call( obj ) === "[object "+ type +"]"; } is['string'] = function(obj){ return (typeof obj == 'string' || obj instanceof String); } is.number = function(obj){ return (typeof obj == 'number' || obj instanceof Number); } is.bool = function(obj){ return (typeof obj == 'boolean' || obj instanceof Boolean); } is.object = function(obj){ return (typeof obj == 'object'); } is.array = function(obj){ return (obj instanceof Array); } is.func = function(obj){ return (obj instanceof Function); } is.set = function(obj){ return (obj != undefined && obj != null); } is.unset = function(obj){ return !this.set(obj); } function argSchema(argArr){ this.checkin = function(){ var arr, qual, type, name; function check(type, arg){ if(is.unset(arg)) return true; switch(type){ case 'string': if(is.string(arg)){ this[name] = arg; } else return false; break; case 'number': if(is.number(arg)){ this[name] = arg; } else return false; break; case 'bool': if(is.bool(arg)){ this[name] = arg; } else return false; break; case 'object': if(is.object(arg)){ this[name] = arg; } else return false; break; case 'function': if(is.func(arg)){ this[name] = arg; } else return false; break; case 'array': if(is.array(arg)){ this[name] = arg; } else return false; break; } return true; } for(var i=0, j=0; i<arguments.length; i++){ arr = arguments[i].split(' '); qual = type = name = null; if(arr.length == 3){ qual = arr[0]; type = arr[1]; name = arr[2]; } else if(arr.length == 2){ type = arr[0]; name = arr[1]; } delete this[name]; if(qual == 'opt'){ if(check.call(this, type, argArr[j])){ j++; } } else{ if(!check.call(this, type, argArr[j])) return false; j++; } } return true; } }
Source: https://habr.com/ru/post/195816/
All Articles