typed-function
Advanced tools
Comparing version 0.10.5 to 0.10.6
# History | ||
## 2017-11-18, version 0.10.6 | ||
- Fixed a security issue allowing to execute aritrary JavaScript code via a | ||
specially prepared function name of a typed function. Thanks Masato Kinugawa. | ||
## 2016-11-18, version 0.10.5 | ||
@@ -5,0 +11,0 @@ |
{ | ||
"name": "typed-function", | ||
"version": "0.10.5", | ||
"version": "0.10.6", | ||
"description": "Type checking for JavaScript functions", | ||
@@ -5,0 +5,0 @@ "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)", |
@@ -1099,8 +1099,11 @@ /** | ||
// generate code for the typed function | ||
// safeName is a conservative replacement of characters | ||
// to prevend being able to inject JS code at the place of the function name | ||
// the name is useful for stack trackes therefore we want have it there | ||
var code = []; | ||
var _name = name || ''; | ||
var _args = getArgs(maxParams(_signatures)); | ||
code.push('function ' + _name + '(' + _args.join(', ') + ') {'); | ||
var safeName = (name || '').replace(/[^a-zA-Z0-9_$]/g, '_') | ||
var args = getArgs(maxParams(_signatures)); | ||
code.push('function ' + safeName + '(' + args.join(', ') + ') {'); | ||
code.push(' "use strict";'); | ||
code.push(' var name = \'' + _name + '\';'); | ||
code.push(' var name = ' + JSON.stringify(name || '') + ';'); | ||
code.push(node.toCode(refs, ' ', false)); | ||
@@ -1107,0 +1110,0 @@ code.push('}'); |
@@ -1,1 +0,1 @@ | ||
"use strict";!function(r,n){"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():r.typed=n()}(this,function(){function r(){function n(r){for(var n,t=0;t<C.types.length;t++){var e=C.types[t];if(e.name===r){n=e.test;break}}if(!n){var s;for(t=0;t<C.types.length;t++)if(e=C.types[t],e.name.toLowerCase()==r.toLowerCase()){s=e.name;break}throw new Error('Unknown type "'+r+'"'+(s?'. Did you mean "'+s+'"?':""))}return n}function t(r){for(var n="",t=0;t<r.length;t++){var e=r[t];if(e.signatures&&""!=e.name)if(""==n)n=e.name;else if(n!=e.name){var s=new Error("Function names do not match (expected: "+n+", actual: "+e.name+")");throw s.data={actual:e.name,expected:n},s}}return n}function e(r,n,t,e,s){var o,i=y(e),a=s?s.split(","):null,p=r||"unnamed",u=a&&l(a,"any"),h={fn:r,index:t,actual:e,expected:a};o=a?n>t&&!u?"Unexpected type of argument in function "+p+" (expected: "+a.join(" or ")+", actual: "+i+", index: "+t+")":"Too few arguments in function "+p+" (expected: "+a.join(" or ")+", index: "+t+")":"Too many arguments in function "+p+" (expected: "+t+", actual: "+n+")";var f=new TypeError(o);return f.data=h,f}function s(r){this.name=r||"refs",this.categories={}}function o(r,n){if("string"==typeof r){var t=r.trim(),e="..."===t.substr(0,3);if(e&&(t=t.substr(3)),""===t)this.types=["any"];else{this.types=t.split("|");for(var s=0;s<this.types.length;s++)this.types[s]=this.types[s].trim()}}else{if(!Array.isArray(r)){if(r instanceof o)return r.clone();throw new Error("String or Array expected")}this.types=r}this.conversions=[],this.varArgs=e||n||!1,this.anyType=-1!==this.types.indexOf("any")}function i(r,n){var t;if("string"==typeof r)t=""!==r?r.split(","):[];else{if(!Array.isArray(r))throw new Error("string or Array expected");t=r}this.params=new Array(t.length),this.anyType=!1,this.varArgs=!1;for(var e=0;e<t.length;e++){var s=new o(t[e]);if(this.params[e]=s,s.anyType&&(this.anyType=!0),e===t.length-1)this.varArgs=s.varArgs;else if(s.varArgs)throw new SyntaxError('Unexpected variable arguments operator "..."')}this.fn=n}function a(r,n,t,e){this.path=r||[],this.param=r[r.length-1]||null,this.signature=n||null,this.childs=t||[],this.fallThrough=e||!1}function p(r){var n,t,e={},s=[];for(var o in r)if(r.hasOwnProperty(o)){var a=r[o];if(n=new i(o,a),n.ignore())continue;var p=n.expand();for(t=0;t<p.length;t++){var u=p[t],h=u.toString(),f=e[h];if(f){var c=i.compare(u,f);if(0>c)e[h]=u;else if(0===c)throw new Error('Signature "'+h+'" is defined twice')}else e[h]=u}}for(h in e)e.hasOwnProperty(h)&&s.push(e[h]);for(s.sort(function(r,n){return i.compare(r,n)}),t=0;t<s.length;t++)if(n=s[t],n.varArgs)for(var g=n.params.length-1,v=n.params[g],y=0;y<v.types.length;){if(v.conversions[y])for(var m=v.types[y],d=0;d<s.length;d++){var w=s[d],A=w.params[g];if(w!==n&&A&&l(A.types,m)&&!A.conversions[g]){v.types.splice(y,1),v.conversions.splice(y,1),y--;break}}y++}return s}function u(r){for(var n=[],t=0;t<r.length;t++)r[t].anyType&&n.push(r[t]);return n}function h(r){for(var n={},t=0;t<r.length;t++){var e=r[t];if(e.fn&&!e.hasConversions()){var s=e.params.join(",");n[s]=e.fn}}return n}function f(r,n,t){var e,s,i,p=n.length,u=[];for(e=0;e<r.length;e++)s=r[e],s.params.length!==p||i||(i=s),void 0!=s.params[p]&&u.push(s);u.sort(function(r,n){return o.compare(r.params[p],n.params[p])});var h=[];for(e=0;e<u.length;e++){s=u[e];var c=s.params[p],g=h.filter(function(r){return r.param.overlapping(c)})[0];if(g){if(g.param.varArgs)throw new Error('Conflicting types "'+g.param+'" and "'+c+'"');g.signatures.push(s)}else h.push({param:c,signatures:[s]})}var v=[];for(e=0;e<t.length;e++)t[e].paramsStartWith(n)&&v.push(t[e]);var y=!1;for(e=0;e<v.length;e++)if(!l(r,v[e])){y=!0;break}var m=new Array(h.length);for(e=0;e<h.length;e++){var d=h[e];m[e]=f(d.signatures,n.concat(d.param),v)}return new a(n,i,m,y)}function c(r){for(var n=[],t=0;r>t;t++)n[t]="arg"+t;return n}function g(r,n){var t=new s,o=p(n);if(0==o.length)throw new Error("No signatures provided");var i=u(o),a=f(o,[],i),g=[],y=r||"",l=c(v(o));g.push("function "+y+"("+l.join(", ")+") {"),g.push(' "use strict";'),g.push(" var name = '"+y+"';"),g.push(a.toCode(t," ",!1)),g.push("}");var m=[t.toCode(),"return "+g.join("\n")].join("\n"),d=new Function(t.name,"createError",m),w=d(t,e);return w.signatures=h(o),w}function v(r){for(var n=0,t=0;t<r.length;t++){var e=r[t].params.length;e>n&&(n=e)}return n}function y(r){for(var n,t=0;t<C.types.length;t++){var e=C.types[t];if("Object"===e.name)n=e;else if(e.test(r))return e.name}return n&&n.test(r)?n.name:"unknown"}function l(r,n){return-1!==r.indexOf(n)}function m(r){return r[r.length-1]}function d(r,n){if(!r.signatures)throw new TypeError("Function is no typed-function");var t;if("string"==typeof n){t=n.split(",");for(var e=0;e<t.length;e++)t[e]=t[e].trim()}else{if(!Array.isArray(n))throw new TypeError("String array or a comma separated string expected");t=n}var s=t.join(","),o=r.signatures[s];if(o)return o;throw new TypeError("Signature not found (signature: "+(r.name||"unnamed")+"("+t.join(", ")+"))")}function w(r,n){var t=y(r);if(n===t)return r;for(var e=0;e<C.conversions.length;e++){var s=C.conversions[e];if(s.from===t&&s.to===n)return s.convert(r)}throw new Error("Cannot convert from "+t+" to "+n)}s.prototype.add=function(r,n){var t=n||"fn";this.categories[t]||(this.categories[t]=[]);var e=this.categories[t].indexOf(r);return-1==e&&(e=this.categories[t].length,this.categories[t].push(r)),t+e},s.prototype.toCode=function(){var r=[],n=this.name+".categories",t=this.categories;for(var e in t)if(t.hasOwnProperty(e))for(var s=t[e],o=0;o<s.length;o++)r.push("var "+e+o+" = "+n+"['"+e+"']["+o+"];");return r.join("\n")},o.compare=function(r,n){if(r.anyType)return 1;if(n.anyType)return-1;if(l(r.types,"Object"))return 1;if(l(n.types,"Object"))return-1;if(r.hasConversions()){if(n.hasConversions()){var t,e,s;for(t=0;t<r.conversions.length;t++)if(void 0!==r.conversions[t]){e=r.conversions[t];break}for(t=0;t<n.conversions.length;t++)if(void 0!==n.conversions[t]){s=n.conversions[t];break}return C.conversions.indexOf(e)-C.conversions.indexOf(s)}return 1}if(n.hasConversions())return-1;var o,i;for(t=0;t<C.types.length;t++)if(C.types[t].name===r.types[0]){o=t;break}for(t=0;t<C.types.length;t++)if(C.types[t].name===n.types[0]){i=t;break}return o-i},o.prototype.overlapping=function(r){for(var n=0;n<this.types.length;n++)if(l(r.types,this.types[n]))return!0;return!1},o.prototype.matches=function(r){return this.anyType||r.anyType||this.overlapping(r)},o.prototype.clone=function(){var r=new o(this.types.slice(),this.varArgs);return r.conversions=this.conversions.slice(),r},o.prototype.hasConversions=function(){return this.conversions.length>0},o.prototype.contains=function(r){for(var n=0;n<this.types.length;n++)if(r[this.types[n]])return!0;return!1},o.prototype.toString=function(r){for(var n=[],t={},e=0;e<this.types.length;e++){var s=this.conversions[e],o=r&&s?s.to:this.types[e];o in t||(t[o]=!0,n.push(o))}return(this.varArgs?"...":"")+n.join("|")},i.prototype.clone=function(){return new i(this.params.slice(),this.fn)},i.prototype.expand=function(){function r(t,e){if(e.length<t.params.length){var s,a,p,u=t.params[e.length];if(u.varArgs){for(a=u.clone(),s=0;s<C.conversions.length;s++)if(p=C.conversions[s],!l(u.types,p.from)&&l(u.types,p.to)){var h=a.types.length;a.types[h]=p.from,a.conversions[h]=p}r(t,e.concat(a))}else{for(s=0;s<u.types.length;s++)r(t,e.concat(new o(u.types[s])));for(s=0;s<C.conversions.length;s++)p=C.conversions[s],!l(u.types,p.from)&&l(u.types,p.to)&&(a=new o(p.from),a.conversions[0]=p,r(t,e.concat(a)))}}else n.push(new i(e,t.fn))}var n=[];return r(this,[]),n},i.compare=function(r,n){if(r.params.length>n.params.length)return 1;if(r.params.length<n.params.length)return-1;var t,e=r.params.length,s=0,i=0;for(t=0;e>t;t++)r.params[t].hasConversions()&&s++,n.params[t].hasConversions()&&i++;if(s>i)return 1;if(i>s)return-1;for(t=0;t<r.params.length;t++){var a=o.compare(r.params[t],n.params[t]);if(0!==a)return a}return 0},i.prototype.hasConversions=function(){for(var r=0;r<this.params.length;r++)if(this.params[r].hasConversions())return!0;return!1},i.prototype.ignore=function(){for(var r={},n=0;n<C.ignore.length;n++)r[C.ignore[n]]=!0;for(n=0;n<this.params.length;n++)if(this.params[n].contains(r))return!0;return!1},i.prototype.paramsStartWith=function(r){if(0===r.length)return!0;for(var n=m(this.params),t=m(r),e=0;e<r.length;e++){var s=this.params[e]||(n.varArgs?n:null),o=r[e]||(t.varArgs?t:null);if(!s||!o||!s.matches(o))return!1}return!0},i.prototype.toCode=function(r,n){for(var t=[],e=new Array(this.params.length),s=0;s<this.params.length;s++){var o=this.params[s],i=o.conversions[0];e[s]=o.varArgs?"varArgs":i?r.add(i.convert,"convert")+"(arg"+s+")":"arg"+s}var a=this.fn?r.add(this.fn,"signature"):void 0;return a?n+"return "+a+"("+e.join(", ")+"); // signature: "+this.params.join(", "):t.join("\n")},i.prototype.toString=function(){return this.params.join(", ")},a.prototype.toCode=function(r,t){var e=[];if(this.param){var s=this.path.length-1,o=this.param.conversions[0],i="// type: "+(o?o.from+" (convert to "+o.to+")":this.param);if(this.param.varArgs)if(this.param.anyType)e.push(t+"if (arguments.length > "+s+") {"),e.push(t+" var varArgs = [];"),e.push(t+" for (var i = "+s+"; i < arguments.length; i++) {"),e.push(t+" varArgs.push(arguments[i]);"),e.push(t+" }"),e.push(this.signature.toCode(r,t+" ")),e.push(t+"}");else{for(var a=function(t,e){for(var s=[],o=0;o<t.length;o++)s[o]=r.add(n(t[o]),"test")+"("+e+")";return s.join(" || ")}.bind(this),p=this.param.types,u=[],h=0;h<p.length;h++)void 0===this.param.conversions[h]&&u.push(p[h]);e.push(t+"if ("+a(p,"arg"+s)+") { "+i),e.push(t+" var varArgs = [arg"+s+"];"),e.push(t+" for (var i = "+(s+1)+"; i < arguments.length; i++) {"),e.push(t+" if ("+a(u,"arguments[i]")+") {"),e.push(t+" varArgs.push(arguments[i]);");for(var h=0;h<p.length;h++){var f=this.param.conversions[h];if(f){var c=r.add(n(p[h]),"test"),g=r.add(f.convert,"convert");e.push(t+" }"),e.push(t+" else if ("+c+"(arguments[i])) {"),e.push(t+" varArgs.push("+g+"(arguments[i]));")}}e.push(t+" } else {"),e.push(t+" throw createError(name, arguments.length, i, arguments[i], '"+u.join(",")+"');"),e.push(t+" }"),e.push(t+" }"),e.push(this.signature.toCode(r,t+" ")),e.push(t+"}")}else if(this.param.anyType)e.push(t+"// type: any"),e.push(this._innerCode(r,t));else{var v=this.param.types[0],c="any"!==v?r.add(n(v),"test"):null;e.push(t+"if ("+c+"(arg"+s+")) { "+i),e.push(this._innerCode(r,t+" ")),e.push(t+"}")}}else e.push(this._innerCode(r,t));return e.join("\n")},a.prototype._innerCode=function(r,n){var t,e=[];for(this.signature&&(e.push(n+"if (arguments.length === "+this.path.length+") {"),e.push(this.signature.toCode(r,n+" ")),e.push(n+"}")),t=0;t<this.childs.length;t++)e.push(this.childs[t].toCode(r,n));if(!this.fallThrough||this.param&&this.param.anyType){var s=this._exceptions(r,n);s&&e.push(s)}return e.join("\n")},a.prototype._exceptions=function(r,n){var t=this.path.length;if(0===this.childs.length)return[n+"if (arguments.length > "+t+") {",n+" throw createError(name, arguments.length, "+t+", arguments["+t+"]);",n+"}"].join("\n");for(var e={},s=[],o=0;o<this.childs.length;o++){var i=this.childs[o];if(i.param)for(var a=0;a<i.param.types.length;a++){var p=i.param.types[a];p in e||i.param.conversions[a]||(e[p]=!0,s.push(p))}}return n+"throw createError(name, arguments.length, "+t+", arguments["+t+"], '"+s.join(",")+"');"};var A=[{name:"number",test:function(r){return"number"==typeof r}},{name:"string",test:function(r){return"string"==typeof r}},{name:"boolean",test:function(r){return"boolean"==typeof r}},{name:"Function",test:function(r){return"function"==typeof r}},{name:"Array",test:Array.isArray},{name:"Date",test:function(r){return r instanceof Date}},{name:"RegExp",test:function(r){return r instanceof RegExp}},{name:"Object",test:function(r){return"object"==typeof r}},{name:"null",test:function(r){return null===r}},{name:"undefined",test:function(r){return void 0===r}}],x={},j=[],b=[],C={config:x,types:A,conversions:j,ignore:b};return C=g("typed",{Object:function(r){var n=[];for(var e in r)r.hasOwnProperty(e)&&n.push(r[e]);var s=t(n);return g(s,r)},"string, Object":g,"...Function":function(r){for(var n,e=t(r),s={},o=0;o<r.length;o++){var i=r[o];if("object"!=typeof i.signatures)throw n=new TypeError("Function is no typed-function (index: "+o+")"),n.data={index:o},n;for(var a in i.signatures)if(i.signatures.hasOwnProperty(a))if(s.hasOwnProperty(a)){if(i.signatures[a]!==s[a])throw n=new Error('Signature "'+a+'" is defined twice'),n.data={signature:a},n}else s[a]=i.signatures[a]}return g(e,s)}}),C.config=x,C.types=A,C.conversions=j,C.ignore=b,C.create=r,C.find=d,C.convert=w,C.addType=function(r){if(!r||"string"!=typeof r.name||"function"!=typeof r.test)throw new TypeError("Object with properties {name: string, test: function} expected");C.types.push(r)},C.addConversion=function(r){if(!r||"string"!=typeof r.from||"string"!=typeof r.to||"function"!=typeof r.convert)throw new TypeError("Object with properties {from: string, to: string, convert: function} expected");C.conversions.push(r)},C}return r()}); | ||
"use strict";!function(r,n){"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():r.typed=n()}(this,function(){function r(){function n(r){for(var n,t=0;t<C.types.length;t++){var e=C.types[t];if(e.name===r){n=e.test;break}}if(!n){var s;for(t=0;t<C.types.length;t++)if(e=C.types[t],e.name.toLowerCase()==r.toLowerCase()){s=e.name;break}throw new Error('Unknown type "'+r+'"'+(s?'. Did you mean "'+s+'"?':""))}return n}function t(r){for(var n="",t=0;t<r.length;t++){var e=r[t];if(e.signatures&&""!=e.name)if(""==n)n=e.name;else if(n!=e.name){var s=new Error("Function names do not match (expected: "+n+", actual: "+e.name+")");throw s.data={actual:e.name,expected:n},s}}return n}function e(r,n,t,e,s){var i,o=y(e),a=s?s.split(","):null,p=r||"unnamed",u=a&&l(a,"any"),h={fn:r,index:t,actual:e,expected:a};i=a?n>t&&!u?"Unexpected type of argument in function "+p+" (expected: "+a.join(" or ")+", actual: "+o+", index: "+t+")":"Too few arguments in function "+p+" (expected: "+a.join(" or ")+", index: "+t+")":"Too many arguments in function "+p+" (expected: "+t+", actual: "+n+")";var f=new TypeError(i);return f.data=h,f}function s(r){this.name=r||"refs",this.categories={}}function i(r,n){if("string"==typeof r){var t=r.trim(),e="..."===t.substr(0,3);if(e&&(t=t.substr(3)),""===t)this.types=["any"];else{this.types=t.split("|");for(var s=0;s<this.types.length;s++)this.types[s]=this.types[s].trim()}}else{if(!Array.isArray(r)){if(r instanceof i)return r.clone();throw new Error("String or Array expected")}this.types=r}this.conversions=[],this.varArgs=e||n||!1,this.anyType=-1!==this.types.indexOf("any")}function o(r,n){var t;if("string"==typeof r)t=""!==r?r.split(","):[];else{if(!Array.isArray(r))throw new Error("string or Array expected");t=r}this.params=new Array(t.length),this.anyType=!1,this.varArgs=!1;for(var e=0;e<t.length;e++){var s=new i(t[e]);if(this.params[e]=s,s.anyType&&(this.anyType=!0),e===t.length-1)this.varArgs=s.varArgs;else if(s.varArgs)throw new SyntaxError('Unexpected variable arguments operator "..."')}this.fn=n}function a(r,n,t,e){this.path=r||[],this.param=r[r.length-1]||null,this.signature=n||null,this.childs=t||[],this.fallThrough=e||!1}function p(r){var n,t,e={},s=[];for(var i in r)if(r.hasOwnProperty(i)){var a=r[i];if(n=new o(i,a),n.ignore())continue;var p=n.expand();for(t=0;t<p.length;t++){var u=p[t],h=u.toString(),f=e[h];if(f){var c=o.compare(u,f);if(0>c)e[h]=u;else if(0===c)throw new Error('Signature "'+h+'" is defined twice')}else e[h]=u}}for(h in e)e.hasOwnProperty(h)&&s.push(e[h]);for(s.sort(function(r,n){return o.compare(r,n)}),t=0;t<s.length;t++)if(n=s[t],n.varArgs)for(var g=n.params.length-1,v=n.params[g],y=0;y<v.types.length;){if(v.conversions[y])for(var m=v.types[y],d=0;d<s.length;d++){var w=s[d],A=w.params[g];if(w!==n&&A&&l(A.types,m)&&!A.conversions[g]){v.types.splice(y,1),v.conversions.splice(y,1),y--;break}}y++}return s}function u(r){for(var n=[],t=0;t<r.length;t++)r[t].anyType&&n.push(r[t]);return n}function h(r){for(var n={},t=0;t<r.length;t++){var e=r[t];if(e.fn&&!e.hasConversions()){var s=e.params.join(",");n[s]=e.fn}}return n}function f(r,n,t){var e,s,o,p=n.length,u=[];for(e=0;e<r.length;e++)s=r[e],s.params.length!==p||o||(o=s),void 0!=s.params[p]&&u.push(s);u.sort(function(r,n){return i.compare(r.params[p],n.params[p])});var h=[];for(e=0;e<u.length;e++){s=u[e];var c=s.params[p],g=h.filter(function(r){return r.param.overlapping(c)})[0];if(g){if(g.param.varArgs)throw new Error('Conflicting types "'+g.param+'" and "'+c+'"');g.signatures.push(s)}else h.push({param:c,signatures:[s]})}var v=[];for(e=0;e<t.length;e++)t[e].paramsStartWith(n)&&v.push(t[e]);var y=!1;for(e=0;e<v.length;e++)if(!l(r,v[e])){y=!0;break}var m=new Array(h.length);for(e=0;e<h.length;e++){var d=h[e];m[e]=f(d.signatures,n.concat(d.param),v)}return new a(n,o,m,y)}function c(r){for(var n=[],t=0;r>t;t++)n[t]="arg"+t;return n}function g(r,n){var t=new s,i=p(n);if(0==i.length)throw new Error("No signatures provided");var o=u(i),a=f(i,[],o),g=[],y=(r||"").replace(/[^a-zA-Z0-9_$]/g,"_"),l=c(v(i));g.push("function "+y+"("+l.join(", ")+") {"),g.push(' "use strict";'),g.push(" var name = "+JSON.stringify(r||"")+";"),g.push(a.toCode(t," ",!1)),g.push("}");var m=[t.toCode(),"return "+g.join("\n")].join("\n"),d=new Function(t.name,"createError",m),w=d(t,e);return w.signatures=h(i),w}function v(r){for(var n=0,t=0;t<r.length;t++){var e=r[t].params.length;e>n&&(n=e)}return n}function y(r){for(var n,t=0;t<C.types.length;t++){var e=C.types[t];if("Object"===e.name)n=e;else if(e.test(r))return e.name}return n&&n.test(r)?n.name:"unknown"}function l(r,n){return-1!==r.indexOf(n)}function m(r){return r[r.length-1]}function d(r,n){if(!r.signatures)throw new TypeError("Function is no typed-function");var t;if("string"==typeof n){t=n.split(",");for(var e=0;e<t.length;e++)t[e]=t[e].trim()}else{if(!Array.isArray(n))throw new TypeError("String array or a comma separated string expected");t=n}var s=t.join(","),i=r.signatures[s];if(i)return i;throw new TypeError("Signature not found (signature: "+(r.name||"unnamed")+"("+t.join(", ")+"))")}function w(r,n){var t=y(r);if(n===t)return r;for(var e=0;e<C.conversions.length;e++){var s=C.conversions[e];if(s.from===t&&s.to===n)return s.convert(r)}throw new Error("Cannot convert from "+t+" to "+n)}s.prototype.add=function(r,n){var t=n||"fn";this.categories[t]||(this.categories[t]=[]);var e=this.categories[t].indexOf(r);return-1==e&&(e=this.categories[t].length,this.categories[t].push(r)),t+e},s.prototype.toCode=function(){var r=[],n=this.name+".categories",t=this.categories;for(var e in t)if(t.hasOwnProperty(e))for(var s=t[e],i=0;i<s.length;i++)r.push("var "+e+i+" = "+n+"['"+e+"']["+i+"];");return r.join("\n")},i.compare=function(r,n){if(r.anyType)return 1;if(n.anyType)return-1;if(l(r.types,"Object"))return 1;if(l(n.types,"Object"))return-1;if(r.hasConversions()){if(n.hasConversions()){var t,e,s;for(t=0;t<r.conversions.length;t++)if(void 0!==r.conversions[t]){e=r.conversions[t];break}for(t=0;t<n.conversions.length;t++)if(void 0!==n.conversions[t]){s=n.conversions[t];break}return C.conversions.indexOf(e)-C.conversions.indexOf(s)}return 1}if(n.hasConversions())return-1;var i,o;for(t=0;t<C.types.length;t++)if(C.types[t].name===r.types[0]){i=t;break}for(t=0;t<C.types.length;t++)if(C.types[t].name===n.types[0]){o=t;break}return i-o},i.prototype.overlapping=function(r){for(var n=0;n<this.types.length;n++)if(l(r.types,this.types[n]))return!0;return!1},i.prototype.matches=function(r){return this.anyType||r.anyType||this.overlapping(r)},i.prototype.clone=function(){var r=new i(this.types.slice(),this.varArgs);return r.conversions=this.conversions.slice(),r},i.prototype.hasConversions=function(){return this.conversions.length>0},i.prototype.contains=function(r){for(var n=0;n<this.types.length;n++)if(r[this.types[n]])return!0;return!1},i.prototype.toString=function(r){for(var n=[],t={},e=0;e<this.types.length;e++){var s=this.conversions[e],i=r&&s?s.to:this.types[e];i in t||(t[i]=!0,n.push(i))}return(this.varArgs?"...":"")+n.join("|")},o.prototype.clone=function(){return new o(this.params.slice(),this.fn)},o.prototype.expand=function(){function r(t,e){if(e.length<t.params.length){var s,a,p,u=t.params[e.length];if(u.varArgs){for(a=u.clone(),s=0;s<C.conversions.length;s++)if(p=C.conversions[s],!l(u.types,p.from)&&l(u.types,p.to)){var h=a.types.length;a.types[h]=p.from,a.conversions[h]=p}r(t,e.concat(a))}else{for(s=0;s<u.types.length;s++)r(t,e.concat(new i(u.types[s])));for(s=0;s<C.conversions.length;s++)p=C.conversions[s],!l(u.types,p.from)&&l(u.types,p.to)&&(a=new i(p.from),a.conversions[0]=p,r(t,e.concat(a)))}}else n.push(new o(e,t.fn))}var n=[];return r(this,[]),n},o.compare=function(r,n){if(r.params.length>n.params.length)return 1;if(r.params.length<n.params.length)return-1;var t,e=r.params.length,s=0,o=0;for(t=0;e>t;t++)r.params[t].hasConversions()&&s++,n.params[t].hasConversions()&&o++;if(s>o)return 1;if(o>s)return-1;for(t=0;t<r.params.length;t++){var a=i.compare(r.params[t],n.params[t]);if(0!==a)return a}return 0},o.prototype.hasConversions=function(){for(var r=0;r<this.params.length;r++)if(this.params[r].hasConversions())return!0;return!1},o.prototype.ignore=function(){for(var r={},n=0;n<C.ignore.length;n++)r[C.ignore[n]]=!0;for(n=0;n<this.params.length;n++)if(this.params[n].contains(r))return!0;return!1},o.prototype.paramsStartWith=function(r){if(0===r.length)return!0;for(var n=m(this.params),t=m(r),e=0;e<r.length;e++){var s=this.params[e]||(n.varArgs?n:null),i=r[e]||(t.varArgs?t:null);if(!s||!i||!s.matches(i))return!1}return!0},o.prototype.toCode=function(r,n){for(var t=[],e=new Array(this.params.length),s=0;s<this.params.length;s++){var i=this.params[s],o=i.conversions[0];e[s]=i.varArgs?"varArgs":o?r.add(o.convert,"convert")+"(arg"+s+")":"arg"+s}var a=this.fn?r.add(this.fn,"signature"):void 0;return a?n+"return "+a+"("+e.join(", ")+"); // signature: "+this.params.join(", "):t.join("\n")},o.prototype.toString=function(){return this.params.join(", ")},a.prototype.toCode=function(r,t){var e=[];if(this.param){var s=this.path.length-1,i=this.param.conversions[0],o="// type: "+(i?i.from+" (convert to "+i.to+")":this.param);if(this.param.varArgs)if(this.param.anyType)e.push(t+"if (arguments.length > "+s+") {"),e.push(t+" var varArgs = [];"),e.push(t+" for (var i = "+s+"; i < arguments.length; i++) {"),e.push(t+" varArgs.push(arguments[i]);"),e.push(t+" }"),e.push(this.signature.toCode(r,t+" ")),e.push(t+"}");else{for(var a=function(t,e){for(var s=[],i=0;i<t.length;i++)s[i]=r.add(n(t[i]),"test")+"("+e+")";return s.join(" || ")}.bind(this),p=this.param.types,u=[],h=0;h<p.length;h++)void 0===this.param.conversions[h]&&u.push(p[h]);e.push(t+"if ("+a(p,"arg"+s)+") { "+o),e.push(t+" var varArgs = [arg"+s+"];"),e.push(t+" for (var i = "+(s+1)+"; i < arguments.length; i++) {"),e.push(t+" if ("+a(u,"arguments[i]")+") {"),e.push(t+" varArgs.push(arguments[i]);");for(var h=0;h<p.length;h++){var f=this.param.conversions[h];if(f){var c=r.add(n(p[h]),"test"),g=r.add(f.convert,"convert");e.push(t+" }"),e.push(t+" else if ("+c+"(arguments[i])) {"),e.push(t+" varArgs.push("+g+"(arguments[i]));")}}e.push(t+" } else {"),e.push(t+" throw createError(name, arguments.length, i, arguments[i], '"+u.join(",")+"');"),e.push(t+" }"),e.push(t+" }"),e.push(this.signature.toCode(r,t+" ")),e.push(t+"}")}else if(this.param.anyType)e.push(t+"// type: any"),e.push(this._innerCode(r,t));else{var v=this.param.types[0],c="any"!==v?r.add(n(v),"test"):null;e.push(t+"if ("+c+"(arg"+s+")) { "+o),e.push(this._innerCode(r,t+" ")),e.push(t+"}")}}else e.push(this._innerCode(r,t));return e.join("\n")},a.prototype._innerCode=function(r,n){var t,e=[];for(this.signature&&(e.push(n+"if (arguments.length === "+this.path.length+") {"),e.push(this.signature.toCode(r,n+" ")),e.push(n+"}")),t=0;t<this.childs.length;t++)e.push(this.childs[t].toCode(r,n));if(!this.fallThrough||this.param&&this.param.anyType){var s=this._exceptions(r,n);s&&e.push(s)}return e.join("\n")},a.prototype._exceptions=function(r,n){var t=this.path.length;if(0===this.childs.length)return[n+"if (arguments.length > "+t+") {",n+" throw createError(name, arguments.length, "+t+", arguments["+t+"]);",n+"}"].join("\n");for(var e={},s=[],i=0;i<this.childs.length;i++){var o=this.childs[i];if(o.param)for(var a=0;a<o.param.types.length;a++){var p=o.param.types[a];p in e||o.param.conversions[a]||(e[p]=!0,s.push(p))}}return n+"throw createError(name, arguments.length, "+t+", arguments["+t+"], '"+s.join(",")+"');"};var A=[{name:"number",test:function(r){return"number"==typeof r}},{name:"string",test:function(r){return"string"==typeof r}},{name:"boolean",test:function(r){return"boolean"==typeof r}},{name:"Function",test:function(r){return"function"==typeof r}},{name:"Array",test:Array.isArray},{name:"Date",test:function(r){return r instanceof Date}},{name:"RegExp",test:function(r){return r instanceof RegExp}},{name:"Object",test:function(r){return"object"==typeof r}},{name:"null",test:function(r){return null===r}},{name:"undefined",test:function(r){return void 0===r}}],x={},j=[],b=[],C={config:x,types:A,conversions:j,ignore:b};return C=g("typed",{Object:function(r){var n=[];for(var e in r)r.hasOwnProperty(e)&&n.push(r[e]);var s=t(n);return g(s,r)},"string, Object":g,"...Function":function(r){for(var n,e=t(r),s={},i=0;i<r.length;i++){var o=r[i];if("object"!=typeof o.signatures)throw n=new TypeError("Function is no typed-function (index: "+i+")"),n.data={index:i},n;for(var a in o.signatures)if(o.signatures.hasOwnProperty(a))if(s.hasOwnProperty(a)){if(o.signatures[a]!==s[a])throw n=new Error('Signature "'+a+'" is defined twice'),n.data={signature:a},n}else s[a]=o.signatures[a]}return g(e,s)}}),C.config=x,C.types=A,C.conversions=j,C.ignore=b,C.create=r,C.find=d,C.convert=w,C.addType=function(r){if(!r||"string"!=typeof r.name||"function"!=typeof r.test)throw new TypeError("Object with properties {name: string, test: function} expected");C.types.push(r)},C.addConversion=function(r){if(!r||"string"!=typeof r.from||"string"!=typeof r.to||"function"!=typeof r.convert)throw new TypeError("Object with properties {from: string, to: string, convert: function} expected");C.conversions.push(r)},C}return r()}); |
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
135685
2618