typed-function
Advanced tools
Comparing version 0.10.4 to 0.10.5
# History | ||
## 2016-11-18, version 0.10.5 | ||
- Fixed the use of multi-layered use of `any` type. See #8. | ||
## 2016-04-09, version 0.10.4 | ||
@@ -5,0 +10,0 @@ |
{ | ||
"name": "typed-function", | ||
"version": "0.10.4", | ||
"version": "0.10.5", | ||
"description": "Type checking for JavaScript functions", | ||
@@ -23,5 +23,5 @@ "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)", | ||
"devDependencies": { | ||
"istanbul": "^0.3.2", | ||
"mocha": "^1.21.5", | ||
"uglify-js": "^2.4.15" | ||
"istanbul": "0.4.5", | ||
"mocha": "3.1.2", | ||
"uglify-js": "2.4.15" | ||
}, | ||
@@ -28,0 +28,0 @@ "main": "./typed-function", |
@@ -85,2 +85,3 @@ var assert = require('assert'); | ||
assert.throws(function () {fn([], 'foo')}, /TypeError: Too many arguments in function unnamed \(expected: 1, actual: 2\)/); | ||
assert.throws(function () {fn([], 'foo', 4)}, /TypeError: Too many arguments in function unnamed \(expected: 1, actual: 3\)/); | ||
}); | ||
@@ -106,2 +107,22 @@ | ||
it('should compose a function with multiple any type arguments (5)', function() { | ||
var fn = typed({ | ||
'string,string': function () { | ||
return 'string,string'; | ||
}, | ||
'any': function () { | ||
return 'any'; | ||
} | ||
}); | ||
assert(fn.signatures instanceof Object); | ||
assert.strictEqual(Object.keys(fn.signatures).length, 2); | ||
assert.equal(fn('foo', 'bar'), 'string,string'); | ||
assert.equal(fn([]), 'any'); | ||
assert.equal(fn('foo'), 'any'); | ||
assert.throws(function () {fn('foo', 'bar', 5)}, /TypeError: Too many arguments in function unnamed \(expected: 2, actual: 3\)/); | ||
assert.throws(function () {fn('foo', 2, 5)}, /TypeError: Too many arguments in function unnamed \(expected: 1, actual: 3\)/); | ||
assert.throws(function () {fn('foo', 5)}, /TypeError: Too many arguments in function unnamed \(expected: 1, actual: 2\)/); | ||
}); | ||
it('var arg any type arguments should only handle unmatched types', function() { | ||
@@ -118,2 +139,3 @@ var fn = typed({ | ||
assert.equal(fn([], 'foo'), 'Array,string'); | ||
assert.equal(fn([], 'foo', 'bar'), 'any'); | ||
assert.equal(fn('string'), 'any'); | ||
@@ -126,6 +148,62 @@ assert.equal(fn(2), 'any'); | ||
// FIXME: should should permit multi-layered use of any. See https://github.com/josdejong/typed-function/pull/8 | ||
it.skip('should permit multi-layered use of any', function() { | ||
it('multiple use of any', function() { | ||
var fn = typed({ | ||
'number,number': function () { | ||
return 'numbers'; | ||
}, | ||
'any,any': function () { | ||
return 'any'; | ||
} | ||
}); | ||
assert(fn.signatures instanceof Object); | ||
assert.strictEqual(Object.keys(fn.signatures).length, 2); | ||
assert.equal(fn('a','b'), 'any'); | ||
assert.equal(fn(1,1), 'numbers'); | ||
assert.equal(fn(1,'b'), 'any'); | ||
assert.equal(fn('a',1), 'any'); | ||
}); | ||
it('use one any in combination with vararg', function() { | ||
var fn = typed({ | ||
'number': function () { | ||
return 'numbers'; | ||
}, | ||
'any,...any': function () { | ||
return 'any'; | ||
} | ||
}); | ||
assert(fn.signatures instanceof Object); | ||
assert.strictEqual(Object.keys(fn.signatures).length, 2); | ||
assert.equal(fn('a','b'), 'any'); | ||
assert.equal(fn(1), 'numbers'); | ||
assert.equal(fn(1,'b'), 'any'); | ||
assert.equal(fn('a',2), 'any'); | ||
assert.equal(fn(1,2), 'any'); | ||
assert.equal(fn(1,2,3), 'any'); | ||
}); | ||
it('use multi-layered any in combination with vararg', function() { | ||
var fn = typed({ | ||
'number,number': function () { | ||
return 'numbers'; | ||
}, | ||
'any,any,...any': function () { | ||
return 'any'; | ||
} | ||
}); | ||
assert(fn.signatures instanceof Object); | ||
assert.strictEqual(Object.keys(fn.signatures).length, 2); | ||
assert.equal(fn('a','b','c'), 'any'); | ||
assert.equal(fn(1,2), 'numbers'); | ||
assert.equal(fn(1,'b',2), 'any'); | ||
assert.equal(fn('a',2,3), 'any'); | ||
assert.equal(fn(1,2,3), 'any'); | ||
}); | ||
it('should permit multi-layered use of any', function() { | ||
var fn = typed({ | ||
'any,any': function () { | ||
return 'two'; | ||
@@ -143,5 +221,5 @@ }, | ||
assert.equal(fn(1,1,'a'), 'three'); | ||
assert.throws(function () {fn(1,1,1)}, /TypeError: Unexpected type of argument in function unnamed \(expected: string, actual: number, index: 2\)/); | ||
assert.throws(function () {fn(1,1,1)}, /TypeError: Too many arguments in function unnamed \(expected: 2, actual: 3\)/); | ||
}); | ||
}); |
@@ -309,4 +309,5 @@ /** | ||
* Test whether this parameters types overlap an other parameters types. | ||
* Will not match ['any'] with ['number'] | ||
* @param {Param} other | ||
* @return {boolean} Returns true when there are conflicting types | ||
* @return {boolean} Returns true when there are overlapping types | ||
*/ | ||
@@ -323,2 +324,12 @@ Param.prototype.overlapping = function (other) { | ||
/** | ||
* Test whether this parameters types matches an other parameters types. | ||
* When any of the two parameters contains `any`, true is returned | ||
* @param {Param} other | ||
* @return {boolean} Returns true when there are matching types | ||
*/ | ||
Param.prototype.matches = function (other) { | ||
return this.anyType || other.anyType || this.overlapping(other); | ||
}; | ||
/** | ||
* Create a clone of this param | ||
@@ -403,5 +414,10 @@ * @returns {Param} Returns a cloned version of this param | ||
this.params = new Array(_params.length); | ||
this.anyType = false; | ||
this.varArgs = false; | ||
for (var i = 0; i < _params.length; i++) { | ||
var param = new Param(_params[i]); | ||
this.params[i] = param; | ||
if (param.anyType) { | ||
this.anyType = true; | ||
} | ||
if (i === _params.length - 1) { | ||
@@ -563,2 +579,26 @@ // the last argument | ||
/** | ||
* Test whether the path of this signature matches a given path. | ||
* @param {Param[]} params | ||
*/ | ||
Signature.prototype.paramsStartWith = function (params) { | ||
if (params.length === 0) { | ||
return true; | ||
} | ||
var aLast = last(this.params); | ||
var bLast = last(params); | ||
for (var i = 0; i < params.length; i++) { | ||
var a = this.params[i] || (aLast.varArgs ? aLast: null); | ||
var b = params[i] || (bLast.varArgs ? bLast: null); | ||
if (!a || !b || !a.matches(b)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Generate the code to invoke this signature | ||
@@ -608,5 +648,6 @@ * @param {Refs} refs | ||
* @param {Node[]} childs | ||
* @param {boolean} [fallThrough=false] | ||
* @constructor | ||
*/ | ||
function Node(path, signature, childs) { | ||
function Node(path, signature, childs, fallThrough) { | ||
this.path = path || []; | ||
@@ -616,2 +657,3 @@ this.param = path[path.length - 1] || null; | ||
this.childs = childs || []; | ||
this.fallThrough = fallThrough || false; | ||
} | ||
@@ -623,6 +665,5 @@ | ||
* @param {string} prefix | ||
* @param {Node | undefined} [anyType] Sibling of this node with any type parameter | ||
* @returns {string} Returns the code as string | ||
*/ | ||
Node.prototype.toCode = function (refs, prefix, anyType) { | ||
Node.prototype.toCode = function (refs, prefix) { | ||
// TODO: split this function in multiple functions, it's too large | ||
@@ -696,3 +737,3 @@ var code = []; | ||
code.push(prefix + '// type: any'); | ||
code.push(this._innerCode(refs, prefix, anyType)); | ||
code.push(this._innerCode(refs, prefix)); | ||
} | ||
@@ -705,3 +746,3 @@ else { | ||
code.push(prefix + 'if (' + test + '(arg' + index + ')) { ' + comment); | ||
code.push(this._innerCode(refs, prefix + ' ', anyType)); | ||
code.push(this._innerCode(refs, prefix + ' ')); | ||
code.push(prefix + '}'); | ||
@@ -713,3 +754,3 @@ } | ||
// root node (path is empty) | ||
code.push(this._innerCode(refs, prefix, anyType)); | ||
code.push(this._innerCode(refs, prefix)); | ||
} | ||
@@ -725,7 +766,6 @@ | ||
* @param {string} prefix | ||
* @param {Node | undefined} [anyType] Sibling of this node with any type parameter | ||
* @returns {string} Returns the inner code as string | ||
* @private | ||
*/ | ||
Node.prototype._innerCode = function (refs, prefix, anyType) { | ||
Node.prototype._innerCode = function (refs, prefix) { | ||
var code = []; | ||
@@ -740,26 +780,18 @@ var i; | ||
var nextAnyType; | ||
for (i = 0; i < this.childs.length; i++) { | ||
if (this.childs[i].param.anyType) { | ||
nextAnyType = this.childs[i]; | ||
break; | ||
} | ||
code.push(this.childs[i].toCode(refs, prefix)); | ||
} | ||
for (i = 0; i < this.childs.length; i++) { | ||
code.push(this.childs[i].toCode(refs, prefix, nextAnyType)); | ||
// TODO: shouldn't the this.param.anyType check be redundant | ||
if (!this.fallThrough || (this.param && this.param.anyType)) { | ||
var exceptions = this._exceptions(refs, prefix); | ||
if (exceptions) { | ||
code.push(exceptions); | ||
} | ||
} | ||
if (anyType && !this.param.anyType) { | ||
code.push(anyType.toCode(refs, prefix, nextAnyType)); | ||
} | ||
var exceptions = this._exceptions(refs, prefix); | ||
if (exceptions) { | ||
code.push(exceptions); | ||
} | ||
return code.join('\n'); | ||
}; | ||
/** | ||
@@ -899,2 +931,19 @@ * Generate code to throw exceptions | ||
/** | ||
* Filter all any type signatures | ||
* @param {Signature[]} signatures | ||
* @return {Signature[]} Returns only any type signatures | ||
*/ | ||
function filterAnyTypeSignatures (signatures) { | ||
var filtered = []; | ||
for (var i = 0; i < signatures.length; i++) { | ||
if (signatures[i].anyType) { | ||
filtered.push(signatures[i]); | ||
} | ||
} | ||
return filtered; | ||
} | ||
/** | ||
* create a map with normalized signatures as key and the function as value | ||
@@ -924,5 +973,6 @@ * @param {Signature[]} signatures An array with split signatures | ||
* @param {Param[]} path Traversed path of parameter types | ||
* @param {Signature[]} anys | ||
* @return {Node} Returns a node tree | ||
*/ | ||
function parseTree(signatures, path) { | ||
function parseTree(signatures, path, anys) { | ||
var i, signature; | ||
@@ -985,2 +1035,22 @@ var index = path.length; | ||
// find all any type signature that can still match our current path | ||
var matchingAnys = []; | ||
for (i = 0; i < anys.length; i++) { | ||
if (anys[i].paramsStartWith(path)) { | ||
matchingAnys.push(anys[i]); | ||
} | ||
} | ||
// see if there are any type signatures that don't match any of the | ||
// signatures that we have in our tree, i.e. we have alternative | ||
// matching signature(s) outside of our current tree and we should | ||
// fall through to them instead of throwing an exception | ||
var fallThrough = false; | ||
for (i = 0; i < matchingAnys.length; i++) { | ||
if (!contains(signatures, matchingAnys[i])) { | ||
fallThrough = true; | ||
break; | ||
} | ||
} | ||
// parse the childs | ||
@@ -990,6 +1060,6 @@ var childs = new Array(entries.length); | ||
var entry = entries[i]; | ||
childs[i] = parseTree(entry.signatures, path.concat(entry.param)) | ||
childs[i] = parseTree(entry.signatures, path.concat(entry.param), matchingAnys) | ||
} | ||
return new Node(path, nodeSignature, childs); | ||
return new Node(path, nodeSignature, childs, fallThrough); | ||
} | ||
@@ -1034,4 +1104,7 @@ | ||
// filter all any type signatures | ||
var anys = filterAnyTypeSignatures(_signatures); | ||
// parse signatures into a node tree | ||
var node = parseTree(_signatures, []); | ||
var node = parseTree(_signatures, [], anys); | ||
@@ -1049,3 +1122,3 @@ //var util = require('util'); | ||
code.push(' var name = \'' + _name + '\';'); | ||
code.push(node.toCode(refs, ' ')); | ||
code.push(node.toCode(refs, ' ', false)); | ||
code.push('}'); | ||
@@ -1116,11 +1189,20 @@ | ||
/** | ||
* Test whether an array contains some entry | ||
* Test whether an array contains some item | ||
* @param {Array} array | ||
* @param {*} entry | ||
* @return {boolean} Returns true if array contains entry, false if not. | ||
* @param {*} item | ||
* @return {boolean} Returns true if array contains item, false if not. | ||
*/ | ||
function contains(array, entry) { | ||
return array.indexOf(entry) !== -1; | ||
function contains(array, item) { | ||
return array.indexOf(item) !== -1; | ||
} | ||
/** | ||
* Returns the last item in the array | ||
* @param {Array} array | ||
* @return {*} item | ||
*/ | ||
function last (array) { | ||
return array[array.length - 1]; | ||
} | ||
// data type tests | ||
@@ -1127,0 +1209,0 @@ var types = [ |
@@ -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<A.types.length;t++){var e=A.types[t];if(e.name===r){n=e.test;break}}if(!n){var s;for(t=0;t<A.types.length;t++)if(e=A.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=v(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);for(var e=0;e<t.length;e++){var s=new o(t[e]);if(this.params[e]=s,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){this.path=r||[],this.param=r[r.length-1]||null,this.signature=n||null,this.childs=t||[]}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],x=w.params[g];if(w!==n&&x&&l(x.types,m)&&!x.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++){var e=r[t];if(e.fn&&!e.hasConversions()){var s=e.params.join(",");n[s]=e.fn}}return n}function h(r,n){var t,e,s,i=n.length,p=[];for(t=0;t<r.length;t++)e=r[t],e.params.length!==i||s||(s=e),void 0!=e.params[i]&&p.push(e);p.sort(function(r,n){return o.compare(r.params[i],n.params[i])});var u=[];for(t=0;t<p.length;t++){e=p[t];var f=e.params[i],c=u.filter(function(r){return r.param.overlapping(f)})[0];if(c){if(c.param.varArgs)throw new Error('Conflicting types "'+c.param+'" and "'+f+'"');c.signatures.push(e)}else u.push({param:f,signatures:[e]})}var g=new Array(u.length);for(t=0;t<u.length;t++){var v=u[t];g[t]=h(v.signatures,n.concat(v.param))}return new a(n,s,g)}function f(r){for(var n=[],t=0;r>t;t++)n[t]="arg"+t;return n}function c(r,n){var t=new s,o=p(n);if(0==o.length)throw new Error("No signatures provided");var i=h(o,[]),a=[],c=r||"",v=f(g(o));a.push("function "+c+"("+v.join(", ")+") {"),a.push(' "use strict";'),a.push(" var name = '"+c+"';"),a.push(i.toCode(t," ")),a.push("}");var l=[t.toCode(),"return "+a.join("\n")].join("\n"),y=new Function(t.name,"createError",l),m=y(t,e);return m.signatures=u(o),m}function g(r){for(var n=0,t=0;t<r.length;t++){var e=r[t].params.length;e>n&&(n=e)}return n}function v(r){for(var n,t=0;t<A.types.length;t++){var e=A.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 y(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 m(r,n){var t=v(r);if(n===t)return r;for(var e=0;e<A.conversions.length;e++){var s=A.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 A.conversions.indexOf(e)-A.conversions.indexOf(s)}return 1}if(n.hasConversions())return-1;var o,i;for(t=0;t<A.types.length;t++)if(A.types[t].name===r.types[0]){o=t;break}for(t=0;t<A.types.length;t++)if(A.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.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<A.conversions.length;s++)if(p=A.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<A.conversions.length;s++)p=A.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<A.ignore.length;n++)r[A.ignore[n]]=!0;for(n=0;n<this.params.length;n++)if(this.params[n].contains(r))return!0;return!1},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];o.varArgs?e[s]="varArgs":i?e[s]=r.add(i.convert,"convert")+"(arg"+s+")":e[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,e){var s=[];if(this.param){var o=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)s.push(t+"if (arguments.length > "+o+") {"),s.push(t+" var varArgs = [];"),s.push(t+" for (var i = "+o+"; i < arguments.length; i++) {"),s.push(t+" varArgs.push(arguments[i]);"),s.push(t+" }"),s.push(this.signature.toCode(r,t+" ")),s.push(t+"}");else{for(var p=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),u=this.param.types,h=[],f=0;f<u.length;f++)void 0===this.param.conversions[f]&&h.push(u[f]);s.push(t+"if ("+p(u,"arg"+o)+") { "+a),s.push(t+" var varArgs = [arg"+o+"];"),s.push(t+" for (var i = "+(o+1)+"; i < arguments.length; i++) {"),s.push(t+" if ("+p(h,"arguments[i]")+") {"),s.push(t+" varArgs.push(arguments[i]);");for(var f=0;f<u.length;f++){var c=this.param.conversions[f];if(c){var g=r.add(n(u[f]),"test"),v=r.add(c.convert,"convert");s.push(t+" }"),s.push(t+" else if ("+g+"(arguments[i])) {"),s.push(t+" varArgs.push("+v+"(arguments[i]));")}}s.push(t+" } else {"),s.push(t+" throw createError(name, arguments.length, i, arguments[i], '"+h.join(",")+"');"),s.push(t+" }"),s.push(t+" }"),s.push(this.signature.toCode(r,t+" ")),s.push(t+"}")}else if(this.param.anyType)s.push(t+"// type: any"),s.push(this._innerCode(r,t,e));else{var l=this.param.types[0],g="any"!==l?r.add(n(l),"test"):null;s.push(t+"if ("+g+"(arg"+o+")) { "+a),s.push(this._innerCode(r,t+" ",e)),s.push(t+"}")}}else s.push(this._innerCode(r,t,e));return s.join("\n")},a.prototype._innerCode=function(r,n,t){var e,s=[];this.signature&&(s.push(n+"if (arguments.length === "+this.path.length+") {"),s.push(this.signature.toCode(r,n+" ")),s.push(n+"}"));var o;for(e=0;e<this.childs.length;e++)if(this.childs[e].param.anyType){o=this.childs[e];break}for(e=0;e<this.childs.length;e++)s.push(this.childs[e].toCode(r,n,o));t&&!this.param.anyType&&s.push(t.toCode(r,n,o));var i=this._exceptions(r,n);return i&&s.push(i),s.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 d=[{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}}],w={},x=[],j=[],A={config:w,types:d,conversions:x,ignore:j};return A=c("typed",{Object:function(r){var n=[];for(var e in r)r.hasOwnProperty(e)&&n.push(r[e]);var s=t(n);return c(s,r)},"string, Object":c,"...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 c(e,s)}}),A.config=w,A.types=d,A.conversions=x,A.ignore=j,A.create=r,A.find=y,A.convert=m,A.addType=function(r){if(!r||"string"!=typeof r.name||"function"!=typeof r.test)throw new TypeError("Object with properties {name: string, test: function} expected");A.types.push(r)},A.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");A.conversions.push(r)},A}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 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()}); |
Sorry, the diff of this file is not supported yet
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
135268
2601