Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

overload2

Package Overview
Dependencies
Maintainers
1
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.0.2 to 0.0.3

4

CHANGELOG.md

@@ -5,2 +5,6 @@ # Change Log

## [0.0.3] -2017-05
``overload2()`` will create a new overloaded function. Before, in v0.0.2, the overloaded function may be appended with an implementation meanwhile being created, if ``overload2()`` is called with arguments. This pattern is not only ambiguous, but also restrict the future expandability of *overload2* (arguments passed to ``overload2()`` SHOULD be used to define attributes concerned with overloading, instead of specific implementation). So, since this version, ``overload2()`` will return an overloaded function _WITHOUT ANY IMPLEMENTATION__. And programmers must implement it by calling ``.overload()`` method.
## [0.0.1] - 2017-04

@@ -7,0 +11,0 @@

2

dist/overload2.js

@@ -424,3 +424,3 @@ /**

if (arguments.length) {
append(arguments);
// append(arguments);
}

@@ -427,0 +427,0 @@

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

var MODULE_REQUIRE,declareException=function declareException(name,parent,constructorFunction){if(!parent)parent=Error;var Ex=function(message){this.name="OVERLOAD2."+name;if(constructorFunction){constructorFunction.apply(this,arguments)}else{this.message=message}var err=new parent;this.stack=[this.name+": "+this.message].concat(err.stack.split("\n").slice(2)).join("\n")};Ex.prototype=Object.create(parent.prototype);Ex.prototype.consturctor=Ex;Ex.prototype.toString=Ex.prototype.valueOf=function(){return"["+this.name+": "+this.message+"]"};Object.defineProperty(Ex,"name",{value:name});return Ex},ERR={Generic:declareException("Error"),Arguments:declareException("ArgumentsError",TypeError,function(expected,actual){this.message=expected+" argument(s) expected but actual "+actual}),Type:declareException("TypeError",TypeError,function(desc,types,actual){this.message=desc+" must be "+(typeof types=="string"?types:types.join(" | "))+": "+actual}),Range:declareException("RangeError",RangeError,function(desc,info,actual){if(info instanceof Array){info=info.join(" | ")}this.message=desc+" must be "+info+": "+actual}),NotImplemented:declareException("EmptyException"),Unmatching:declareException("UnmatchingException",TypeError)},generateCreator=function(C){function F(args){return C.apply(this,args)}F.prototype=C.prototype;return function(){return new F(arguments)}},has=function(arr,item){return arr.indexOf(item)>=0};function Type(matcher){if(arguments.length!=1){throw new ERR.Arguments(1,arguments.length)}if(!(typeof matcher=="function"||matcher instanceof RegExp)){throw new ERR.Type("Type matcher",["Function","RegExp"],matcher)}if(!(this instanceof Type))return new Type(matcher);if(matcher instanceof RegExp)this.match=function(value){return matcher.test(value)};else this.match=matcher}function Param(){if(arguments.length==0){throw new ERR.Arguments("1+",0)}if(arguments.length==1&&arguments[0]instanceof Param){return arguments[0]}var type=arguments[0],decos=[];if(typeof type=="string"){decos=type.trim().split(/\s+/);type=decos.shift();var formalType=TYPE_ALIAS[type];if(!formalType){throw new ERR.Range("type alias",Object.keys(TYPE_ALIAS),type)}type=formalType}else if(type instanceof Type){}else if(typeof type=="function"){type=function(type){return new Type(function(value){return value instanceof type})}(type)}else{throw new ERR.Type("Param type",["overload2.Type","string","Function"],type)}this.type=type;var DECOS=["NULL","UNDEFINED"];for(var i=1;i<arguments.length;i++){decos=decos.concat(arguments[i].trim().split(/\s+/))}for(var i=0;i<decos.length;i++){var rawDeco=decos[i];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")}Param.parse=generateCreator(Param);Param.prototype.satisfy=function(value){if(value===null&&this.nil)return true;if(value===undefined&&this.undef)return true;return this.type.match(value)};function ParamList(){var params=[];for(var i=0,args;i<arguments.length;i++){args=arguments[i]instanceof Array?arguments[i]:[arguments[i]];params[i]=Param.parse.apply(null,args)}this.length=params.length;this.item=function(i){return params[i]}}ParamList.parse=generateCreator(ParamList);ParamList.prototype.satisfy=function(args){if(args.length!=this.length){return false}var matched=true;for(var i=0;matched&&i<this.length;i++){matched=this.item(i).satisfy(args[i])}return matched};function Overload(){if(arguments.length==0){throw new ERR.Arguments("1+",0)}var args=Array.from(arguments);var method=args.pop();if(typeof method=="function"){this.method=method}else{throw new ERR.Generic("overloading implementation function missed")}if(typeof args[0]=="number"){if(args.length>1){throw new ERR.Arguments(2,args.length+1)}this.paramCount=args[0]}else if(args[0]instanceof ParamList){if(args.length>1){throw new ERR.Arguments(2,args.length+1)}this.params=args[0]}else{this.params=ParamList.parse.apply(null,args)}}Overload.parse=generateCreator(Overload);Overload.prototype.exec=function(scope,args){if(typeof this.paramCount=="number"&&args.length!=this.paramCount){return false}if(this.params&&!this.params.satisfy(args)){return false}return[this.method.apply(scope,args)]};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(scope,args){if(this._overloads.length==0){throw new ERR.NotImplemented}for(var i=0,ret;i<this._overloads.length;i++){ret=this._overloads[i].exec(scope,args);if(ret){return ret[0]}}if(this._defaultMethod){return this.defaultMethod.apply(scope,args)}throw new ERR.Unmatching("Unmatching arguments: "+args)};OverloadedFunction.prototype.call=function(scope){var args=Array.from(arguments);return this.apply(scope,args.slice(1))};OverloadedFunction.prototype.overload=function(){var overloadInstance;if(arguments[0]instanceof Overload){overloadInstance=arguments[0];if(arguments[1]){this._defaultMethod=overloadInstance.method}if(arguments.length>2){console.warn("redundant arguments found")}}else{overloadInstance=Overload.parse.apply(null,arguments)}this._overloads.push(overloadInstance);return this};OverloadedFunction.prototype.default=function(method){if(typeof method!="function"){throw new ERR.Generic("invalid default method")}if(arguments.length>1){console.warn("redundant arguments found")}this._defaultMethod=method};function Overloader(){var overloaded=new OverloadedFunction;var append=function(args){overloaded.overload.apply(overloaded,args)};var saveDEF=function(args,isDefault){var overloadInstance;if(args[0]instanceof Overload){overloadInstance=args[0]}else{overloadInstance=Overload.parse.apply(null,args)}overloaded.overload(overloadInstance);if(isDefault){overloaded.defaultMethod=overloadInstance.method}};if(arguments.length){append(arguments)}var fn=function(){return overloaded.apply(this,arguments)};fn.overload=function(){append(arguments);return fn};fn.default=function(method){overloaded.default(method);return fn};return fn}Overloader.ANY=new Type(function(value){return true});Overloader.BOOLEAN=new Type(function(value){return typeof value=="boolean"});Overloader.CHAR=new Type(function(value){return typeof value=="string"&&value.length==1});Overloader.NUMBER=new Type(function(value){return typeof value=="number"});Overloader.SCALAR=new Type(function(value){return["boolean","number","string"].indexOf(typeof value)>=0});Overloader.STRING=new Type(function(value){return typeof value=="string"});Overloader.enum=function(){var args=[];for(var i=0;i<arguments.length;i++){args.push(arguments[i])}return new Type(function(value){return args.indexOf(value)>=0})};Overloader.Type=Type;Overloader.type=Type;Overloader.parseType=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;for(var name in ERR){if(ERR.hasOwnProperty(name)){var Err=ERR[name];Overloader[Err.name]=Err}}var TYPE_ALIAS={"*":Overloader.ANY,any:Overloader.ANY,"boolean":Overloader.BOOLEAN,"char":Overloader.CHAR,number:Overloader.NUMBER,scalar:Overloader.SCALAR,string:Overloader.STRING};module.exports=Overloader;
var MODULE_REQUIRE,declareException=function declareException(name,parent,constructorFunction){if(!parent)parent=Error;var Ex=function(message){this.name="OVERLOAD2."+name;if(constructorFunction){constructorFunction.apply(this,arguments)}else{this.message=message}var err=new parent;this.stack=[this.name+": "+this.message].concat(err.stack.split("\n").slice(2)).join("\n")};Ex.prototype=Object.create(parent.prototype);Ex.prototype.consturctor=Ex;Ex.prototype.toString=Ex.prototype.valueOf=function(){return"["+this.name+": "+this.message+"]"};Object.defineProperty(Ex,"name",{value:name});return Ex},ERR={Generic:declareException("Error"),Arguments:declareException("ArgumentsError",TypeError,function(expected,actual){this.message=expected+" argument(s) expected but actual "+actual}),Type:declareException("TypeError",TypeError,function(desc,types,actual){this.message=desc+" must be "+(typeof types=="string"?types:types.join(" | "))+": "+actual}),Range:declareException("RangeError",RangeError,function(desc,info,actual){if(info instanceof Array){info=info.join(" | ")}this.message=desc+" must be "+info+": "+actual}),NotImplemented:declareException("EmptyException"),Unmatching:declareException("UnmatchingException",TypeError)},generateCreator=function(C){function F(args){return C.apply(this,args)}F.prototype=C.prototype;return function(){return new F(arguments)}},has=function(arr,item){return arr.indexOf(item)>=0};function Type(matcher){if(arguments.length!=1){throw new ERR.Arguments(1,arguments.length)}if(!(typeof matcher=="function"||matcher instanceof RegExp)){throw new ERR.Type("Type matcher",["Function","RegExp"],matcher)}if(!(this instanceof Type))return new Type(matcher);if(matcher instanceof RegExp)this.match=function(value){return matcher.test(value)};else this.match=matcher}function Param(){if(arguments.length==0){throw new ERR.Arguments("1+",0)}if(arguments.length==1&&arguments[0]instanceof Param){return arguments[0]}var type=arguments[0],decos=[];if(typeof type=="string"){decos=type.trim().split(/\s+/);type=decos.shift();var formalType=TYPE_ALIAS[type];if(!formalType){throw new ERR.Range("type alias",Object.keys(TYPE_ALIAS),type)}type=formalType}else if(type instanceof Type){}else if(typeof type=="function"){type=function(type){return new Type(function(value){return value instanceof type})}(type)}else{throw new ERR.Type("Param type",["overload2.Type","string","Function"],type)}this.type=type;var DECOS=["NULL","UNDEFINED"];for(var i=1;i<arguments.length;i++){decos=decos.concat(arguments[i].trim().split(/\s+/))}for(var i=0;i<decos.length;i++){var rawDeco=decos[i];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")}Param.parse=generateCreator(Param);Param.prototype.satisfy=function(value){if(value===null&&this.nil)return true;if(value===undefined&&this.undef)return true;return this.type.match(value)};function ParamList(){var params=[];for(var i=0,args;i<arguments.length;i++){args=arguments[i]instanceof Array?arguments[i]:[arguments[i]];params[i]=Param.parse.apply(null,args)}this.length=params.length;this.item=function(i){return params[i]}}ParamList.parse=generateCreator(ParamList);ParamList.prototype.satisfy=function(args){if(args.length!=this.length){return false}var matched=true;for(var i=0;matched&&i<this.length;i++){matched=this.item(i).satisfy(args[i])}return matched};function Overload(){if(arguments.length==0){throw new ERR.Arguments("1+",0)}var args=Array.from(arguments);var method=args.pop();if(typeof method=="function"){this.method=method}else{throw new ERR.Generic("overloading implementation function missed")}if(typeof args[0]=="number"){if(args.length>1){throw new ERR.Arguments(2,args.length+1)}this.paramCount=args[0]}else if(args[0]instanceof ParamList){if(args.length>1){throw new ERR.Arguments(2,args.length+1)}this.params=args[0]}else{this.params=ParamList.parse.apply(null,args)}}Overload.parse=generateCreator(Overload);Overload.prototype.exec=function(scope,args){if(typeof this.paramCount=="number"&&args.length!=this.paramCount){return false}if(this.params&&!this.params.satisfy(args)){return false}return[this.method.apply(scope,args)]};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(scope,args){if(this._overloads.length==0){throw new ERR.NotImplemented}for(var i=0,ret;i<this._overloads.length;i++){ret=this._overloads[i].exec(scope,args);if(ret){return ret[0]}}if(this._defaultMethod){return this.defaultMethod.apply(scope,args)}throw new ERR.Unmatching("Unmatching arguments: "+args)};OverloadedFunction.prototype.call=function(scope){var args=Array.from(arguments);return this.apply(scope,args.slice(1))};OverloadedFunction.prototype.overload=function(){var overloadInstance;if(arguments[0]instanceof Overload){overloadInstance=arguments[0];if(arguments[1]){this._defaultMethod=overloadInstance.method}if(arguments.length>2){console.warn("redundant arguments found")}}else{overloadInstance=Overload.parse.apply(null,arguments)}this._overloads.push(overloadInstance);return this};OverloadedFunction.prototype.default=function(method){if(typeof method!="function"){throw new ERR.Generic("invalid default method")}if(arguments.length>1){console.warn("redundant arguments found")}this._defaultMethod=method};function Overloader(){var overloaded=new OverloadedFunction;var append=function(args){overloaded.overload.apply(overloaded,args)};var saveDEF=function(args,isDefault){var overloadInstance;if(args[0]instanceof Overload){overloadInstance=args[0]}else{overloadInstance=Overload.parse.apply(null,args)}overloaded.overload(overloadInstance);if(isDefault){overloaded.defaultMethod=overloadInstance.method}};if(arguments.length){}var fn=function(){return overloaded.apply(this,arguments)};fn.overload=function(){append(arguments);return fn};fn.default=function(method){overloaded.default(method);return fn};return fn}Overloader.ANY=new Type(function(value){return true});Overloader.BOOLEAN=new Type(function(value){return typeof value=="boolean"});Overloader.CHAR=new Type(function(value){return typeof value=="string"&&value.length==1});Overloader.NUMBER=new Type(function(value){return typeof value=="number"});Overloader.SCALAR=new Type(function(value){return["boolean","number","string"].indexOf(typeof value)>=0});Overloader.STRING=new Type(function(value){return typeof value=="string"});Overloader.enum=function(){var args=[];for(var i=0;i<arguments.length;i++){args.push(arguments[i])}return new Type(function(value){return args.indexOf(value)>=0})};Overloader.Type=Type;Overloader.type=Type;Overloader.parseType=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;for(var name in ERR){if(ERR.hasOwnProperty(name)){var Err=ERR[name];Overloader[Err.name]=Err}}var TYPE_ALIAS={"*":Overloader.ANY,any:Overloader.ANY,"boolean":Overloader.BOOLEAN,"char":Overloader.CHAR,number:Overloader.NUMBER,scalar:Overloader.SCALAR,string:Overloader.STRING};module.exports=Overloader;
//# sourceMappingURL=overload2.map

@@ -424,3 +424,3 @@ /**

if (arguments.length) {
append(arguments);
// append(arguments);
}

@@ -427,0 +427,0 @@

{
"name": "overload2",
"version": "0.0.2",
"version": "0.0.3",
"description": "Elegant solution for function overloading in JavaScript",

@@ -19,3 +19,4 @@ "main": "overload2.js",

"overload",
"overloading",
"function-overloading",
"constructor-overloading",
"strongly-typed"

@@ -26,5 +27,5 @@ ],

"bugs": {
"url": "https://github.com/YounGoat/javascript.overload2/issues"
"url": "https://github.com/YounGoat/ecmascript.overload2/issues"
},
"homepage": "https://github.com/YounGoat/javascript.overload2#readme"
"homepage": "https://github.com/YounGoat/ecmascript.overload2#readme"
}
# overload2
__Elegant solution for function overload in JavaScript.__
__Elegant solution for function overloading in JavaScript.__

@@ -9,7 +9,7 @@ [![NPM](https://nodei.co/npm/overload2.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/overload2)

On programming with strongly-typed language, such as C++ and Java, [function overloading](https://en.wikipedia.org/wiki/Function_overloading) is frequently employed to make API more convenient to be used. As a weakly-typed language, JavaScript does not support function overloading. At the same time, fortunately, functions in JavaScript may be passed with any arguments that is why *overload2* is feasible.
On programming with strongly-typed languages such as C++ and Java, [function overloading](https://en.wikipedia.org/wiki/Function_overloading) is frequently employed to make API more convenient to be used. As a weakly-typed language, JavaScript does not support function overloading. At the same time, fortunately, functions in JavaScript may be passed with any arguments that is why *overload2* is feasible.
## Table of contents
* [Get Start](#get-started)
* [Get Started](#get-started)
* [Datatypes](#datatypes)

@@ -21,3 +21,3 @@ * [Move Forward](#move-forward)

See [CHANGE LOG](./CHANGELOG.md) for notable changes. Or access project's [homepage](./readme.md) for latest updates.
See [CHANGE LOG](./CHANGELOG.md) for notable changes. Or access project's [homepage](./README.md) for latest updates.

@@ -165,9 +165,9 @@ ## Get Started

* \<fn\> __overload2( [ \<datatype\>, ... ] function \<implementation\> )__
Create a new overloaded function.
* \<fn\> __overload2__()
Create a new overloaded function. The function has no implementations before ``.overload()`` called.
* \<fn\> __\<fn\>.overload( [ \<datatype\>, ... ] function \<implementation\> )__
* \<fn\> __\<fn\>.overload__( [ \<datatype\>, ... ] function \<implementation\> )
Append an overloading implementation to existing overloaded function.
* \<fn\> __\<fn\>.default( function \<implementation\> )__
* \<fn\> __\<fn\>.default__( function \<implementation\> )
Set default implementation function for existing overloaded function.

@@ -179,6 +179,6 @@

* __new overload2.Type( function | RegExp \<matcher\> )__
* new __overload2.Type__( function | RegExp \<matcher\> )
Here ``matcher`` may be a function or RegExp object.
* *private* boolean __\<type\>.match( \<value\> )__
* *private* boolean __\<type\>.match__( \<value\> )
Return ``true`` if value matches the datatype, otherwise return ``false``.

@@ -195,12 +195,12 @@

* __new overload2.Param( string "\<alias\> \<decorator\> ..." )__
* new __overload2.Param__( string "\<alias\> \<decorator\> ..." )
The ``alias`` should be one of alias listed in table [Datatype Alias](#datatype-alias).
* __new overload2.Param( Type | function | string \<datatype\>, string \<decorator(s)\> [ , string \<decorator(s)\> ] )__
* new __overload2.Param__( Type | function | string \<datatype\>, string \<decorator(s)\> [ , string \<decorator(s)\> ] )
Here ``datatype`` may be instance of ``Type``, or construtor function, or datatype alias.
* *private* boolean __\<param\>.satisfy( \<value\> )__
* *private* boolean __\<param\>.satisfy__( \<value\> )
To judge if the argument value satisfy the parameter.
* \<Param\> __overload2.Param.parse( ? )__
* Param __overload2.Param.parse__( ? )
Arguments suitable for ``new Param()`` are also suitable for the ``Param.parse()``.

@@ -210,20 +210,20 @@

* __new overload2.ParamList( [ Param | Array | String \<param\> [ , ... ] ] )__
* new __overload2.ParamList__( [ Param | Array | String \<param\> [ , ... ] ] )
Here ``param`` may be an instance of ``Param``, or a string or an array which may used as argument(s) for ``new Param()``.
* *private* boolean __\<paramList\>.satisfy( Array | Arguments \<args\> )__
* *private* boolean __\<paramList\>.satisfy__( Array | Arguments \<args\> )
To check arguments with parameters, return ``true`` if matched or ``false`` if not.
* \<ParamList\> __overload2.ParamList.parse( ? )__
* ParamList __overload2.ParamList.parse__( ? )
Arguments suitable for ``new ParamList()`` are also suitable for the ``ParamList.parse()``.
### overload2.Overload
* __new overload2.Overload( number <argumentsNumber>, function \<implementation\> )__
* new __overload2.Overload__( number <argumentsNumber>, function \<implementation\> )
* __new overload2.Overload( ParamList, function \<implementation\> )__
* new __overload2.Overload__( ParamList, function \<implementation\> )
* __new overload2.Overload( [ \<param\>, [ ..., ] ] function \<implementation\> )__
* new __overload2.Overload__( [ \<param\>, [ ..., ] ] function \<implementation\> )
* \<Overload\> __overload2.Overload.parse( ? )__
* Overload __overload2.Overload.parse__( ? )
Arguments suitable for ``new Overload()`` are also suitable for the ``Overload.parse()``.

@@ -233,18 +233,18 @@

* __new overload2.OverloadedFunction()__
* new __overload2.OverloadedFunction__()
The instance of ``OverloadedFunction`` is a wrapper, not a function itself.
* __\<wrapper\>.exec( ... )__
* __\<wrapper\>.exec__( ... )
Run the overloaded function.
* __\<wrapper\>.apply( \<scope\>, Array | Arguments \<args\> )__
* __\<wrapper\>.apply__( \<scope\>, Array | Arguments \<args\> )
Run the overloaded function under specified scope, passing arguments as an array or Arguments instance.
* __\<wrapper\>.call( \<scope\> [ , \<arg\> [ , ... ] ] )__
* __\<wrapper\>.call__( \<scope\> [ , \<arg\> [ , ... ] ] )
Run the overloaded function under specified scope, passing arguments one by one.
* __\<wrapper\>.overload( Overload \<overloadInstance\> )__
* __\<wrapper\>.overload__( Overload \<overloadInstance\> )
Append an overloading implementation.
* __\<wrapper\>.overload( ? )__
* __\<wrapper\>.overload__( ? )
Append an overloading implementation, arguments suitable for ``new Overload()`` are also suitable for the ``<wrapper>.overload()``.

@@ -270,2 +270,2 @@

To be continued.
To be continued.

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