Socket
Socket
Sign inDemoInstall

overload2

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

overload2 - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

6

CHANGELOG.md

@@ -5,2 +5,8 @@ # overload2

## [0.3.1] - Mar 2nd, 2018
* Fixed 2 bugs on processing optional parameters, absent value may be regarded as arguments and taken to match the following parameters.
这个问题出现在可选参数的“让贤匹配”过程中,由于缺省值上位后未及时终止匹配,导致缺省值被当作实参继续“让贤匹配”流程,最终结果变得不可预测。
上一个版本标注为 RISKY 真是一语成谶!
## [0.3.0] - Feb 23th, 2018, RISKY

@@ -7,0 +13,0 @@

33

dist/overload2.js

@@ -565,2 +565,5 @@ /**

if (param.arrayed) newArgs.push(paramArgs);
// 此时,paramArgs.length == 1
// @tag 20180302b
else newArgs.push(paramArgs[0]);

@@ -593,6 +596,7 @@ };

var restParams = params.slice(paramCursor);
// 剩余形参数组,该数组在后续的让贤匹配中是不变的。
/*const*/ var restParams = params.slice(paramCursor);
// 抵达匹配边界时,若
// 仅匹配了最小数量的实参且该参数不可缺省,或者已是最后一个形式参数,
// 仅匹配了最小数量的实参且该参数不可缺省,或者已是最后一个形式参数(既所有形式参数均已匹配),
// 则直接固定参数值。

@@ -605,4 +609,13 @@ if (!param.absent && paramArgs.length == param.minSize || restParams.length == 0) {

// 否则,须尝试让贤。
// 让贤的策略是:
// 将剩余实参与剩余形参匹配(递归调用)。
// 如匹配不成功,则出让一个当前形参匹配的实参,列为剩余实参之首,再次尝试匹配。
// 直至,无法出让,或匹配成功。
// 剩余实参数组,该数组在后续的让贤匹配中是递增的。
var restArgs = args.slice(argCursor + 1);
// @tag 20180302a
// @fixbug
var lasttime = false;
// 步步让贤,直到让无可让。

@@ -618,2 +631,10 @@ do {

}
// 已是最后一次尝试(当前形参已出让所有实参)。
// @tag 20180302a
if (lasttime) {
// 就此罢了(liao)。
newArgs = null;
break;
}

@@ -625,3 +646,9 @@ // 如果已让无可让:

restArgs = paramArgs.concat(restArgs);
paramArgs = param.absentValue;
// paramArgs = param.absentValue;
// @tag 20180302b
paramArgs = param.arrayed ? param.absentValue : [ param.absentValue ];
// @tag 20180302a
lasttime = true;
continue;

@@ -628,0 +655,0 @@ }

2

dist/overload2.min.js

@@ -1,1 +0,1 @@

(function(global,undefined){var MODULE_REQUIRE,declareException=function e(t,r,n){if(!r)r=Error;var a=function(e){this.name="OVERLOAD2."+t;if(n){n.apply(this,arguments)}else{this.message=e}var a=new r;this.stack=[this.name+": "+this.message].concat(a.stack.split("\n").slice(2)).join("\n")};a.prototype=Object.create(r.prototype);a.prototype.consturctor=a;a.prototype.toString=a.prototype.valueOf=function(){return"["+this.name+": "+this.message+"]"};Object.defineProperty(a,"name",{value:t});return a},ERR={Generic:declareException("Error"),Arguments:declareException("ArgumentsError",TypeError,function(e,t){this.message=e+" argument(s) expected but actual "+t}),Type:declareException("TypeError",TypeError,function(e,t,r){this.message=e+" must be "+(typeof t=="string"?t:t.join(" | "))+": "+r}),Range:declareException("RangeError",RangeError,function(e,t,r){if(t instanceof Array){t=t.join(" | ")}this.message=e+" must be "+t+": "+r}),NotImplemented:declareException("EmptyException"),Unmatching:declareException("UnmatchingException",TypeError)},generateCreator=function(e){function t(t){return e.apply(this,t)}t.prototype=e.prototype;return function(){return new t(arguments)}},has=function(e,t){return e.indexOf(t)>=0},eachMatch=function(e,t){var r=true;for(var n=0;r&&n<e.length;n++){r=t(e[n],n)}return r},onceMatch=function(e,t){var r=false;for(var n=0;!r&&n<e.length;n++){r=t(e[n],n)}return r};function Absent(e){this.value=e}function Type(e){if(arguments.length!=1){throw new ERR.Arguments(1,arguments.length)}if(!(typeof e=="function"||e instanceof RegExp)){throw new ERR.Type("Type matcher",["Function","RegExp"],e)}if(!(this instanceof Type))return new Type(e);if(e instanceof RegExp){this.match=function(t){return e.test(t)}}else{this.match=e}}Type.ANY=new Type(function(){return true});Type.BOOLEAN=new Type(function(e){return typeof e==="boolean"});Type.CHAR=new Type(function(e){return typeof e==="string"&&e.length==1});Type.NUMBER=new Type(function(e){return typeof e==="number"});Type.PLAIN_OBJECT=new Type(function(e){return typeof e==="object"&&e.constructor===Object});Type.SCALAR=new Type(function(e){return["boolean","number","string"].indexOf(typeof e)>=0});Type.STRING=new Type(function(e){return typeof e==="string"});Type.enum=function(){var e=Array.from(arguments);return new Type(function(t){return e.indexOf(t)>=0})};Type.and=function(){var e=Array.from(arguments).map(Type.parse);return new Type(function(t){return eachMatch(e,function(e){return e.match(t)})})};Type.or=function(){var e=Array.from(arguments).map(Type.parse);return new Type(function(t){return onceMatch(e,function(e){return e.match(t)})})};Type.not=function(e){return new Type(function(t){return!e.match(t)})};Type.parse=function(e){if(typeof e=="string"){var t=TYPE_ALIAS[e];if(!t){throw new ERR.Range("type alias",Object.keys(TYPE_ALIAS),e)}e=t}else if(e instanceof Type){}else if(typeof e=="function"){e=function(e){return new Type(function(t){return t instanceof e})}(e)}else{throw new ERR.Type("Param type",["overload2.Type","string","Function"],e)}return e};function Param(){if(arguments.length==0){throw new ERR.Arguments("1+",0)}if(arguments.length==1&&arguments[0]instanceof Param){return arguments[0]}this.type=null;this.minSize=1;this.maxSize=1;this.nil=false;this.undef=false;this.absent=false;this.absentValue=undefined;this.arrayed=false;var setSize=function(e){if(typeof e=="string"){e=e.replace(/\s/g,"");if(e=="*"||e=="..."){this.minSize=0;this.maxSize=Infinity;this.arrayed=true;return true}if(e=="+"){this.minSize=1;this.maxSize=Infinity;this.arrayed=true;return true}if(/^\{.+\}$/.test(e)){e=e.slice(1,-1)}if(/^\d+$/.test(e)){this.minSize=this.maxSize=parseInt(e);this.arrayed=true;return true}if(/^,(\d+)$/.test(e)){this.minSize=0;this.maxSize=parseInt(RegExp.$1);this.arrayed=true;return true}if(/^(\d+),$/.test(e)){this.minSize=parseInt(RegExp.$1);this.maxSize=Infinity;this.arrayed=true;return true}if(/^(\d+),(\d+)$/.test(e)){this.minSize=parseInt(RegExp.$1);this.maxSize=parseInt(RegExp.$2);this.arrayed=true;return true}return false}if(Number.isInteger(e)&&e>0){this.minSize=this.maxSize=e;this.arrayed=true;return true}return false}.bind(this);var type=arguments[0],decos=[];if(setSize(type)){this.type=Type.ANY;return this}if(typeof type=="string"){decos=type.trim().split(/\s+/);type=decos.shift()}this.type=Type.parse(type);var DECOS=["NULL","UNDEFINED","ABSENT"],i;for(i=1;i<arguments.length;i++){if(typeof arguments[i]=="string"){decos=decos.concat(arguments[i].trim().split(/\s+/))}else if(Number.isInteger(arguments[i])){decos.push(arguments[i])}else if(arguments[i]instanceof Absent){this.absent=true;this.absentValue=arguments[i].value}}for(i=0;i<decos.length;i++){var rawDeco=decos[i];if(setSize(rawDeco))continue;if(/^=(.+)$/.test(rawDeco)){this.absent=true;this.absentValue=eval(RegExp.$1);continue}decos[i]=decos[i].toUpperCase();if(!has(DECOS,decos[i])){throw new ERR.Range("param decorator",DECOS,rawDeco)}}this.nil=has(decos,"NULL");this.undef=has(decos,"UNDEFINED");if(!this.absent){this.absent=has(decos,"ABSENT")}}Param.parse=generateCreator(Param);Param.prototype.satisfy=function(e){if(e===null)return this.nil;if(e===undefined)return this.undef;return this.type.match(e)};function ParamList(){var e=[];var t=0;var r=0;var n=true;for(var a=0,i;a<arguments.length;a++){i=arguments[a]instanceof Array?arguments[a]:[arguments[a]];e[a]=Param.parse.apply(null,i);r+=e[a].maxSize;t+=e[a].absent?0:e[a].minSize;n=n&&!(e[a].absent||e[a].arrayed)}this.easy=n;this.minSize=t;this.maxSize=r;this.params=e}ParamList.parse=generateCreator(ParamList);ParamList.prototype.satisfy=function(e){if(e.length!=this.params.length){return false}return eachMatch(this.params,function(t,r){return t.satisfy(e[r])})};ParamList.prototype.parse=function(e){if(e.length<this.minSize||this.maxSize<e.length){return null}if(this.easy){var t=eachMatch(this.params,function(t,r){return t.satisfy(e[r])});return t?e:null}var r=function(e,t){var n=[];var a=0,i=0;for(;a<e.length;a++){if(i>=t.length){return null}var o=t[i];var s=e[a];if(!o.absent&&o.minSize==1&&o.maxSize==1){if(!o.satisfy(s)){return null}else{n.push(s);i++}continue}if(e.length-a<o.minSize){if(o.absent){n.push(o.absentValue);a--;continue}return null}var u=[];var l=function(){if(o.arrayed)n.push(u);else n.push(u[0])};for(;a<e.length&&o.satisfy(e[a]);a++){u.push(e[a])}a--;i++;if(u.length<o.minSize){if(o.absent){n.push(o.absentValue);a-=u.length;continue}return null}var p=t.slice(i);if(!o.absent&&u.length==o.minSize||p.length==0){l();continue}var f=e.slice(a+1);do{var c=r(f,p);if(c!=null){l();n=n.concat(c);break}if(u.length==o.minSize){if(o.absent){f=u.concat(f);u=o.absentValue;continue}n=null;break}else{f.unshift(u.pop())}}while(true);return n}for(;n.length<t.length;i++){if(t[i].absent){n.push(t[i].absentValue)}else if(t[i].minSize==0){n.push([])}else{return null}}return n};return r(Array.from(e),this.params)};function Overload(){if(arguments.length==0){throw new ERR.Arguments("1+",0)}var e=Array.from(arguments);var t=e.pop();if(typeof t=="function"){this.method=t}else{throw new ERR.Generic("overloading implementation function missed")}if(typeof e[0]=="number"&&e.length==1){this.argLength=e[0]}else if(e[0]instanceof ParamList){if(e.length>1){throw new ERR.Arguments(2,e.length+1)}this.params=e[0]}else{this.params=ParamList.parse.apply(null,e)}}Overload.parse=generateCreator(Overload);Overload.prototype.exec=function(e,t){if(typeof this.argLength=="number"){if(t.length!=this.argLength)return false}else{t=this.params.parse(t);if(!t)return false}return[this.method.apply(e,t)]};function OverloadedFunction(){if(!(this instanceof OverloadedFunction))return new OverloadedFunction;this._defaultMethod=null;this._overloads=[]}OverloadedFunction.prototype.exec=function(){return this.apply(this,arguments)};OverloadedFunction.prototype.apply=function(e,t){if(this._overloads.length==0){throw new ERR.NotImplemented}for(var r=0,n;r<this._overloads.length;r++){n=this._overloads[r].exec(e,t);if(n){return n[0]}}if(this._defaultMethod){return this._defaultMethod.apply(e,t)}var a=[];for(var r=0,i;r<t.length;r++){i=t[r]===null?"null":typeof t[r];if(i==="object")i=t[r].constructor.name;a.push(i)}throw new ERR.Unmatching("Unmatching arguments: "+a.join(", "))};OverloadedFunction.prototype.call=function(e){var t=Array.from(arguments);return this.apply(e,t.slice(1))};OverloadedFunction.prototype.overload=function(){var e;if(arguments[0]instanceof Overload){e=arguments[0];if(arguments[1]){this._defaultMethod=e.method}if(arguments.length>2){}}else{e=Overload.parse.apply(null,arguments)}this._overloads.push(e);return this};OverloadedFunction.prototype.default=function(e){if(typeof e!="function"){throw new ERR.Generic("invalid default method")}if(arguments.length>1){}this._defaultMethod=e};function Overloader(){var e=new OverloadedFunction;var t=function(t){e.overload.apply(e,t)};if(arguments.length){}var r=function(){return e.apply(this,arguments)};r.overload=function(){t(arguments);return r};r.default=function(t){e.default(t);return r};return r}Overloader.ANY=Type.ANY;Overloader.BOOLEAN=Type.BOOLEAN;Overloader.CHAR=Type.CHAR;Overloader.NUMBER=Type.NUMBER;Overloader.SCALAR=Type.SCALAR;Overloader.STRING=Type.STRING;Overloader.enum=Type.enum;Overloader.Type=Type;Overloader.type=Type;Overloader.Param=Param;Overloader.param=Param.parse;Overloader.parseParam=Param.parse;Overloader.ParamList=ParamList;Overloader.paramList=ParamList.parse;Overloader.parseParamList=ParamList.parse;Overloader.Overload=Overload;Overloader.overload=Overload.parse;Overloader.parseOverload=Overload.parse;Overloader.Function=OverloadedFunction;Overloader.createFunction=OverloadedFunction;Overloader.absent=function(e){return new Absent(e)};for(var name in ERR){if(ERR.hasOwnProperty(name)){var Err=ERR[name];Overloader[Err.name]=Err}}var TYPE_ALIAS={"?":Type.ANY,any:Type.ANY,boolean:Type.BOOLEAN,char:Type.CHAR,number:Type.NUMBER,object:Type.PLAIN_OBJECT,scalar:Type.SCALAR,string:Type.STRING};if(typeof module=="object"&&typeof module.exports=="object"){module.exports=Overloader}else if(typeof global.define=="function"){global.define(function(){return Overloader})}else{global.overload2=Overloader}})(this);
(function(global,undefined){var MODULE_REQUIRE,declareException=function e(t,r,n){if(!r)r=Error;var a=function(e){this.name="OVERLOAD2."+t;if(n){n.apply(this,arguments)}else{this.message=e}var a=new r;this.stack=[this.name+": "+this.message].concat(a.stack.split("\n").slice(2)).join("\n")};a.prototype=Object.create(r.prototype);a.prototype.consturctor=a;a.prototype.toString=a.prototype.valueOf=function(){return"["+this.name+": "+this.message+"]"};Object.defineProperty(a,"name",{value:t});return a},ERR={Generic:declareException("Error"),Arguments:declareException("ArgumentsError",TypeError,function(e,t){this.message=e+" argument(s) expected but actual "+t}),Type:declareException("TypeError",TypeError,function(e,t,r){this.message=e+" must be "+(typeof t=="string"?t:t.join(" | "))+": "+r}),Range:declareException("RangeError",RangeError,function(e,t,r){if(t instanceof Array){t=t.join(" | ")}this.message=e+" must be "+t+": "+r}),NotImplemented:declareException("EmptyException"),Unmatching:declareException("UnmatchingException",TypeError)},generateCreator=function(e){function t(t){return e.apply(this,t)}t.prototype=e.prototype;return function(){return new t(arguments)}},has=function(e,t){return e.indexOf(t)>=0},eachMatch=function(e,t){var r=true;for(var n=0;r&&n<e.length;n++){r=t(e[n],n)}return r},onceMatch=function(e,t){var r=false;for(var n=0;!r&&n<e.length;n++){r=t(e[n],n)}return r};function Absent(e){this.value=e}function Type(e){if(arguments.length!=1){throw new ERR.Arguments(1,arguments.length)}if(!(typeof e=="function"||e instanceof RegExp)){throw new ERR.Type("Type matcher",["Function","RegExp"],e)}if(!(this instanceof Type))return new Type(e);if(e instanceof RegExp){this.match=function(t){return e.test(t)}}else{this.match=e}}Type.ANY=new Type(function(){return true});Type.BOOLEAN=new Type(function(e){return typeof e==="boolean"});Type.CHAR=new Type(function(e){return typeof e==="string"&&e.length==1});Type.NUMBER=new Type(function(e){return typeof e==="number"});Type.PLAIN_OBJECT=new Type(function(e){return typeof e==="object"&&e.constructor===Object});Type.SCALAR=new Type(function(e){return["boolean","number","string"].indexOf(typeof e)>=0});Type.STRING=new Type(function(e){return typeof e==="string"});Type.enum=function(){var e=Array.from(arguments);return new Type(function(t){return e.indexOf(t)>=0})};Type.and=function(){var e=Array.from(arguments).map(Type.parse);return new Type(function(t){return eachMatch(e,function(e){return e.match(t)})})};Type.or=function(){var e=Array.from(arguments).map(Type.parse);return new Type(function(t){return onceMatch(e,function(e){return e.match(t)})})};Type.not=function(e){return new Type(function(t){return!e.match(t)})};Type.parse=function(e){if(typeof e=="string"){var t=TYPE_ALIAS[e];if(!t){throw new ERR.Range("type alias",Object.keys(TYPE_ALIAS),e)}e=t}else if(e instanceof Type){}else if(typeof e=="function"){e=function(e){return new Type(function(t){return t instanceof e})}(e)}else{throw new ERR.Type("Param type",["overload2.Type","string","Function"],e)}return e};function Param(){if(arguments.length==0){throw new ERR.Arguments("1+",0)}if(arguments.length==1&&arguments[0]instanceof Param){return arguments[0]}this.type=null;this.minSize=1;this.maxSize=1;this.nil=false;this.undef=false;this.absent=false;this.absentValue=undefined;this.arrayed=false;var setSize=function(e){if(typeof e=="string"){e=e.replace(/\s/g,"");if(e=="*"||e=="..."){this.minSize=0;this.maxSize=Infinity;this.arrayed=true;return true}if(e=="+"){this.minSize=1;this.maxSize=Infinity;this.arrayed=true;return true}if(/^\{.+\}$/.test(e)){e=e.slice(1,-1)}if(/^\d+$/.test(e)){this.minSize=this.maxSize=parseInt(e);this.arrayed=true;return true}if(/^,(\d+)$/.test(e)){this.minSize=0;this.maxSize=parseInt(RegExp.$1);this.arrayed=true;return true}if(/^(\d+),$/.test(e)){this.minSize=parseInt(RegExp.$1);this.maxSize=Infinity;this.arrayed=true;return true}if(/^(\d+),(\d+)$/.test(e)){this.minSize=parseInt(RegExp.$1);this.maxSize=parseInt(RegExp.$2);this.arrayed=true;return true}return false}if(Number.isInteger(e)&&e>0){this.minSize=this.maxSize=e;this.arrayed=true;return true}return false}.bind(this);var type=arguments[0],decos=[];if(setSize(type)){this.type=Type.ANY;return this}if(typeof type=="string"){decos=type.trim().split(/\s+/);type=decos.shift()}this.type=Type.parse(type);var DECOS=["NULL","UNDEFINED","ABSENT"],i;for(i=1;i<arguments.length;i++){if(typeof arguments[i]=="string"){decos=decos.concat(arguments[i].trim().split(/\s+/))}else if(Number.isInteger(arguments[i])){decos.push(arguments[i])}else if(arguments[i]instanceof Absent){this.absent=true;this.absentValue=arguments[i].value}}for(i=0;i<decos.length;i++){var rawDeco=decos[i];if(setSize(rawDeco))continue;if(/^=(.+)$/.test(rawDeco)){this.absent=true;this.absentValue=eval(RegExp.$1);continue}decos[i]=decos[i].toUpperCase();if(!has(DECOS,decos[i])){throw new ERR.Range("param decorator",DECOS,rawDeco)}}this.nil=has(decos,"NULL");this.undef=has(decos,"UNDEFINED");if(!this.absent){this.absent=has(decos,"ABSENT")}}Param.parse=generateCreator(Param);Param.prototype.satisfy=function(e){if(e===null)return this.nil;if(e===undefined)return this.undef;return this.type.match(e)};function ParamList(){var e=[];var t=0;var r=0;var n=true;for(var a=0,i;a<arguments.length;a++){i=arguments[a]instanceof Array?arguments[a]:[arguments[a]];e[a]=Param.parse.apply(null,i);r+=e[a].maxSize;t+=e[a].absent?0:e[a].minSize;n=n&&!(e[a].absent||e[a].arrayed)}this.easy=n;this.minSize=t;this.maxSize=r;this.params=e}ParamList.parse=generateCreator(ParamList);ParamList.prototype.satisfy=function(e){if(e.length!=this.params.length){return false}return eachMatch(this.params,function(t,r){return t.satisfy(e[r])})};ParamList.prototype.parse=function(e){if(e.length<this.minSize||this.maxSize<e.length){return null}if(this.easy){var t=eachMatch(this.params,function(t,r){return t.satisfy(e[r])});return t?e:null}var r=function(e,t){var n=[];var a=0,i=0;for(;a<e.length;a++){if(i>=t.length){return null}var o=t[i];var s=e[a];if(!o.absent&&o.minSize==1&&o.maxSize==1){if(!o.satisfy(s)){return null}else{n.push(s);i++}continue}if(e.length-a<o.minSize){if(o.absent){n.push(o.absentValue);a--;continue}return null}var u=[];var l=function(){if(o.arrayed)n.push(u);else n.push(u[0])};for(;a<e.length&&o.satisfy(e[a]);a++){u.push(e[a])}a--;i++;if(u.length<o.minSize){if(o.absent){n.push(o.absentValue);a-=u.length;continue}return null}var p=t.slice(i);if(!o.absent&&u.length==o.minSize||p.length==0){l();continue}var f=e.slice(a+1);var c=false;do{var h=r(f,p);if(h!=null){l();n=n.concat(h);break}if(c){n=null;break}if(u.length==o.minSize){if(o.absent){f=u.concat(f);u=o.arrayed?o.absentValue:[o.absentValue];c=true;continue}n=null;break}else{f.unshift(u.pop())}}while(true);return n}for(;n.length<t.length;i++){if(t[i].absent){n.push(t[i].absentValue)}else if(t[i].minSize==0){n.push([])}else{return null}}return n};return r(Array.from(e),this.params)};function Overload(){if(arguments.length==0){throw new ERR.Arguments("1+",0)}var e=Array.from(arguments);var t=e.pop();if(typeof t=="function"){this.method=t}else{throw new ERR.Generic("overloading implementation function missed")}if(typeof e[0]=="number"&&e.length==1){this.argLength=e[0]}else if(e[0]instanceof ParamList){if(e.length>1){throw new ERR.Arguments(2,e.length+1)}this.params=e[0]}else{this.params=ParamList.parse.apply(null,e)}}Overload.parse=generateCreator(Overload);Overload.prototype.exec=function(e,t){if(typeof this.argLength=="number"){if(t.length!=this.argLength)return false}else{t=this.params.parse(t);if(!t)return false}return[this.method.apply(e,t)]};function OverloadedFunction(){if(!(this instanceof OverloadedFunction))return new OverloadedFunction;this._defaultMethod=null;this._overloads=[]}OverloadedFunction.prototype.exec=function(){return this.apply(this,arguments)};OverloadedFunction.prototype.apply=function(e,t){if(this._overloads.length==0){throw new ERR.NotImplemented}for(var r=0,n;r<this._overloads.length;r++){n=this._overloads[r].exec(e,t);if(n){return n[0]}}if(this._defaultMethod){return this._defaultMethod.apply(e,t)}var a=[];for(var r=0,i;r<t.length;r++){i=t[r]===null?"null":typeof t[r];if(i==="object")i=t[r].constructor.name;a.push(i)}throw new ERR.Unmatching("Unmatching arguments: "+a.join(", "))};OverloadedFunction.prototype.call=function(e){var t=Array.from(arguments);return this.apply(e,t.slice(1))};OverloadedFunction.prototype.overload=function(){var e;if(arguments[0]instanceof Overload){e=arguments[0];if(arguments[1]){this._defaultMethod=e.method}if(arguments.length>2){}}else{e=Overload.parse.apply(null,arguments)}this._overloads.push(e);return this};OverloadedFunction.prototype.default=function(e){if(typeof e!="function"){throw new ERR.Generic("invalid default method")}if(arguments.length>1){}this._defaultMethod=e};function Overloader(){var e=new OverloadedFunction;var t=function(t){e.overload.apply(e,t)};if(arguments.length){}var r=function(){return e.apply(this,arguments)};r.overload=function(){t(arguments);return r};r.default=function(t){e.default(t);return r};return r}Overloader.ANY=Type.ANY;Overloader.BOOLEAN=Type.BOOLEAN;Overloader.CHAR=Type.CHAR;Overloader.NUMBER=Type.NUMBER;Overloader.SCALAR=Type.SCALAR;Overloader.STRING=Type.STRING;Overloader.enum=Type.enum;Overloader.Type=Type;Overloader.type=Type;Overloader.Param=Param;Overloader.param=Param.parse;Overloader.parseParam=Param.parse;Overloader.ParamList=ParamList;Overloader.paramList=ParamList.parse;Overloader.parseParamList=ParamList.parse;Overloader.Overload=Overload;Overloader.overload=Overload.parse;Overloader.parseOverload=Overload.parse;Overloader.Function=OverloadedFunction;Overloader.createFunction=OverloadedFunction;Overloader.absent=function(e){return new Absent(e)};for(var name in ERR){if(ERR.hasOwnProperty(name)){var Err=ERR[name];Overloader[Err.name]=Err}}var TYPE_ALIAS={"?":Type.ANY,any:Type.ANY,boolean:Type.BOOLEAN,char:Type.CHAR,number:Type.NUMBER,object:Type.PLAIN_OBJECT,scalar:Type.SCALAR,string:Type.STRING};if(typeof module=="object"&&typeof module.exports=="object"){module.exports=Overloader}else if(typeof global.define=="function"){global.define(function(){return Overloader})}else{global.overload2=Overloader}})(this);

@@ -565,2 +565,5 @@ /**

if (param.arrayed) newArgs.push(paramArgs);
// 此时,paramArgs.length == 1
// @tag 20180302b
else newArgs.push(paramArgs[0]);

@@ -593,6 +596,7 @@ };

var restParams = params.slice(paramCursor);
// 剩余形参数组,该数组在后续的让贤匹配中是不变的。
/*const*/ var restParams = params.slice(paramCursor);
// 抵达匹配边界时,若
// 仅匹配了最小数量的实参且该参数不可缺省,或者已是最后一个形式参数,
// 仅匹配了最小数量的实参且该参数不可缺省,或者已是最后一个形式参数(既所有形式参数均已匹配),
// 则直接固定参数值。

@@ -605,4 +609,13 @@ if (!param.absent && paramArgs.length == param.minSize || restParams.length == 0) {

// 否则,须尝试让贤。
// 让贤的策略是:
// 将剩余实参与剩余形参匹配(递归调用)。
// 如匹配不成功,则出让一个当前形参匹配的实参,列为剩余实参之首,再次尝试匹配。
// 直至,无法出让,或匹配成功。
// 剩余实参数组,该数组在后续的让贤匹配中是递增的。
var restArgs = args.slice(argCursor + 1);
// @tag 20180302a
// @fixbug
var lasttime = false;
// 步步让贤,直到让无可让。

@@ -618,2 +631,10 @@ do {

}
// 已是最后一次尝试(当前形参已出让所有实参)。
// @tag 20180302a
if (lasttime) {
// 就此罢了(liao)。
newArgs = null;
break;
}

@@ -625,3 +646,9 @@ // 如果已让无可让:

restArgs = paramArgs.concat(restArgs);
paramArgs = param.absentValue;
// paramArgs = param.absentValue;
// @tag 20180302b
paramArgs = param.arrayed ? param.absentValue : [ param.absentValue ];
// @tag 20180302a
lasttime = true;
continue;

@@ -628,0 +655,0 @@ }

{
"name": "overload2",
"version": "0.3.0",
"version": "0.3.1",
"description": "Elegant solution for function overloading in JavaScript",

@@ -5,0 +5,0 @@ "main": "overload2.js",

@@ -18,2 +18,5 @@ var MODULE_REQUIRE

function Foo() {};
var foo = new Foo();
describe('Param absent', function() {

@@ -39,2 +42,6 @@ var fn = overload2()

})
.overload(Foo, '? =null', 'number', function(foo, strings, number) {
return [ 7, Array.from(arguments) ];
})
;

@@ -67,2 +74,12 @@

});
it('bugs fixed on 20180302', function() {
try {
fn(foo, string, string);
} catch(ex) {
assert(ex instanceof overload2.UnmatchingException);
}
DE([ 7, [ foo, null, number ] ], fn(foo, number));
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc