typed-function
Advanced tools
Comparing version 0.6.1 to 0.6.2
# History | ||
## 2015-02-26, version 0.6.2 | ||
- Fixed a bug sometimes not ordering the handling of any type arguments last. | ||
- Fixed a bug sometimes not choosing the signature with the lowest number of | ||
conversions. | ||
## 2015-02-07, version 0.6.1 | ||
@@ -4,0 +11,0 @@ |
{ | ||
"name": "typed-function", | ||
"version": "0.6.1", | ||
"version": "0.6.2", | ||
"description": "Type checking for JavaScript functions", | ||
@@ -5,0 +5,0 @@ "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)", |
@@ -221,2 +221,60 @@ var assert = require('assert'); | ||
it('should insert conversions when having an any type argument', function() { | ||
var fn = typed({ | ||
'number': function (a) { | ||
return 'number'; | ||
}, | ||
'any': function (a) { | ||
return 'any'; | ||
} | ||
}); | ||
// booleans should be converted to number | ||
assert.equal(fn(2), 'number'); | ||
assert.equal(fn(true), 'number'); | ||
assert.equal(fn('foo'), 'any'); | ||
assert.equal(fn('{}'), 'any'); | ||
}); | ||
describe ('ordering', function () { | ||
it('should correctly select the signatures with the least amount of conversions', function () { | ||
typed.conversions = [ | ||
{from: 'number', to: 'string', convert: function (x) {return x + '';}}, | ||
{from: 'boolean', to: 'string', convert: function (x) {return x + '';}}, | ||
{from: 'boolean', to: 'number', convert: function (x) {return +x;}} | ||
]; | ||
var fn = typed({ | ||
'boolean, boolean': function (a, b) { | ||
assert.equal(typeof a, 'boolean'); | ||
assert.equal(typeof b, 'boolean'); | ||
return 'booleans'; | ||
}, | ||
'number, number': function (a, b) { | ||
assert.equal(typeof a, 'number'); | ||
assert.equal(typeof b, 'number'); | ||
return 'numbers'; | ||
}, | ||
'string, string': function (a, b) { | ||
assert.equal(typeof a, 'string'); | ||
assert.equal(typeof b, 'string'); | ||
return 'strings'; | ||
} | ||
}); | ||
assert.equal(fn(true, true), 'booleans'); | ||
assert.equal(fn(2, true), 'numbers'); | ||
assert.equal(fn(true, 2), 'numbers'); | ||
assert.equal(fn(2, 2), 'numbers'); | ||
assert.equal(fn('foo', 'bar'), 'strings'); | ||
assert.equal(fn('foo', 2), 'strings'); | ||
assert.equal(fn(2, 'foo'), 'strings'); | ||
assert.equal(fn(true, 'foo'), 'strings'); | ||
assert.equal(fn('foo', true), 'strings'); | ||
}); | ||
}) | ||
}); |
@@ -207,3 +207,3 @@ /** | ||
if (a.anyType) return 1; | ||
if (a.anyType) return -1; | ||
if (b.anyType) return -1; | ||
@@ -402,2 +402,14 @@ if (contains(a.types, 'Object')) return 1; | ||
// count the number of conversions | ||
var ac = a.params.filter(function (param) { | ||
return param.hasConversions(); | ||
}); | ||
var bc = b.params.filter(function (param) { | ||
return param.hasConversions(); | ||
}); | ||
if (ac.length > bc.length) return 1; | ||
if (ac.length < bc.length) return -1; | ||
// compare the conversion index per parameter | ||
for (var i = 0; i < a.params.length; i++) { | ||
@@ -404,0 +416,0 @@ var cmp = Param.compare(a.params[i], b.params[i]); |
@@ -1,1 +0,1 @@ | ||
!function(n){"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():window.typed=n()}(function(){"use strict";function getTypeTest(n){var r=typed.types[n];if(!r){var t=Object.keys(typed.types).filter(function(r){return r.toLowerCase()==n.toLowerCase()}).map(function(n){return'"'+n+'"'});throw new Error('Unknown type "'+n+'"'+(t.length?". Did you mean "+t.join(", or ")+"?":""))}return r}function createError(n,r,t,e){var s,i=r[t],a=getTypeOf(i),o=e?e.split(","):null,u=o&&contains(o,"any"),p={fn:n,index:t,actual:i,expected:o};s=o?r.length>t&&!u?"Unexpected type of argument (expected: "+o.join(" or ")+", actual: "+a+", index: "+t+")":"Too few arguments (expected: "+o.join(" or ")+", index: "+t+")":"Too many arguments (expected: "+t+", actual: "+r.length+")";var c=new TypeError(s);return c.data=p,c}function Refs(n){this.name=n||"refs",this.categories={}}function Param(n,r){if("string"==typeof n){var t=n.trim(),e="..."===t.substr(0,3);e&&(t=t.substr(3)),this.types=""===t?["any"]:t.split("|").map(function(n){return n.trim()})}else{if(!Array.isArray(n)){if(n instanceof Param)return n.clone();throw new Error("String or Array expected")}this.types=n}this.conversions=[],this.varArgs=e||r||!1,this.anyType=this.types.some(function(n){return"any"==n})}function Signature(n,r){if("string"==typeof n)this.params=""!==n?n.split(",").map(function(n){return new Param(n)}):[];else{if(!Array.isArray(n))throw new Error("string or Array expected");this.params=n.map(function(n){return new Param(n)})}var t=this.params.filter(function(n){return n.varArgs});if(0===t.length)this.varArgs=!1;else{if(t[0]!==this.params[this.params.length-1])throw new SyntaxError('Unexpected variable arguments operator "..."');this.varArgs=!0}this.fn=r}function Node(n,r,t){this.path=n||[],this.param=n[n.length-1]||null,this.signature=r||null,this.childs=t||[]}function parseSignatures(n){var r=Object.keys(n).reduce(function(r,t){var e=n[t],s=new Signature(t,e);return s.expand().forEach(function(n){var t=n.toString();if(void 0===r[t])r[t]=n;else{var e=Signature.compare(n,r[t]);if(0>e)r[t]=n;else if(0===e)throw new Error('Signature "'+t+'" is defined twice')}}),r},{}),t=Object.keys(r).map(function(n){return r[n]});return t.forEach(function(n){if(n.varArgs)for(var r=n.params.length-1,e=n.params[r],s=0;s<e.types.length;){if(e.conversions[s]){var i=e.types[s],a=t.some(function(t){var e=t.params[r];return t!==n&&e&&contains(e.types,i)&&!e.conversions[r]});a&&(e.types.splice(s,1),e.conversions.splice(s,1),s--)}s++}}),t}function mapSignatures(n){return n.reduce(function(n,r){var t=r.params.join(",");return r.fn&&(n[t]=r.fn),n},{})}function parseTree(n,r){var t=r.length,e=n.filter(function(n){return n.params.length===t})[0],s=n.filter(function(n){return void 0!=n.params[t]}).sort(function(n,r){return Param.compare(n.params[t],r.params[t])}).reduce(function(n,r){var e=r.params[t],s=n.filter(function(n){return n.param.overlapping(e)})[0];if(s){if(s.param.varArgs)throw new Error('Conflicting types "'+s.param+'" and "'+e+'"');s.signatures.push(r)}else n.push({param:e,signatures:[r]});return n},[]).map(function(n){return parseTree(n.signatures,r.concat(n.param))});return new Node(r,e,s)}function getArgs(n){for(var r=[],t=0;n>t;t++)r[t]="arg"+t;return r}function _typed(name,signatures){var refs=new Refs,_signatures=parseSignatures(signatures);if(0==_signatures.length)throw new Error("No signatures provided");var node=parseTree(_signatures,[]),code=[],_name=name||"",_args=getArgs(maxParams(_signatures));code.push("function "+_name+"("+_args.join(", ")+") {"),code.push(node.toCode(refs," ")),code.push("}");var factory=["(function ("+refs.name+") {",refs.toCode(),"return "+code.join("\n"),"})"].join("\n"),fn=eval(factory)(refs);return fn.signatures=mapSignatures(_signatures),fn}function maxParams(n){return n.reduce(function(n,r){return Math.max(n,r.params.length)},0)}function getTypeOf(n){for(var r in types)if(types.hasOwnProperty(r)&&"Object"!==r&&types[r](n))return r;return types.Object(n)?r:"unknown"}function contains(n,r){return-1!==n.indexOf(r)}Refs.prototype.add=function(n,r){var t=r||"fn";this.categories[t]||(this.categories[t]=[]);var e=this.categories[t].indexOf(n);return-1==e&&(e=this.categories[t].length,this.categories[t].push(n)),t+e},Refs.prototype.toCode=function(){var n=[],r=this.name+".categories",t=this.categories;return Object.keys(t).forEach(function(e){t[e].forEach(function(t,s){n.push("var "+e+s+" = "+r+"['"+e+"']["+s+"];")})}),n.join("\n")},Param.compare=function(n,r){if(n.anyType)return 1;if(n.anyType)return-1;if(contains(n.types,"Object"))return 1;if(contains(r.types,"Object"))return-1;if(n.hasConversions()){if(r.hasConversions()){var t=n.conversions.filter(function(n){return void 0!==n})[0],e=r.conversions.filter(function(n){return void 0!==n})[0];return typed.conversions.indexOf(t)-typed.conversions.indexOf(e)}return 1}return r.hasConversions()?-1:0},Param.prototype.overlapping=function(n){return this.types.some(function(r){return contains(n.types,r)})},Param.prototype.clone=function(){var n=new Param(this.types.slice(),this.varArgs);return n.conversions=this.conversions.slice(),n},Param.prototype.hasConversions=function(){return this.conversions.length>0},Param.prototype.toString=function(n){var r=this.types.map(function(r,t){var e=this.conversions[t];return n&&e?e.to:r}.bind(this)).filter(function(n,r,t){return t.indexOf(n)===r});return(this.varArgs?"...":"")+r.join("|")},Signature.prototype.clone=function(){return new Signature(this.params.slice(),this.fn)},Signature.prototype.expand=function(){function n(t,e){if(e.length<t.params.length){var s=t.params[e.length];if(s.varArgs){var i=s.clone();typed.conversions.forEach(function(n){if(!contains(s.types,n.from)&&contains(s.types,n.to)){var r=i.types.length;i.types[r]=n.from,i.conversions[r]=n}}),n(t,e.concat(i))}else s.types.forEach(function(r){n(t,e.concat(new Param(r)))}),typed.conversions.forEach(function(r){if(!contains(s.types,r.from)&&contains(s.types,r.to)){var i=new Param(r.from);i.conversions[0]=r,n(t,e.concat(i))}})}else r.push(new Signature(e,t.fn))}var r=[];return n(this,[]),r},Signature.compare=function(n,r){if(n.params.length>r.params.length)return 1;if(n.params.length<r.params.length)return-1;for(var t=0;t<n.params.length;t++){var e=Param.compare(n.params[t],r.params[t]);if(0!==e)return e}return 0},Signature.prototype.hasConversions=function(){return this.params.some(function(n){return n.hasConversions()})},Signature.prototype.toCode=function(n,r){var t=[],e=this.params.map(function(r,t){var e=r.conversions[0];return r.varArgs?"varArgs":e?n.add(e.convert,"convert")+"(arg"+t+")":"arg"+t}).join(", "),s=this.fn?n.add(this.fn,"signature"):void 0;return s?r+"return "+s+"("+e+"); // signature: "+this.params.join(", "):t.join("\n")},Signature.prototype.toString=function(){return this.params.join(", ")},Node.prototype.toCode=function(n,r,t){var e=[];if(this.param){var s=this.path.length-1,i=this.param.conversions[0],a="// type: "+(i?i.from+" (convert to "+i.to+")":this.param);if(this.param.varArgs)if(this.param.anyType)e.push(r+"if (arguments.length > "+s+") {"),e.push(r+" var varArgs = [];"),e.push(r+" for (var i = "+s+"; i < arguments.length; i++) {"),e.push(r+" varArgs.push(arguments[i]);"),e.push(r+" }"),e.push(this.signature.toCode(n,r+" ")),e.push(r+"}");else{var o=function(r,t){return r.map(function(r){return n.add(getTypeTest(r),"test")+"("+t+")"}).join(" || ")}.bind(this),u=this.param.types,p=u.filter(function(n,r){return void 0===this.param.conversions[r]}.bind(this));e.push(r+"if ("+o(u,"arg"+s)+") { "+a),e.push(r+" var varArgs = [arg"+s+"];"),e.push(r+" for (var i = "+(s+1)+"; i < arguments.length; i++) {"),e.push(r+" if ("+o(p,"arguments[i]")+") {"),e.push(r+" varArgs.push(arguments[i]);"),u.forEach(function(t,s){var i=this.param.conversions[s];if(i){var a=n.add(getTypeTest(t),"test"),o=n.add(i.convert,"convert");e.push(r+" }"),e.push(r+" else if ("+a+"(arguments[i])) {"),e.push(r+" varArgs.push("+o+"(arguments[i]));")}}.bind(this)),e.push(r+" } else {"),e.push(r+" throw createError('', arguments, i, '"+u.join(",")+"');"),e.push(r+" }"),e.push(r+" }"),e.push(this.signature.toCode(n,r+" ")),e.push(r+"}")}else if(this.param.anyType)e.push(r+"// type: any"),e.push(this._innerCode(n,r,t));else{var c=this.param.types[0],f="any"!==c?n.add(getTypeTest(c),"test"):null;e.push(r+"if ("+f+"(arg"+s+")) { "+a),e.push(this._innerCode(n,r+" ",t)),e.push(r+"}")}}else e.push(this._innerCode(n,r,t));return e.join("\n")},Node.prototype._innerCode=function(n,r,t){var e=[];this.signature&&(e.push(r+"if (arguments.length === "+this.path.length+") {"),e.push(this.signature.toCode(n,r+" ")),e.push(r+"}"));var s=this.childs.filter(function(n){return n.param.anyType})[0];this.childs.forEach(function(t){e.push(t.toCode(n,r,s))}),t&&!this.param.anyType&&e.push(t.toCode(n,r,s));var i=this._exceptions(n,r);return i&&e.push(i),e.join("\n")},Node.prototype._exceptions=function(n,r){var t=this.path.length;if(0===this.childs.length)return[r+"if (arguments.length > "+t+") {",r+" throw createError('', arguments, "+t+")",r+"}"].join("\n");var e=this.childs.reduce(function(n,r){return r.param&&r.param.types.forEach(function(r){-1===n.indexOf(r)&&n.push(r)}),n},[]);return r+"throw createError('', arguments, "+t+", '"+e.join(",")+"');"};var types={"null":function(n){return null===n},undefined:function(n){return void 0===n},"boolean":function(n){return"boolean"==typeof n},number:function(n){return"number"==typeof n},string:function(n){return"string"==typeof n},"function":function(n){return"function"==typeof n},Array:function(n){return Array.isArray(n)},Date:function(n){return n instanceof Date},RegExp:function(n){return n instanceof RegExp},Object:function(n){return"object"==typeof n}},config={},conversions=[],typed={config:config,types:types,conversions:conversions};return typed=_typed("typed",{Object:function(n){return _typed(null,n)},"string, Object":_typed,"string, function":function(n,r){var t={};return t[n]=r,_typed(r.name||null,t)},"string, string, function":function(n,r,t){var e={};return e[r]=t,_typed(n,e)},"...function":function(n){var r="",t={};return n.forEach(function(n,e){var s;if("object"!=typeof n.signatures)throw s=new TypeError("Function is no typed-function (index: "+e+")"),s.data={index:e},s;for(var i in n.signatures)if(n.signatures.hasOwnProperty(i)){if(t.hasOwnProperty(i))throw s=new Error('Signature "'+i+'" is defined twice'),s.data={signature:i},s;t[i]=n.signatures[i]}if(""!=n.name)if(""==r)r=n.name;else if(r!=n.name)throw s=new Error("Function names do not match (expected: "+r+", actual: "+n.name+")"),s.data={actual:n.name,expected:r},s}),_typed(r,t)}}),typed.config=config,typed.types=types,typed.conversions=conversions,typed}); | ||
!function(n){"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():window.typed=n()}(function(){"use strict";function getTypeTest(n){var r=typed.types[n];if(!r){var t=Object.keys(typed.types).filter(function(r){return r.toLowerCase()==n.toLowerCase()}).map(function(n){return'"'+n+'"'});throw new Error('Unknown type "'+n+'"'+(t.length?". Did you mean "+t.join(", or ")+"?":""))}return r}function createError(n,r,t,e){var s,i=r[t],a=getTypeOf(i),o=e?e.split(","):null,u=o&&contains(o,"any"),p={fn:n,index:t,actual:i,expected:o};s=o?r.length>t&&!u?"Unexpected type of argument (expected: "+o.join(" or ")+", actual: "+a+", index: "+t+")":"Too few arguments (expected: "+o.join(" or ")+", index: "+t+")":"Too many arguments (expected: "+t+", actual: "+r.length+")";var c=new TypeError(s);return c.data=p,c}function Refs(n){this.name=n||"refs",this.categories={}}function Param(n,r){if("string"==typeof n){var t=n.trim(),e="..."===t.substr(0,3);e&&(t=t.substr(3)),this.types=""===t?["any"]:t.split("|").map(function(n){return n.trim()})}else{if(!Array.isArray(n)){if(n instanceof Param)return n.clone();throw new Error("String or Array expected")}this.types=n}this.conversions=[],this.varArgs=e||r||!1,this.anyType=this.types.some(function(n){return"any"==n})}function Signature(n,r){if("string"==typeof n)this.params=""!==n?n.split(",").map(function(n){return new Param(n)}):[];else{if(!Array.isArray(n))throw new Error("string or Array expected");this.params=n.map(function(n){return new Param(n)})}var t=this.params.filter(function(n){return n.varArgs});if(0===t.length)this.varArgs=!1;else{if(t[0]!==this.params[this.params.length-1])throw new SyntaxError('Unexpected variable arguments operator "..."');this.varArgs=!0}this.fn=r}function Node(n,r,t){this.path=n||[],this.param=n[n.length-1]||null,this.signature=r||null,this.childs=t||[]}function parseSignatures(n){var r=Object.keys(n).reduce(function(r,t){var e=n[t],s=new Signature(t,e);return s.expand().forEach(function(n){var t=n.toString();if(void 0===r[t])r[t]=n;else{var e=Signature.compare(n,r[t]);if(0>e)r[t]=n;else if(0===e)throw new Error('Signature "'+t+'" is defined twice')}}),r},{}),t=Object.keys(r).map(function(n){return r[n]});return t.forEach(function(n){if(n.varArgs)for(var r=n.params.length-1,e=n.params[r],s=0;s<e.types.length;){if(e.conversions[s]){var i=e.types[s],a=t.some(function(t){var e=t.params[r];return t!==n&&e&&contains(e.types,i)&&!e.conversions[r]});a&&(e.types.splice(s,1),e.conversions.splice(s,1),s--)}s++}}),t}function mapSignatures(n){return n.reduce(function(n,r){var t=r.params.join(",");return r.fn&&(n[t]=r.fn),n},{})}function parseTree(n,r){var t=r.length,e=n.filter(function(n){return n.params.length===t})[0],s=n.filter(function(n){return void 0!=n.params[t]}).sort(function(n,r){return Param.compare(n.params[t],r.params[t])}).reduce(function(n,r){var e=r.params[t],s=n.filter(function(n){return n.param.overlapping(e)})[0];if(s){if(s.param.varArgs)throw new Error('Conflicting types "'+s.param+'" and "'+e+'"');s.signatures.push(r)}else n.push({param:e,signatures:[r]});return n},[]).map(function(n){return parseTree(n.signatures,r.concat(n.param))});return new Node(r,e,s)}function getArgs(n){for(var r=[],t=0;n>t;t++)r[t]="arg"+t;return r}function _typed(name,signatures){var refs=new Refs,_signatures=parseSignatures(signatures);if(0==_signatures.length)throw new Error("No signatures provided");var node=parseTree(_signatures,[]),code=[],_name=name||"",_args=getArgs(maxParams(_signatures));code.push("function "+_name+"("+_args.join(", ")+") {"),code.push(node.toCode(refs," ")),code.push("}");var factory=["(function ("+refs.name+") {",refs.toCode(),"return "+code.join("\n"),"})"].join("\n"),fn=eval(factory)(refs);return fn.signatures=mapSignatures(_signatures),fn}function maxParams(n){return n.reduce(function(n,r){return Math.max(n,r.params.length)},0)}function getTypeOf(n){for(var r in types)if(types.hasOwnProperty(r)&&"Object"!==r&&types[r](n))return r;return types.Object(n)?r:"unknown"}function contains(n,r){return-1!==n.indexOf(r)}Refs.prototype.add=function(n,r){var t=r||"fn";this.categories[t]||(this.categories[t]=[]);var e=this.categories[t].indexOf(n);return-1==e&&(e=this.categories[t].length,this.categories[t].push(n)),t+e},Refs.prototype.toCode=function(){var n=[],r=this.name+".categories",t=this.categories;return Object.keys(t).forEach(function(e){t[e].forEach(function(t,s){n.push("var "+e+s+" = "+r+"['"+e+"']["+s+"];")})}),n.join("\n")},Param.compare=function(n,r){if(n.anyType)return 1;if(r.anyType)return-1;if(contains(n.types,"Object"))return 1;if(contains(r.types,"Object"))return-1;if(n.hasConversions()){if(r.hasConversions()){var t=n.conversions.filter(function(n){return void 0!==n})[0],e=r.conversions.filter(function(n){return void 0!==n})[0];return typed.conversions.indexOf(t)-typed.conversions.indexOf(e)}return 1}return r.hasConversions()?-1:0},Param.prototype.overlapping=function(n){return this.types.some(function(r){return contains(n.types,r)})},Param.prototype.clone=function(){var n=new Param(this.types.slice(),this.varArgs);return n.conversions=this.conversions.slice(),n},Param.prototype.hasConversions=function(){return this.conversions.length>0},Param.prototype.toString=function(n){var r=this.types.map(function(r,t){var e=this.conversions[t];return n&&e?e.to:r}.bind(this)).filter(function(n,r,t){return t.indexOf(n)===r});return(this.varArgs?"...":"")+r.join("|")},Signature.prototype.clone=function(){return new Signature(this.params.slice(),this.fn)},Signature.prototype.expand=function(){function n(t,e){if(e.length<t.params.length){var s=t.params[e.length];if(s.varArgs){var i=s.clone();typed.conversions.forEach(function(n){if(!contains(s.types,n.from)&&contains(s.types,n.to)){var r=i.types.length;i.types[r]=n.from,i.conversions[r]=n}}),n(t,e.concat(i))}else s.types.forEach(function(r){n(t,e.concat(new Param(r)))}),typed.conversions.forEach(function(r){if(!contains(s.types,r.from)&&contains(s.types,r.to)){var i=new Param(r.from);i.conversions[0]=r,n(t,e.concat(i))}})}else r.push(new Signature(e,t.fn))}var r=[];return n(this,[]),r},Signature.compare=function(n,r){if(n.params.length>r.params.length)return 1;if(n.params.length<r.params.length)return-1;var t=n.params.filter(function(n){return n.hasConversions()}),e=r.params.filter(function(n){return n.hasConversions()});if(t.length>e.length)return 1;if(t.length<e.length)return-1;for(var s=0;s<n.params.length;s++){var i=Param.compare(n.params[s],r.params[s]);if(0!==i)return i}return 0},Signature.prototype.hasConversions=function(){return this.params.some(function(n){return n.hasConversions()})},Signature.prototype.toCode=function(n,r){var t=[],e=this.params.map(function(r,t){var e=r.conversions[0];return r.varArgs?"varArgs":e?n.add(e.convert,"convert")+"(arg"+t+")":"arg"+t}).join(", "),s=this.fn?n.add(this.fn,"signature"):void 0;return s?r+"return "+s+"("+e+"); // signature: "+this.params.join(", "):t.join("\n")},Signature.prototype.toString=function(){return this.params.join(", ")},Node.prototype.toCode=function(n,r,t){var e=[];if(this.param){var s=this.path.length-1,i=this.param.conversions[0],a="// type: "+(i?i.from+" (convert to "+i.to+")":this.param);if(this.param.varArgs)if(this.param.anyType)e.push(r+"if (arguments.length > "+s+") {"),e.push(r+" var varArgs = [];"),e.push(r+" for (var i = "+s+"; i < arguments.length; i++) {"),e.push(r+" varArgs.push(arguments[i]);"),e.push(r+" }"),e.push(this.signature.toCode(n,r+" ")),e.push(r+"}");else{var o=function(r,t){return r.map(function(r){return n.add(getTypeTest(r),"test")+"("+t+")"}).join(" || ")}.bind(this),u=this.param.types,p=u.filter(function(n,r){return void 0===this.param.conversions[r]}.bind(this));e.push(r+"if ("+o(u,"arg"+s)+") { "+a),e.push(r+" var varArgs = [arg"+s+"];"),e.push(r+" for (var i = "+(s+1)+"; i < arguments.length; i++) {"),e.push(r+" if ("+o(p,"arguments[i]")+") {"),e.push(r+" varArgs.push(arguments[i]);"),u.forEach(function(t,s){var i=this.param.conversions[s];if(i){var a=n.add(getTypeTest(t),"test"),o=n.add(i.convert,"convert");e.push(r+" }"),e.push(r+" else if ("+a+"(arguments[i])) {"),e.push(r+" varArgs.push("+o+"(arguments[i]));")}}.bind(this)),e.push(r+" } else {"),e.push(r+" throw createError('', arguments, i, '"+u.join(",")+"');"),e.push(r+" }"),e.push(r+" }"),e.push(this.signature.toCode(n,r+" ")),e.push(r+"}")}else if(this.param.anyType)e.push(r+"// type: any"),e.push(this._innerCode(n,r,t));else{var c=this.param.types[0],f="any"!==c?n.add(getTypeTest(c),"test"):null;e.push(r+"if ("+f+"(arg"+s+")) { "+a),e.push(this._innerCode(n,r+" ",t)),e.push(r+"}")}}else e.push(this._innerCode(n,r,t));return e.join("\n")},Node.prototype._innerCode=function(n,r,t){var e=[];this.signature&&(e.push(r+"if (arguments.length === "+this.path.length+") {"),e.push(this.signature.toCode(n,r+" ")),e.push(r+"}"));var s=this.childs.filter(function(n){return n.param.anyType})[0];this.childs.forEach(function(t){e.push(t.toCode(n,r,s))}),t&&!this.param.anyType&&e.push(t.toCode(n,r,s));var i=this._exceptions(n,r);return i&&e.push(i),e.join("\n")},Node.prototype._exceptions=function(n,r){var t=this.path.length;if(0===this.childs.length)return[r+"if (arguments.length > "+t+") {",r+" throw createError('', arguments, "+t+")",r+"}"].join("\n");var e=this.childs.reduce(function(n,r){return r.param&&r.param.types.forEach(function(r){-1===n.indexOf(r)&&n.push(r)}),n},[]);return r+"throw createError('', arguments, "+t+", '"+e.join(",")+"');"};var types={"null":function(n){return null===n},undefined:function(n){return void 0===n},"boolean":function(n){return"boolean"==typeof n},number:function(n){return"number"==typeof n},string:function(n){return"string"==typeof n},"function":function(n){return"function"==typeof n},Array:function(n){return Array.isArray(n)},Date:function(n){return n instanceof Date},RegExp:function(n){return n instanceof RegExp},Object:function(n){return"object"==typeof n}},config={},conversions=[],typed={config:config,types:types,conversions:conversions};return typed=_typed("typed",{Object:function(n){return _typed(null,n)},"string, Object":_typed,"string, function":function(n,r){var t={};return t[n]=r,_typed(r.name||null,t)},"string, string, function":function(n,r,t){var e={};return e[r]=t,_typed(n,e)},"...function":function(n){var r="",t={};return n.forEach(function(n,e){var s;if("object"!=typeof n.signatures)throw s=new TypeError("Function is no typed-function (index: "+e+")"),s.data={index:e},s;for(var i in n.signatures)if(n.signatures.hasOwnProperty(i)){if(t.hasOwnProperty(i))throw s=new Error('Signature "'+i+'" is defined twice'),s.data={signature:i},s;t[i]=n.signatures[i]}if(""!=n.name)if(""==r)r=n.name;else if(r!=n.name)throw s=new Error("Function names do not match (expected: "+r+", actual: "+n.name+")"),s.data={actual:n.name,expected:r},s}),_typed(r,t)}}),typed.config=config,typed.types=types,typed.conversions=conversions,typed}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
97276
1917
0