Comparing version 0.10.1 to 0.10.2
104
Args.js
@@ -51,36 +51,37 @@ /** | ||
var _typeMatches = function(arg, schemeEl) { | ||
var ok = false; | ||
if ((schemeEl.sarg & Args.ANY) !== 0) { | ||
return true; | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.STRING) !== 0 && typeof arg === "string") { | ||
return true; | ||
else if ((schemeEl.sarg & Args.STRING) !== 0 && typeof arg === "string") { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.FUNCTION) !== 0 && typeof arg === "function") { | ||
return true; | ||
else if ((schemeEl.sarg & Args.FUNCTION) !== 0 && typeof arg === "function") { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.INT) !== 0 && (typeof arg === "number" && Math.floor(arg) === arg)) { | ||
return true; | ||
else if ((schemeEl.sarg & Args.INT) !== 0 && (typeof arg === "number" && Math.floor(arg) === arg)) { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.FLOAT) !== 0 && typeof arg === "number") { | ||
return true; | ||
else if ((schemeEl.sarg & Args.FLOAT) !== 0 && typeof arg === "number") { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.ARRAY) !== 0 && (arg instanceof Array)) { | ||
return true; | ||
else if ((schemeEl.sarg & Args.ARRAY) !== 0 && (arg instanceof Array)) { | ||
ok = true; | ||
} | ||
if (((schemeEl.sarg & Args.OBJECT) !== 0 || schemeEl.typeValue !== undefined) && ( | ||
else if (((schemeEl.sarg & Args.OBJECT) !== 0 || schemeEl.typeValue !== undefined) && ( | ||
typeof arg === "object" && | ||
(schemeEl.typeValue === undefined || (arg instanceof schemeEl.typeValue)) | ||
)) { | ||
return true; | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.ARRAY_BUFFER) !== 0 && arg.toString().match(/ArrayBuffer/)) { | ||
return true; | ||
else if ((schemeEl.sarg & Args.ARRAY_BUFFER) !== 0 && arg.toString().match(/ArrayBuffer/)) { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.DATE) !== 0 && arg instanceof Date) { | ||
return true; | ||
else if ((schemeEl.sarg & Args.DATE) !== 0 && arg instanceof Date) { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.BOOL) !== 0 && typeof arg === "boolean") { | ||
return true; | ||
else if ((schemeEl.sarg & Args.BOOL) !== 0 && typeof arg === "boolean") { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.DOM_EL) !== 0 && | ||
else if ((schemeEl.sarg & Args.DOM_EL) !== 0 && | ||
( | ||
@@ -91,10 +92,12 @@ (arg instanceof HTMLElement) || | ||
) { | ||
return true; | ||
ok = true; | ||
} | ||
if (schemeEl.customCheck !== undefined && typeof schemeEl.customCheck === "function") { | ||
if (schemeEl.customCheck(arg)) { | ||
return true; | ||
ok = true; | ||
} else { | ||
ok = false; | ||
} | ||
} | ||
return false; | ||
return ok; | ||
}; | ||
@@ -177,3 +180,3 @@ | ||
return (schemeA.sarg & ~(Args.Optional | Args.Required)) === (schemeB.sarg & ~(Args.Optional | Args.Required)) && | ||
schemeA.typeValue === schemeB.typeValue && schemeA.customCheck === schemeB.customCheck; | ||
schemeA.typeValue === schemeB.typeValue; | ||
}; | ||
@@ -189,2 +192,18 @@ | ||
var _reasonForFailure = function(schemeEl, a, arg) { | ||
var err = "" | ||
if (_isTypeSpecified(schemeEl)) { | ||
err = "Argument " + a + " ("+schemeEl.sname+") should be type "+_getTypeString(schemeEl)+", but it was type " + (typeof arg) + " with value " + arg + "."; | ||
} else if (schemeEl.customCheck !== undefined) { | ||
var funcString = schemeEl.customCheck.toString(); | ||
if (funcString.length > 50) { | ||
funcString = funcString.substr(0, 40) + "..." + funcString.substr(funcString.length-10); | ||
} | ||
err = "Argument " + a + " ("+schemeEl.sname+") does not pass the custom check ("+funcString+")."; | ||
} else { | ||
err = "Argument " + a + " ("+schemeEl.sname+") has no valid type specified."; | ||
} | ||
return err; | ||
}; | ||
/** | ||
@@ -233,10 +252,15 @@ * Last argument may be a named argument object. This is decided in a non-greedy way, if | ||
}; | ||
var _shiftRun = function(schemeEl, r) { | ||
var _shiftRun = function(schemeEl, a, r) { | ||
if (r === undefined) r = run.length-1; | ||
if (r < 0) return; | ||
var lastMatch = run[r]; | ||
returns[schemeEl.sname] = returns[lastMatch.sname]; | ||
returns[lastMatch.sname] = undefined; | ||
if ((lastMatch.sarg & Args.Optional) === 0) { // if the last in the run was not optional | ||
_shiftRun(lastMatch, r-1); | ||
var arg = returns[lastMatch.sname]; | ||
if (_typeMatches(arg, schemeEl)) { | ||
returns[schemeEl.sname] = arg; | ||
returns[lastMatch.sname] = undefined; | ||
if ((lastMatch.sarg & Args.Optional) === 0) { // if the last in the run was not optional | ||
_shiftRun(lastMatch, a, r-1); | ||
} | ||
} else { | ||
return _reasonForFailure(schemeEl, arg, a); | ||
} | ||
@@ -316,4 +340,6 @@ }; | ||
if (_isTypeSpecified(schemeEl) && _schemesMatch(schemeEl, runType)) { | ||
_shiftRun(schemeEl); | ||
_addToRun(schemeEl); | ||
err = _shiftRun(schemeEl, a); | ||
if (err === "") { | ||
_addToRun(schemeEl); | ||
} | ||
return a; | ||
@@ -326,18 +352,10 @@ } else { | ||
else if (!_typeMatches(arg, schemeEl)) { | ||
if (_isTypeSpecified(schemeEl)) { | ||
if (_schemesMatch(schemeEl, runType)) { | ||
_shiftRun(schemeEl); | ||
if (_isTypeSpecified(schemeEl) && _schemesMatch(schemeEl, runType)) { | ||
err = _shiftRun(schemeEl, a); | ||
if (err === "") { | ||
_addToRun(schemeEl); | ||
return a+1; | ||
} else { | ||
err = "Argument " + a + " ("+schemeEl.sname+") should be type "+_getTypeString(schemeEl)+", but it was type " + (typeof arg) + " with value " + arg + "."; | ||
} | ||
} else if (schemeEl.customCheck !== undefined) { | ||
var funcString = schemeEl.customCheck.toString(); | ||
if (funcString.length > 50) { | ||
funcString = funcString.substr(0, 40) + "..." + funcString.substr(funcString.length-10); | ||
} | ||
err = "Argument " + a + " ("+schemeEl.sname+") does not pass the custom check ("+funcString+")."; | ||
} else { | ||
err = "Argument " + a + " ("+schemeEl.sname+") has no valid type specified."; | ||
err = _reasonForFailure(schemeEl, arg, a); | ||
} | ||
@@ -344,0 +362,0 @@ return a; |
{ | ||
"name": "args.js", | ||
"version": "0.10.1", | ||
"version": "0.10.2", | ||
"homepage": "http://autographer.github.io/args.js", | ||
@@ -5,0 +5,0 @@ "description": "Create javascript functions with optional, default and named paramaters.", |
104
dist/Args.js
@@ -51,36 +51,37 @@ /** | ||
var _typeMatches = function(arg, schemeEl) { | ||
var ok = false; | ||
if ((schemeEl.sarg & Args.ANY) !== 0) { | ||
return true; | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.STRING) !== 0 && typeof arg === "string") { | ||
return true; | ||
else if ((schemeEl.sarg & Args.STRING) !== 0 && typeof arg === "string") { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.FUNCTION) !== 0 && typeof arg === "function") { | ||
return true; | ||
else if ((schemeEl.sarg & Args.FUNCTION) !== 0 && typeof arg === "function") { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.INT) !== 0 && (typeof arg === "number" && Math.floor(arg) === arg)) { | ||
return true; | ||
else if ((schemeEl.sarg & Args.INT) !== 0 && (typeof arg === "number" && Math.floor(arg) === arg)) { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.FLOAT) !== 0 && typeof arg === "number") { | ||
return true; | ||
else if ((schemeEl.sarg & Args.FLOAT) !== 0 && typeof arg === "number") { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.ARRAY) !== 0 && (arg instanceof Array)) { | ||
return true; | ||
else if ((schemeEl.sarg & Args.ARRAY) !== 0 && (arg instanceof Array)) { | ||
ok = true; | ||
} | ||
if (((schemeEl.sarg & Args.OBJECT) !== 0 || schemeEl.typeValue !== undefined) && ( | ||
else if (((schemeEl.sarg & Args.OBJECT) !== 0 || schemeEl.typeValue !== undefined) && ( | ||
typeof arg === "object" && | ||
(schemeEl.typeValue === undefined || (arg instanceof schemeEl.typeValue)) | ||
)) { | ||
return true; | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.ARRAY_BUFFER) !== 0 && arg.toString().match(/ArrayBuffer/)) { | ||
return true; | ||
else if ((schemeEl.sarg & Args.ARRAY_BUFFER) !== 0 && arg.toString().match(/ArrayBuffer/)) { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.DATE) !== 0 && arg instanceof Date) { | ||
return true; | ||
else if ((schemeEl.sarg & Args.DATE) !== 0 && arg instanceof Date) { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.BOOL) !== 0 && typeof arg === "boolean") { | ||
return true; | ||
else if ((schemeEl.sarg & Args.BOOL) !== 0 && typeof arg === "boolean") { | ||
ok = true; | ||
} | ||
if ((schemeEl.sarg & Args.DOM_EL) !== 0 && | ||
else if ((schemeEl.sarg & Args.DOM_EL) !== 0 && | ||
( | ||
@@ -91,10 +92,12 @@ (arg instanceof HTMLElement) || | ||
) { | ||
return true; | ||
ok = true; | ||
} | ||
if (schemeEl.customCheck !== undefined && typeof schemeEl.customCheck === "function") { | ||
if (schemeEl.customCheck(arg)) { | ||
return true; | ||
ok = true; | ||
} else { | ||
ok = false; | ||
} | ||
} | ||
return false; | ||
return ok; | ||
}; | ||
@@ -177,3 +180,3 @@ | ||
return (schemeA.sarg & ~(Args.Optional | Args.Required)) === (schemeB.sarg & ~(Args.Optional | Args.Required)) && | ||
schemeA.typeValue === schemeB.typeValue && schemeA.customCheck === schemeB.customCheck; | ||
schemeA.typeValue === schemeB.typeValue; | ||
}; | ||
@@ -189,2 +192,18 @@ | ||
var _reasonForFailure = function(schemeEl, a, arg) { | ||
var err = "" | ||
if (_isTypeSpecified(schemeEl)) { | ||
err = "Argument " + a + " ("+schemeEl.sname+") should be type "+_getTypeString(schemeEl)+", but it was type " + (typeof arg) + " with value " + arg + "."; | ||
} else if (schemeEl.customCheck !== undefined) { | ||
var funcString = schemeEl.customCheck.toString(); | ||
if (funcString.length > 50) { | ||
funcString = funcString.substr(0, 40) + "..." + funcString.substr(funcString.length-10); | ||
} | ||
err = "Argument " + a + " ("+schemeEl.sname+") does not pass the custom check ("+funcString+")."; | ||
} else { | ||
err = "Argument " + a + " ("+schemeEl.sname+") has no valid type specified."; | ||
} | ||
return err; | ||
}; | ||
/** | ||
@@ -233,10 +252,15 @@ * Last argument may be a named argument object. This is decided in a non-greedy way, if | ||
}; | ||
var _shiftRun = function(schemeEl, r) { | ||
var _shiftRun = function(schemeEl, a, r) { | ||
if (r === undefined) r = run.length-1; | ||
if (r < 0) return; | ||
var lastMatch = run[r]; | ||
returns[schemeEl.sname] = returns[lastMatch.sname]; | ||
returns[lastMatch.sname] = undefined; | ||
if ((lastMatch.sarg & Args.Optional) === 0) { // if the last in the run was not optional | ||
_shiftRun(lastMatch, r-1); | ||
var arg = returns[lastMatch.sname]; | ||
if (_typeMatches(arg, schemeEl)) { | ||
returns[schemeEl.sname] = arg; | ||
returns[lastMatch.sname] = undefined; | ||
if ((lastMatch.sarg & Args.Optional) === 0) { // if the last in the run was not optional | ||
_shiftRun(lastMatch, a, r-1); | ||
} | ||
} else { | ||
return _reasonForFailure(schemeEl, arg, a); | ||
} | ||
@@ -316,4 +340,6 @@ }; | ||
if (_isTypeSpecified(schemeEl) && _schemesMatch(schemeEl, runType)) { | ||
_shiftRun(schemeEl); | ||
_addToRun(schemeEl); | ||
err = _shiftRun(schemeEl, a); | ||
if (err === "") { | ||
_addToRun(schemeEl); | ||
} | ||
return a; | ||
@@ -326,18 +352,10 @@ } else { | ||
else if (!_typeMatches(arg, schemeEl)) { | ||
if (_isTypeSpecified(schemeEl)) { | ||
if (_schemesMatch(schemeEl, runType)) { | ||
_shiftRun(schemeEl); | ||
if (_isTypeSpecified(schemeEl) && _schemesMatch(schemeEl, runType)) { | ||
err = _shiftRun(schemeEl, a); | ||
if (err === "") { | ||
_addToRun(schemeEl); | ||
return a+1; | ||
} else { | ||
err = "Argument " + a + " ("+schemeEl.sname+") should be type "+_getTypeString(schemeEl)+", but it was type " + (typeof arg) + " with value " + arg + "."; | ||
} | ||
} else if (schemeEl.customCheck !== undefined) { | ||
var funcString = schemeEl.customCheck.toString(); | ||
if (funcString.length > 50) { | ||
funcString = funcString.substr(0, 40) + "..." + funcString.substr(funcString.length-10); | ||
} | ||
err = "Argument " + a + " ("+schemeEl.sname+") does not pass the custom check ("+funcString+")."; | ||
} else { | ||
err = "Argument " + a + " ("+schemeEl.sname+") has no valid type specified."; | ||
err = _reasonForFailure(schemeEl, arg, a); | ||
} | ||
@@ -344,0 +362,0 @@ return a; |
@@ -1,2 +0,2 @@ | ||
var Args=function(){"use strict";var a=function(a){var b={};b.defValue=void 0,b.typeValue=void 0,b.customCheck=void 0;for(var c in a)a.hasOwnProperty(c)&&("_default"===c?b.defValue=a[c]:"_type"===c?b.typeValue=a[c]:"_check"===c?b.customCheck=a[c]:b.sname=c);return b.sarg=a[b.sname],b},b=function(a,b){return 0!==(b.sarg&i.ANY)?!0:0!==(b.sarg&i.STRING)&&"string"==typeof a?!0:0!==(b.sarg&i.FUNCTION)&&"function"==typeof a?!0:0!==(b.sarg&i.INT)&&"number"==typeof a&&Math.floor(a)===a?!0:0!==(b.sarg&i.FLOAT)&&"number"==typeof a?!0:0!==(b.sarg&i.ARRAY)&&a instanceof Array?!0:0===(b.sarg&i.OBJECT)&&void 0===b.typeValue||"object"!=typeof a||!(void 0===b.typeValue||a instanceof b.typeValue)?0!==(b.sarg&i.ARRAY_BUFFER)&&a.toString().match(/ArrayBuffer/)?!0:0!==(b.sarg&i.DATE)&&a instanceof Date?!0:0!==(b.sarg&i.BOOL)&&"boolean"==typeof a?!0:0!==(b.sarg&i.DOM_EL)&&(a instanceof HTMLElement||void 0!==window.$&&a instanceof window.$)?!0:void 0!==b.customCheck&&"function"==typeof b.customCheck&&b.customCheck(a)?!0:!1:!0},c=function(a){return 0!=(a.sarg&(i.ANY|i.STRING|i.FUNCTION|i.INT|i.FLOAT|i.OBJECT|i.ARRAY_BUFFER|i.DATE|i.BOOL|i.DOM_EL|i.ARRAY))||void 0!==a.typeValue},d=function(a){var b=a.sarg,c=a.typeValue,d=a.customCheck;return 0!==(b&i.STRING)?"String":0!==(b&i.FUNCTION)?"Function":0!==(b&i.INT)?"Int":0!==(b&i.FLOAT)?"Float":0!==(b&i.ARRAY)?"Array":0!==(b&i.OBJECT)?void 0!==c?"Object ("+c.toString()+")":"Object":0!==(b&i.ARRAY_BUFFER)?"Arry Buffer":0!==(b&i.DATE)?"Date":0!==(b&i.BOOL)?"Bool":0!==(b&i.DOM_EL)?"DOM Element":void 0!==d?"[Custom checker]":"unknown"},e=function(c,d,e){for(var f=!1,g=0;g<d.length;g++){var h=function(a){var d=!1;for(var f in c){var g=c[f];if(f===a.sname&&b(g,a)){e[f]=g,d=!0;break}}return d}(a(d[g]));h&&d.splice(g--,1),f|=h}return f},f=function(a,b){return a&&b?(a.sarg&~(i.Optional|i.Required))===(b.sarg&~(i.Optional|i.Required))&&a.typeValue===b.typeValue&&a.customCheck===b.customCheck:!1},g=function(a){return!h(a)},h=function(a){return 0!==(a&i.Optional)},i=function(j,k){if(void 0===j)throw new Error("The scheme has not been passed.");if(void 0===k)throw new Error("The arguments have not been passed.");k=Array.prototype.slice.call(k,0);var l,m,n={},o=void 0,p=void 0,q=[],r=function(a){(!p||!f(p,a)||g(p.sarg)&&h(a.sarg))&&(q=[]),(q.length>0||h(a.sarg))&&(p=a,q.push(a))},s=function(a,b){if(void 0===b&&(b=q.length-1),!(0>b)){var c=q[b];n[a.sname]=n[c.sname],n[c.sname]=void 0,0===(c.sarg&i.Optional)&&s(c,b-1)}};for("object"==typeof k[k.length-1]&&e(k[k.length-1],j,n)&&k.splice(k.length-1,1),l=0,m=0;m<j.length&&(l=function(e,g){var h=k[e];if(j[g]instanceof Array){if(null===h||void 0===h)return o="Argument "+e+" is null or undefined but it must be not null.",e;for(var l=j[g],m=void 0,q=0;q<l.length;q++){var t=a(l[q]);b(h,t)&&(m=t.sname)}if(void 0===m){o="Argument "+e+" should be one of: ";for(var q=0;q<l.length;q++){var t=a(l[q]);o+=d(t)+", "}return o+="but it was type "+typeof h+" with value "+h+".",e}return n[m]=h,e+1}var t=a(j[g]);if(0===(t.sarg&i.Optional)){if(null===h||void 0===h)return c(t)&&f(t,p)?(s(t),r(t),e):(o="Argument "+e+" ("+t.sname+") is null or undefined but it must be not null.",e);if(b(h,t))return n[t.sname]=h,r(t),e+1;if(c(t)){if(f(t,p))return s(t),r(t),e+1;o="Argument "+e+" ("+t.sname+") should be type "+d(t)+", but it was type "+typeof h+" with value "+h+"."}else if(void 0!==t.customCheck){var u=t.customCheck.toString();u.length>50&&(u=u.substr(0,40)+"..."+u.substr(u.length-10)),o="Argument "+e+" ("+t.sname+") does not pass the custom check ("+u+")."}else o="Argument "+e+" ("+t.sname+") has no valid type specified.";return e}return null===h||void 0===h?(n[t.sname]=void 0!==t.defValue?t.defValue:h,e+1):b(h,t)?(n[t.sname]=h,r(t),e+1):void 0!==t.defValue?(n[t.sname]=t.defValue,e):e}(l,m),!o);m++);if(o)throw new Error(o);return n};return i.ANY=1,i.STRING=2,i.FUNCTION=4,i.INT=8,i.FLOAT=16,i.ARRAY_BUFFER=32,i.OBJECT=64,i.DATE=128,i.BOOL=256,i.DOM_EL=512,i.ARRAY=1024,i.Optional=2048,i.NotNull=i.Required=4096,i}();try{module.exports=Args}catch(e){} | ||
var Args=function(){"use strict";var a=function(a){var b={};b.defValue=void 0,b.typeValue=void 0,b.customCheck=void 0;for(var c in a)a.hasOwnProperty(c)&&("_default"===c?b.defValue=a[c]:"_type"===c?b.typeValue=a[c]:"_check"===c?b.customCheck=a[c]:b.sname=c);return b.sarg=a[b.sname],b},b=function(a,b){var c=!1;return 0!==(b.sarg&j.ANY)?c=!0:0!==(b.sarg&j.STRING)&&"string"==typeof a?c=!0:0!==(b.sarg&j.FUNCTION)&&"function"==typeof a?c=!0:0!==(b.sarg&j.INT)&&"number"==typeof a&&Math.floor(a)===a?c=!0:0!==(b.sarg&j.FLOAT)&&"number"==typeof a?c=!0:0!==(b.sarg&j.ARRAY)&&a instanceof Array?c=!0:0===(b.sarg&j.OBJECT)&&void 0===b.typeValue||"object"!=typeof a||!(void 0===b.typeValue||a instanceof b.typeValue)?0!==(b.sarg&j.ARRAY_BUFFER)&&a.toString().match(/ArrayBuffer/)?c=!0:0!==(b.sarg&j.DATE)&&a instanceof Date?c=!0:0!==(b.sarg&j.BOOL)&&"boolean"==typeof a?c=!0:0!==(b.sarg&j.DOM_EL)&&(a instanceof HTMLElement||void 0!==window.$&&a instanceof window.$)&&(c=!0):c=!0,void 0!==b.customCheck&&"function"==typeof b.customCheck&&(c=b.customCheck(a)?!0:!1),c},c=function(a){return 0!=(a.sarg&(j.ANY|j.STRING|j.FUNCTION|j.INT|j.FLOAT|j.OBJECT|j.ARRAY_BUFFER|j.DATE|j.BOOL|j.DOM_EL|j.ARRAY))||void 0!==a.typeValue},d=function(a){var b=a.sarg,c=a.typeValue,d=a.customCheck;return 0!==(b&j.STRING)?"String":0!==(b&j.FUNCTION)?"Function":0!==(b&j.INT)?"Int":0!==(b&j.FLOAT)?"Float":0!==(b&j.ARRAY)?"Array":0!==(b&j.OBJECT)?void 0!==c?"Object ("+c.toString()+")":"Object":0!==(b&j.ARRAY_BUFFER)?"Arry Buffer":0!==(b&j.DATE)?"Date":0!==(b&j.BOOL)?"Bool":0!==(b&j.DOM_EL)?"DOM Element":void 0!==d?"[Custom checker]":"unknown"},e=function(c,d,e){for(var f=!1,g=0;g<d.length;g++){var h=function(a){var d=!1;for(var f in c){var g=c[f];if(f===a.sname&&b(g,a)){e[f]=g,d=!0;break}}return d}(a(d[g]));h&&d.splice(g--,1),f|=h}return f},f=function(a,b){return a&&b?(a.sarg&~(j.Optional|j.Required))===(b.sarg&~(j.Optional|j.Required))&&a.typeValue===b.typeValue:!1},g=function(a){return!h(a)},h=function(a){return 0!==(a&j.Optional)},i=function(a,b,e){var f="";if(c(a))f="Argument "+b+" ("+a.sname+") should be type "+d(a)+", but it was type "+typeof e+" with value "+e+".";else if(void 0!==a.customCheck){var g=a.customCheck.toString();g.length>50&&(g=g.substr(0,40)+"..."+g.substr(g.length-10)),f="Argument "+b+" ("+a.sname+") does not pass the custom check ("+g+")."}else f="Argument "+b+" ("+a.sname+") has no valid type specified.";return f},j=function(k,l){if(void 0===k)throw new Error("The scheme has not been passed.");if(void 0===l)throw new Error("The arguments have not been passed.");l=Array.prototype.slice.call(l,0);var m,n,o={},p=void 0,q=void 0,r=[],s=function(a){(!q||!f(q,a)||g(q.sarg)&&h(a.sarg))&&(r=[]),(r.length>0||h(a.sarg))&&(q=a,r.push(a))},t=function(a,c,d){if(void 0===d&&(d=r.length-1),!(0>d)){var e=r[d],f=o[e.sname];return b(f,a)?(o[a.sname]=f,o[e.sname]=void 0,0===(e.sarg&j.Optional)&&t(e,c,d-1),void 0):i(a,f,c)}};for("object"==typeof l[l.length-1]&&e(l[l.length-1],k,o)&&l.splice(l.length-1,1),m=0,n=0;n<k.length&&(m=function(e,g){var h=l[e];if(k[g]instanceof Array){if(null===h||void 0===h)return p="Argument "+e+" is null or undefined but it must be not null.",e;for(var m=k[g],n=void 0,r=0;r<m.length;r++){var u=a(m[r]);b(h,u)&&(n=u.sname)}if(void 0===n){p="Argument "+e+" should be one of: ";for(var r=0;r<m.length;r++){var u=a(m[r]);p+=d(u)+", "}return p+="but it was type "+typeof h+" with value "+h+".",e}return o[n]=h,e+1}var u=a(k[g]);if(0===(u.sarg&j.Optional)){if(null===h||void 0===h)return c(u)&&f(u,q)?(p=t(u,e),""===p&&s(u),e):(p="Argument "+e+" ("+u.sname+") is null or undefined but it must be not null.",e);if(b(h,u))return o[u.sname]=h,s(u),e+1;if(c(u)&&f(u,q)){if(p=t(u,e),""===p)return s(u),e+1}else p=i(u,h,e);return e}return null===h||void 0===h?(o[u.sname]=void 0!==u.defValue?u.defValue:h,e+1):b(h,u)?(o[u.sname]=h,s(u),e+1):void 0!==u.defValue?(o[u.sname]=u.defValue,e):e}(m,n),!p);n++);if(p)throw new Error(p);return o};return j.ANY=1,j.STRING=2,j.FUNCTION=4,j.INT=8,j.FLOAT=16,j.ARRAY_BUFFER=32,j.OBJECT=64,j.DATE=128,j.BOOL=256,j.DOM_EL=512,j.ARRAY=1024,j.Optional=2048,j.NotNull=j.Required=4096,j}();try{module.exports=Args}catch(e){} | ||
//# sourceMappingURL=Args.min.map |
{ | ||
"name": "args-js", | ||
"version": "0.10.1", | ||
"version": "0.10.2", | ||
"description": "Create javascript functions with optional, default and named paramaters.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -474,3 +474,5 @@ if (typeof window === 'undefined') { | ||
describe("Custom check functions", function() { | ||
var testStringArg = "testArg"; | ||
var testIntArg = 42; | ||
it("should match an int based on a custom value check", function() { | ||
@@ -491,2 +493,34 @@ var args = Args([ | ||
}); | ||
it("should parse a required argument with a check after an optional argument of the same type", function() { | ||
var check = function(o) { return o === testStringArg; } | ||
var args = new Args([ | ||
{ test1: Args.STRING | Args.Optional }, | ||
{ test2: Args.STRING | Args.Required, _check: check } | ||
], [testStringArg]); | ||
assert.equal(args.test1, undefined); | ||
assert.equal(args.test2, testStringArg); | ||
}); | ||
it("should call a check for an argument which satisfies a type", function(done) { | ||
var check = function(o) { | ||
done(); | ||
return o === testStringArg; | ||
} | ||
var args = new Args([ | ||
{ test2: Args.STRING | Args.Required, _check: check } | ||
], [testStringArg]); | ||
}); | ||
it("should call a check for an argument after an argument without a check", function(done) { | ||
var check = function(o) { | ||
done(); | ||
return o === testStringArg; | ||
} | ||
var args = new Args([ | ||
{ test1: Args.STRING | Args.Optional }, | ||
{ test2: Args.STRING | Args.Required, _check: check } | ||
], [testStringArg]); | ||
}); | ||
}); | ||
@@ -493,0 +527,0 @@ |
Sorry, the diff of this file is not supported yet
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
876122
26800
4