Comparing version 2.0.3 to 2.0.4
{ | ||
"name": "ducky", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Duck-Typed Value Handling for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "./lib/ducky.js", |
@@ -10,9 +10,12 @@ { | ||
"no-alert": 2, | ||
"no-caller": 2, | ||
"no-array-constructor": 2, | ||
"no-bitwise": 0, | ||
"no-caller": 0, | ||
"no-catch-shadow": 2, | ||
"no-comma-dangle": 2, | ||
"no-cond-assign": 2, | ||
"no-console": 0, | ||
"no-comma-dangle": 2, | ||
"no-control-regex": 2, | ||
"no-debugger": 2, | ||
"no-delete-var": 2, | ||
"no-div-regex": 0, | ||
@@ -23,49 +26,65 @@ "no-dupe-keys": 2, | ||
"no-empty-class": 2, | ||
"no-empty-label": 2, | ||
"no-eq-null": 0, | ||
"no-eval": 2, | ||
"no-ex-assign": 2, | ||
"no-extend-native": 2, | ||
"no-extra-boolean-cast": 2, | ||
"no-extra-parens": 0, | ||
"no-extra-semi": 2, | ||
"no-extra-strict": 2, | ||
"no-fallthrough": 2, | ||
"no-floating-decimal": 0, | ||
"no-func-assign": 2, | ||
"no-floating-decimal": 0, | ||
"no-global-strict": 2, | ||
"no-implied-eval": 2, | ||
"no-with": 2, | ||
"no-fallthrough": 2, | ||
"no-global-strict": 0, | ||
"no-unreachable": 2, | ||
"no-undef": 2, | ||
"no-undef-init": 2, | ||
"no-unused-expressions": 2, | ||
"no-octal": 2, | ||
"no-octal-escape": 2, | ||
"no-obj-calls": 2, | ||
"no-invalid-regexp": 2, | ||
"no-iterator": 2, | ||
"no-label-var": 2, | ||
"no-labels": 2, | ||
"no-lone-blocks": 2, | ||
"no-loop-func": 0, | ||
"no-mixed-requires": [0, false], | ||
"no-multi-str": 2, | ||
"no-new-wrappers": 2, | ||
"no-native-reassign": 2, | ||
"no-negated-in-lhs": 2, | ||
"no-nested-ternary": 0, | ||
"no-new": 2, | ||
"no-new-func": 0, | ||
"no-native-reassign": 2, | ||
"no-new-object": 2, | ||
"no-new-wrappers": 2, | ||
"no-obj-calls": 2, | ||
"no-octal": 2, | ||
"no-octal-escape": 2, | ||
"no-path-concat": 0, | ||
"no-plusplus": 0, | ||
"no-delete-var": 2, | ||
"no-process-exit": 2, | ||
"no-proto": 2, | ||
"no-redeclare": 2, | ||
"no-regex-spaces": 2, | ||
"no-return-assign": 2, | ||
"no-new-array": 0, | ||
"no-new-object": 2, | ||
"no-label-var": 2, | ||
"no-ternary": 0, | ||
"no-script-url": 2, | ||
"no-self-compare": 0, | ||
"no-shadow": 0, | ||
"no-shadow-restricted-names": 2, | ||
"no-spaced-func": 0, | ||
"no-sparse-arrays": 2, | ||
"no-sync": 0, | ||
"no-underscore-dangle": 2, | ||
"no-loop-func": 0, | ||
"no-empty-label": 2, | ||
"no-unused-vars": 2, | ||
"no-script-url": 2, | ||
"no-proto": 2, | ||
"no-iterator": 2, | ||
"no-mixed-requires": [0, false], | ||
"no-ternary": 0, | ||
"no-undef": 2, | ||
"no-undef-init": 2, | ||
"no-underscore-dangle": 0, | ||
"no-unreachable": 2, | ||
"no-unused-expressions": 2, | ||
"no-unused-vars": [2, "local"], | ||
"no-use-before-define": 2, | ||
"no-with": 2, | ||
"no-wrap-func": 2, | ||
"no-shadow": 2, | ||
"no-use-before-define": 2, | ||
"no-redeclare": 2, | ||
"no-yoda": 2, | ||
"block-scoped-var": 0, | ||
"brace-style": 0, | ||
"block-scoped-var": 0, | ||
"camelcase": 0, | ||
"complexity": [0, 11], | ||
"consistent-return": 2, | ||
"consistent-this": [0, "that"], | ||
@@ -75,5 +94,8 @@ "curly": 0, | ||
"eqeqeq": 2, | ||
"func-names": 0, | ||
"func-style": [0, "declaration"], | ||
"guard-for-in": 0, | ||
"max-depth": [0, 4], | ||
"max-len": [0, 80, 4], | ||
"max-nested-callbacks": [0, 2], | ||
"max-params": [0, 3], | ||
@@ -84,11 +106,13 @@ "max-statements": [0, 10], | ||
"one-var": 0, | ||
"quote-props": 0, | ||
"quotes": [2, "double"], | ||
"quote-props": 0, | ||
"radix": 0, | ||
"regex-spaces": 2, | ||
"semi": 2, | ||
"smarter-eqeqeq": 0, | ||
"sort-vars": 0, | ||
"space-infix-ops": 2, | ||
"space-return-throw-case": 2, | ||
"space-unary-word-ops": 0, | ||
"strict": 0, | ||
"unnecessary-strict": 0, | ||
"use-isnan": 2, | ||
"valid-jsdoc": 0, | ||
"wrap-iife": 0, | ||
@@ -95,0 +119,0 @@ "wrap-regex": 0 |
@@ -346,3 +346,3 @@ /*! | ||
ast = this.parse_any(token); | ||
else if (symbol.match(/^[A-Z][_a-zA-Z$0-9]*$/)) | ||
else if (symbol.match(/^[_a-zA-Z$][_a-zA-Z$0-9]*(?:\.[_a-zA-Z$][_a-zA-Z$0-9]*)*$/)) | ||
ast = this.parse_class(token); | ||
@@ -434,3 +434,3 @@ else | ||
var clazz = token.peek(); | ||
if (!clazz.match(/^[A-Z][_a-zA-Z$0-9]*$/)) | ||
if (!clazz.match(/^[_a-zA-Z$][_a-zA-Z$0-9]*(?:\.[_a-zA-Z$][_a-zA-Z$0-9]*)*$/)) | ||
throw new Error("validate: parse error: invalid class type \"" + clazz + "\""); | ||
@@ -437,0 +437,0 @@ token.skip(); |
@@ -24,2 +24,2 @@ /*! | ||
*/ | ||
!function(a,b,c){"function"==typeof define&&"undefined"!=typeof define.amd?define(b,function(){return c(a)}):"object"==typeof module&&"object"==typeof module.exports?module.exports=c(a):a[b]=c(a)}(this,"ducky",function(){var ducky={};ducky.version={major:2,minor:0,micro:3,date:20140125};var Token=function(){this.name="",this.text="",this.tokens=[],this.pos=0,this.len=0};Token.prototype={setName:function(a){this.name=a},setText:function(a){this.text=a},addToken:function(a,b,c,d,e){this.tokens.push({b1:a,b2:b,e2:c,e1:d,symbol:e}),this.len++},peek:function(a){if("undefined"==typeof a&&(a=0),a>=this.len)throw new Error(this.name+": parse error: not enough tokens");return this.tokens[this.pos+a].symbol},skip:function(a){if("undefined"==typeof a&&(a=1),a>this.len)throw new Error(this.name+": parse error: not enough tokens available to skip: "+this.ctx());this.pos+=a,this.len-=a},consume:function(a){if(this.len<=0)throw new Error(this.name+": parse error: no more tokens available to consume: "+this.ctx());if(this.tokens[this.pos].symbol!==a)throw new Error(this.name+': parse error: expected token symbol "'+a+'": '+this.ctx());this.pos++,this.len--},ctx:function(a){"undefined"==typeof a&&(a=78);var b=this.tokens[this.pos],c="<"+this.text.substr(b.b2,b.e2-b.b2+1)+">";c=this.text.substr(b.b1,b.b2-b.b1)+c,c+=this.text.substr(b.e2+1,b.e1-b.e2);var d=a-c.length;if(d>0){d=Math.floor(d/2);var e,f;if(this.pos>0){var g=0;for(e=this.pos-1;e>=0&&(b=this.tokens[e],f=this.text.substr(b.b1,b.e1-b.b1+1),g+=f.length,!(g>d));e--)c=f+c;e>0&&(c="[...]"+c)}if(this.len>1){var h=0;for(e=this.pos+1;e<this.pos+this.len&&(b=this.tokens[e],f=this.text.substr(b.b1,b.e1-b.b1+1),h+=f.length,!(h>d));e++)c+=f;e<this.pos+this.len&&(c+="[...]")}}return c=c.replace(/\r/,"\\r").replace(/\n/,"\\n").replace(/\t/,"\\t")}};var select_compile=function(a){for(var b,c=[],d=0,e=a;""!==e;){if(null!==(b=e.match(/^\s*(?:\.)?\s*([a-zA-Z$0-9_][a-zA-Z$0-9_:-]*)/)))c.push(b[1]);else if(null!==(b=e.match(/^\s*\[\s*(\d+|\*{1,2})\s*\]/)))c.push(b[1]);else if(null!==(b=e.match(/^\s*\[\s*"((?:\\"|.)*?)"\s*\]/)))c.push(b[1].replace(/\\"/g,'"'));else{if(null===(b=e.match(/^\s*\[\s*'((?:\\'|.)*?)'\s*\]/))){if(null!==(b=e.match(/^\s+$/)))break;throw new Error("select: parse error: invalid character at: "+a.substr(0,d)+"<"+e.substr(0,1)+">"+e.substr(1))}c.push(b[1].replace(/\\'/g,"'"))}d+=b[0].length,e=e.substr(b[0].length)}return c},select_execute=function(a,b){if(0===b.length){if(3===arguments.length)throw new Error("select: cannot set value on empty path");return a}for(var c=0;c<b.length-1;){if("object"!=typeof a)throw new Error("select: cannot further dereference: no more intermediate objects in path");a=a[b[c++]]}if("object"!=typeof a)throw new Error("select: cannot further dereference: no object at end of path");var d=a[b[c]];if(3===arguments.length){var e=arguments[2];void 0===e?a instanceof Array?a.splice(parseInt(b[c],10),1):delete a[b[c]]:a[b[c]]=e}return d},select_cache={};ducky.select=function(a,b,c){if(arguments.length<2)throw new Error('select: invalid number of arguments: "'+arguments.length+'" (minimum of 2 expected)');if(arguments.length>3)throw new Error('select: invalid number of arguments: "'+arguments.length+'" (maximum of 3 expected)');if("string"!=typeof b)throw new Error('select: invalid specification argument: "'+b+'" (string expected)');var d=select_cache[b];return"undefined"==typeof d&&(d=select_compile(b),select_cache[b]=d),2===arguments.length?select_execute(a,d):select_execute(a,d,c)},ducky.select.compile=function(a){if(1!==arguments.length)throw new Error('select: invalid number of arguments: "'+arguments.length+'" (exactly 1 expected)');if("string"!=typeof a)throw new Error('select: invalid specification argument: "'+a+'" (string expected)');return select_compile.apply(void 0,arguments)},ducky.select.execute=function(a,b){if(arguments.length<2)throw new Error('select: invalid number of arguments: "'+arguments.length+'" (minimum of 2 expected)');if(arguments.length>3)throw new Error('select: invalid number of arguments: "'+arguments.length+'" (maximum of 3 expected)');if(!("object"==typeof b&&b instanceof Array))throw new Error('select: invalid path argument: "'+b+'" (array expected)');return select_execute.apply(void 0,arguments)};var validate_tokenize=function(a){var b=new Token;b.setName("validate"),b.setText(a);for(var c,d=0;""!==a;){if(c=a.match(/^(\s*)([^{}\[\]:,?*+()!|\s]+|[{}\[\]:,?*+()!|])(\s*)/),null===c)throw new Error('validate: parse error: cannot further canonicalize: "'+a+'"');b.addToken(d,d+c[1].length,d+c[1].length+c[2].length-1,d+c[0].length-1,c[2]),a=a.substr(c[0].length),d+=c[0].length}return b},validate_parse={parse_spec:function(a){if(a.len<=0)return null;var b,c=a.peek();if("!"===c)b=this.parse_not(a);else if("("===c)b=this.parse_group(a);else if("{"===c)b=this.parse_hash(a);else if("["===c)b=this.parse_array(a);else if(c.match(/^(?:null|undefined|boolean|number|string|function|object)$/))b=this.parse_primary(a);else if("any"===c)b=this.parse_any(a);else{if(!c.match(/^[A-Z][_a-zA-Z$0-9]*$/))throw new Error('validate: parse error: invalid token symbol: "'+a.ctx()+'"');b=this.parse_class(a)}return b},parse_not:function(a){a.consume("!");var b=this.parse_spec(a);return b={type:"not",op:b}},parse_group:function(a){a.consume("(");for(var b=this.parse_spec(a);"|"===a.peek();){a.consume("|");var c=this.parse_spec(a);b={type:"or",op1:b,op2:c}}return a.consume(")"),b},parse_hash:function(a){a.consume("{");for(var b=[];"}"!==a.peek();){var c=this.parse_key(a),d=this.parse_arity(a,"?");a.consume(":");var e=this.parse_spec(a);if(b.push({type:"element",key:c,arity:d,element:e}),","!==a.peek())break;a.skip()}var f={type:"hash",elements:b};return a.consume("}"),f},parse_array:function(a){a.consume("[");for(var b=[];"]"!==a.peek();){var c=this.parse_spec(a),d=this.parse_arity(a,"?*+");if(b.push({type:"element",element:c,arity:d}),","!==a.peek())break;a.skip()}var e={type:"array",elements:b};return a.consume("]"),e},parse_primary:function(a){var b=a.peek();if(!b.match(/^(?:null|undefined|boolean|number|string|function|object)$/))throw new Error('validate: parse error: invalid primary type "'+b+'"');return a.skip(),{type:"primary",name:b}},parse_any:function(a){var b=a.peek();if("any"!==b)throw new Error('validate: parse error: invalid any type "'+b+'"');return a.skip(),{type:"any"}},parse_class:function(a){var b=a.peek();if(!b.match(/^[A-Z][_a-zA-Z$0-9]*$/))throw new Error('validate: parse error: invalid class type "'+b+'"');return a.skip(),{type:"class",name:b}},parse_arity:function(a,b){var c=[1,1];if(a.len>=5&&"{"===a.peek(0)&&a.peek(1).match(/^[0-9]+$/)&&","===a.peek(2)&&a.peek(3).match(/^(?:[0-9]+|oo)$/)&&"}"===a.peek(4))c=[parseInt(a.peek(1),10),"oo"===a.peek(3)?Number.MAX_VALUE:parseInt(a.peek(3),10)],a.skip(5);else if(a.len>=1&&1===a.peek().length&&b.indexOf(a.peek())>=0){var d=a.peek();switch(d){case"?":c=[0,1];break;case"*":c=[0,Number.MAX_VALUE];break;case"+":c=[1,Number.MAX_VALUE]}a.skip()}return c},parse_key:function(a){var b=a.peek();if(!b.match(/^(?:[_a-zA-Z$][_a-zA-Z$0-9]*|@)$/))throw new Error('validate: parse error: invalid key "'+b+'"');return a.skip(),b}},validate_execute={exec_spec:function(a,b){var c=!1;if(null!==b)switch(b.type){case"not":c=this.exec_not(a,b);break;case"or":c=this.exec_or(a,b);break;case"hash":c=this.exec_hash(a,b);break;case"array":c=this.exec_array(a,b);break;case"primary":c=this.exec_primary(a,b);break;case"class":c=this.exec_class(a,b);break;case"any":c=!0;break;default:throw new Error('validate: invalid validation AST: node has unknown type "'+b.type+'"')}return c},exec_not:function(a,b){return!this.exec_spec(a,b.op)},exec_or:function(a,b){return this.exec_spec(a,b.op1)||this.exec_spec(a,b.op2)},exec_hash:function(a,b){var c,d,e,f="object"==typeof a,g={};if(f){var h=!1;for(e in a)if(Object.hasOwnProperty.call(a,e)&&Object.propertyIsEnumerable.call(a,e)&&"constructor"!==e&&"prototype"!==e){h=!0;break}for(c=0;c<b.elements.length;c++)if(d=b.elements[c],g[d.key]=d.element,d.arity[0]>0&&("@"===d.key&&!h||"@"!==d.key&&"undefined"==typeof a[d.key])){f=!1;break}}if(f)for(e in a)if(Object.hasOwnProperty.call(a,e)&&Object.propertyIsEnumerable.call(a,e)&&"constructor"!==e&&"prototype"!==e&&!("undefined"!=typeof g[e]&&this.exec_spec(a[e],g[e])||"undefined"!=typeof g["@"]&&this.exec_spec(a[e],g["@"]))){f=!1;break}return f},exec_array:function(a,b){var c,d,e="object"==typeof a&&a instanceof Array;if(e){var f=0;for(c=0;c<b.elements.length;c++){d=b.elements[c];for(var g=0;g<d.arity[1]&&f<a.length&&this.exec_spec(a[f],d.element);)g++,f++;if(g<d.arity[0]){e=!1;break}}f<a.length&&(e=!1)}return e},exec_primary:function(a,b){return"null"===b.name&&null===a||typeof a===b.name},exec_class:function(value,node){return"object"==typeof value&&Object.prototype.toString.call(value)==="[object "+node.name+"]"||eval("value instanceof "+node.name)}},validate_cache={};ducky.validate=function(a,b){if(2!==arguments.length)throw new Error('validate: invalid number of arguments: "'+arguments.length+'" (exactly 2 expected)');if("string"!=typeof b)throw new Error('validate: invalid specification argument: "'+b+'" (string expected)');var c=validate_cache[b];return"undefined"==typeof c&&(c=ducky.validate.compile(b),validate_cache[b]=c),ducky.validate.execute(a,c)},ducky.validate.compile=function(a){if(1!==arguments.length)throw new Error('validate: invalid number of arguments: "'+arguments.length+'" (exactly 1 expected)');if("string"!=typeof a)throw new Error('validate: invalid specification argument: "'+a+'" (string expected)');var b=validate_tokenize(a),c=validate_parse.parse_spec(b);return c},ducky.validate.execute=function(a,b){if(2!==arguments.length)throw new Error('validate: invalid number of arguments: "'+arguments.length+'" (exactly 2 expected)');return validate_execute.exec_spec(a,b)};var params_is_name_based=function(a,b){var c=!1;if(1===a.length&&"object"==typeof a[0]){c=!0;for(var d in a[0])Object.hasOwnProperty.call(a[0],d)||"undefined"==typeof b[d]&&(c=!1)}return c},params_check_validity=function(a,b,c,d,e){if("undefined"!=typeof d&&!ducky.validate(c,d))throw new Error(a+': parameter "'+b+'" has '+e+" "+JSON.stringify(c)+', which does not validate against "'+d+'"')};return ducky.params=function(a,b,c){var d,e={};for(d in c)Object.hasOwnProperty.call(c,d)&&"undefined"!=typeof c[d].def&&("undefined"!=typeof c[d].valid&¶ms_check_validity(a,d,c[d].def,c[d].valid,"default value"),e[d]=c[d].def);if(params_is_name_based(b,c)){b=b[0];for(d in b)if(Object.hasOwnProperty.call(b,d)){if("undefined"==typeof c[d])throw new Error(a+': unknown parameter "'+d+'"');params_check_validity(a,d,b[d],c[d].valid,"value"),e[d]=b[d]}for(d in c)if(Object.hasOwnProperty.call(c,d)&&"undefined"!=typeof c[d].req&&c[d].req&&"undefined"==typeof b[d])throw new Error(a+': required parameter "'+d+'" missing')}else{var f=0,g=0,h={};for(d in c)Object.hasOwnProperty.call(c,d)&&"undefined"!=typeof c[d].pos&&(h[c[d].pos]=d,"number"==typeof c[d].pos&&f++,"undefined"!=typeof c[d].req&&c[d].req&&g++);if(b.length<g)throw new Error(a+": invalid number of arguments (at least "+g+" required)");for(var i=0;f>i&&i<b.length;)params_check_validity(a,h[i],b[i],c[h[i]].valid,"value"),e[h[i]]=b[i],i++;if(i<b.length){if("undefined"==typeof h["..."])throw new Error(a+": too many arguments provided");for(var j=[];i<b.length;)j.push(b[i++]);params_check_validity(a,h["..."],j,c[h["..."]].valid,"value"),e[h["..."]]=j}}return e},ducky}); | ||
!function(a,b,c){"function"==typeof define&&"undefined"!=typeof define.amd?define(b,function(){return c(a)}):"object"==typeof module&&"object"==typeof module.exports?module.exports=c(a):a[b]=c(a)}(this,"ducky",function(){var ducky={};ducky.version={major:2,minor:0,micro:3,date:20140125};var Token=function(){this.name="",this.text="",this.tokens=[],this.pos=0,this.len=0};Token.prototype={setName:function(a){this.name=a},setText:function(a){this.text=a},addToken:function(a,b,c,d,e){this.tokens.push({b1:a,b2:b,e2:c,e1:d,symbol:e}),this.len++},peek:function(a){if("undefined"==typeof a&&(a=0),a>=this.len)throw new Error(this.name+": parse error: not enough tokens");return this.tokens[this.pos+a].symbol},skip:function(a){if("undefined"==typeof a&&(a=1),a>this.len)throw new Error(this.name+": parse error: not enough tokens available to skip: "+this.ctx());this.pos+=a,this.len-=a},consume:function(a){if(this.len<=0)throw new Error(this.name+": parse error: no more tokens available to consume: "+this.ctx());if(this.tokens[this.pos].symbol!==a)throw new Error(this.name+': parse error: expected token symbol "'+a+'": '+this.ctx());this.pos++,this.len--},ctx:function(a){"undefined"==typeof a&&(a=78);var b=this.tokens[this.pos],c="<"+this.text.substr(b.b2,b.e2-b.b2+1)+">";c=this.text.substr(b.b1,b.b2-b.b1)+c,c+=this.text.substr(b.e2+1,b.e1-b.e2);var d=a-c.length;if(d>0){d=Math.floor(d/2);var e,f;if(this.pos>0){var g=0;for(e=this.pos-1;e>=0&&(b=this.tokens[e],f=this.text.substr(b.b1,b.e1-b.b1+1),g+=f.length,!(g>d));e--)c=f+c;e>0&&(c="[...]"+c)}if(this.len>1){var h=0;for(e=this.pos+1;e<this.pos+this.len&&(b=this.tokens[e],f=this.text.substr(b.b1,b.e1-b.b1+1),h+=f.length,!(h>d));e++)c+=f;e<this.pos+this.len&&(c+="[...]")}}return c=c.replace(/\r/,"\\r").replace(/\n/,"\\n").replace(/\t/,"\\t")}};var select_compile=function(a){for(var b,c=[],d=0,e=a;""!==e;){if(null!==(b=e.match(/^\s*(?:\.)?\s*([a-zA-Z$0-9_][a-zA-Z$0-9_:-]*)/)))c.push(b[1]);else if(null!==(b=e.match(/^\s*\[\s*(\d+|\*{1,2})\s*\]/)))c.push(b[1]);else if(null!==(b=e.match(/^\s*\[\s*"((?:\\"|.)*?)"\s*\]/)))c.push(b[1].replace(/\\"/g,'"'));else{if(null===(b=e.match(/^\s*\[\s*'((?:\\'|.)*?)'\s*\]/))){if(null!==(b=e.match(/^\s+$/)))break;throw new Error("select: parse error: invalid character at: "+a.substr(0,d)+"<"+e.substr(0,1)+">"+e.substr(1))}c.push(b[1].replace(/\\'/g,"'"))}d+=b[0].length,e=e.substr(b[0].length)}return c},select_execute=function(a,b){if(0===b.length){if(3===arguments.length)throw new Error("select: cannot set value on empty path");return a}for(var c=0;c<b.length-1;){if("object"!=typeof a)throw new Error("select: cannot further dereference: no more intermediate objects in path");a=a[b[c++]]}if("object"!=typeof a)throw new Error("select: cannot further dereference: no object at end of path");var d=a[b[c]];if(3===arguments.length){var e=arguments[2];void 0===e?a instanceof Array?a.splice(parseInt(b[c],10),1):delete a[b[c]]:a[b[c]]=e}return d},select_cache={};ducky.select=function(a,b,c){if(arguments.length<2)throw new Error('select: invalid number of arguments: "'+arguments.length+'" (minimum of 2 expected)');if(arguments.length>3)throw new Error('select: invalid number of arguments: "'+arguments.length+'" (maximum of 3 expected)');if("string"!=typeof b)throw new Error('select: invalid specification argument: "'+b+'" (string expected)');var d=select_cache[b];return"undefined"==typeof d&&(d=select_compile(b),select_cache[b]=d),2===arguments.length?select_execute(a,d):select_execute(a,d,c)},ducky.select.compile=function(a){if(1!==arguments.length)throw new Error('select: invalid number of arguments: "'+arguments.length+'" (exactly 1 expected)');if("string"!=typeof a)throw new Error('select: invalid specification argument: "'+a+'" (string expected)');return select_compile.apply(void 0,arguments)},ducky.select.execute=function(a,b){if(arguments.length<2)throw new Error('select: invalid number of arguments: "'+arguments.length+'" (minimum of 2 expected)');if(arguments.length>3)throw new Error('select: invalid number of arguments: "'+arguments.length+'" (maximum of 3 expected)');if(!("object"==typeof b&&b instanceof Array))throw new Error('select: invalid path argument: "'+b+'" (array expected)');return select_execute.apply(void 0,arguments)};var validate_tokenize=function(a){var b=new Token;b.setName("validate"),b.setText(a);for(var c,d=0;""!==a;){if(c=a.match(/^(\s*)([^{}\[\]:,?*+()!|\s]+|[{}\[\]:,?*+()!|])(\s*)/),null===c)throw new Error('validate: parse error: cannot further canonicalize: "'+a+'"');b.addToken(d,d+c[1].length,d+c[1].length+c[2].length-1,d+c[0].length-1,c[2]),a=a.substr(c[0].length),d+=c[0].length}return b},validate_parse={parse_spec:function(a){if(a.len<=0)return null;var b,c=a.peek();if("!"===c)b=this.parse_not(a);else if("("===c)b=this.parse_group(a);else if("{"===c)b=this.parse_hash(a);else if("["===c)b=this.parse_array(a);else if(c.match(/^(?:null|undefined|boolean|number|string|function|object)$/))b=this.parse_primary(a);else if("any"===c)b=this.parse_any(a);else{if(!c.match(/^[_a-zA-Z$][_a-zA-Z$0-9]*(?:\.[_a-zA-Z$][_a-zA-Z$0-9]*)*$/))throw new Error('validate: parse error: invalid token symbol: "'+a.ctx()+'"');b=this.parse_class(a)}return b},parse_not:function(a){a.consume("!");var b=this.parse_spec(a);return b={type:"not",op:b}},parse_group:function(a){a.consume("(");for(var b=this.parse_spec(a);"|"===a.peek();){a.consume("|");var c=this.parse_spec(a);b={type:"or",op1:b,op2:c}}return a.consume(")"),b},parse_hash:function(a){a.consume("{");for(var b=[];"}"!==a.peek();){var c=this.parse_key(a),d=this.parse_arity(a,"?");a.consume(":");var e=this.parse_spec(a);if(b.push({type:"element",key:c,arity:d,element:e}),","!==a.peek())break;a.skip()}var f={type:"hash",elements:b};return a.consume("}"),f},parse_array:function(a){a.consume("[");for(var b=[];"]"!==a.peek();){var c=this.parse_spec(a),d=this.parse_arity(a,"?*+");if(b.push({type:"element",element:c,arity:d}),","!==a.peek())break;a.skip()}var e={type:"array",elements:b};return a.consume("]"),e},parse_primary:function(a){var b=a.peek();if(!b.match(/^(?:null|undefined|boolean|number|string|function|object)$/))throw new Error('validate: parse error: invalid primary type "'+b+'"');return a.skip(),{type:"primary",name:b}},parse_any:function(a){var b=a.peek();if("any"!==b)throw new Error('validate: parse error: invalid any type "'+b+'"');return a.skip(),{type:"any"}},parse_class:function(a){var b=a.peek();if(!b.match(/^[_a-zA-Z$][_a-zA-Z$0-9]*(?:\.[_a-zA-Z$][_a-zA-Z$0-9]*)*$/))throw new Error('validate: parse error: invalid class type "'+b+'"');return a.skip(),{type:"class",name:b}},parse_arity:function(a,b){var c=[1,1];if(a.len>=5&&"{"===a.peek(0)&&a.peek(1).match(/^[0-9]+$/)&&","===a.peek(2)&&a.peek(3).match(/^(?:[0-9]+|oo)$/)&&"}"===a.peek(4))c=[parseInt(a.peek(1),10),"oo"===a.peek(3)?Number.MAX_VALUE:parseInt(a.peek(3),10)],a.skip(5);else if(a.len>=1&&1===a.peek().length&&b.indexOf(a.peek())>=0){var d=a.peek();switch(d){case"?":c=[0,1];break;case"*":c=[0,Number.MAX_VALUE];break;case"+":c=[1,Number.MAX_VALUE]}a.skip()}return c},parse_key:function(a){var b=a.peek();if(!b.match(/^(?:[_a-zA-Z$][_a-zA-Z$0-9]*|@)$/))throw new Error('validate: parse error: invalid key "'+b+'"');return a.skip(),b}},validate_execute={exec_spec:function(a,b){var c=!1;if(null!==b)switch(b.type){case"not":c=this.exec_not(a,b);break;case"or":c=this.exec_or(a,b);break;case"hash":c=this.exec_hash(a,b);break;case"array":c=this.exec_array(a,b);break;case"primary":c=this.exec_primary(a,b);break;case"class":c=this.exec_class(a,b);break;case"any":c=!0;break;default:throw new Error('validate: invalid validation AST: node has unknown type "'+b.type+'"')}return c},exec_not:function(a,b){return!this.exec_spec(a,b.op)},exec_or:function(a,b){return this.exec_spec(a,b.op1)||this.exec_spec(a,b.op2)},exec_hash:function(a,b){var c,d,e,f="object"==typeof a,g={};if(f){var h=!1;for(e in a)if(Object.hasOwnProperty.call(a,e)&&Object.propertyIsEnumerable.call(a,e)&&"constructor"!==e&&"prototype"!==e){h=!0;break}for(c=0;c<b.elements.length;c++)if(d=b.elements[c],g[d.key]=d.element,d.arity[0]>0&&("@"===d.key&&!h||"@"!==d.key&&"undefined"==typeof a[d.key])){f=!1;break}}if(f)for(e in a)if(Object.hasOwnProperty.call(a,e)&&Object.propertyIsEnumerable.call(a,e)&&"constructor"!==e&&"prototype"!==e&&!("undefined"!=typeof g[e]&&this.exec_spec(a[e],g[e])||"undefined"!=typeof g["@"]&&this.exec_spec(a[e],g["@"]))){f=!1;break}return f},exec_array:function(a,b){var c,d,e="object"==typeof a&&a instanceof Array;if(e){var f=0;for(c=0;c<b.elements.length;c++){d=b.elements[c];for(var g=0;g<d.arity[1]&&f<a.length&&this.exec_spec(a[f],d.element);)g++,f++;if(g<d.arity[0]){e=!1;break}}f<a.length&&(e=!1)}return e},exec_primary:function(a,b){return"null"===b.name&&null===a||typeof a===b.name},exec_class:function(value,node){return"object"==typeof value&&Object.prototype.toString.call(value)==="[object "+node.name+"]"||eval("value instanceof "+node.name)}},validate_cache={};ducky.validate=function(a,b){if(2!==arguments.length)throw new Error('validate: invalid number of arguments: "'+arguments.length+'" (exactly 2 expected)');if("string"!=typeof b)throw new Error('validate: invalid specification argument: "'+b+'" (string expected)');var c=validate_cache[b];return"undefined"==typeof c&&(c=ducky.validate.compile(b),validate_cache[b]=c),ducky.validate.execute(a,c)},ducky.validate.compile=function(a){if(1!==arguments.length)throw new Error('validate: invalid number of arguments: "'+arguments.length+'" (exactly 1 expected)');if("string"!=typeof a)throw new Error('validate: invalid specification argument: "'+a+'" (string expected)');var b=validate_tokenize(a),c=validate_parse.parse_spec(b);return c},ducky.validate.execute=function(a,b){if(2!==arguments.length)throw new Error('validate: invalid number of arguments: "'+arguments.length+'" (exactly 2 expected)');return validate_execute.exec_spec(a,b)};var params_is_name_based=function(a,b){var c=!1;if(1===a.length&&"object"==typeof a[0]){c=!0;for(var d in a[0])Object.hasOwnProperty.call(a[0],d)||"undefined"==typeof b[d]&&(c=!1)}return c},params_check_validity=function(a,b,c,d,e){if("undefined"!=typeof d&&!ducky.validate(c,d))throw new Error(a+': parameter "'+b+'" has '+e+" "+JSON.stringify(c)+', which does not validate against "'+d+'"')};return ducky.params=function(a,b,c){var d,e={};for(d in c)Object.hasOwnProperty.call(c,d)&&"undefined"!=typeof c[d].def&&("undefined"!=typeof c[d].valid&¶ms_check_validity(a,d,c[d].def,c[d].valid,"default value"),e[d]=c[d].def);if(params_is_name_based(b,c)){b=b[0];for(d in b)if(Object.hasOwnProperty.call(b,d)){if("undefined"==typeof c[d])throw new Error(a+': unknown parameter "'+d+'"');params_check_validity(a,d,b[d],c[d].valid,"value"),e[d]=b[d]}for(d in c)if(Object.hasOwnProperty.call(c,d)&&"undefined"!=typeof c[d].req&&c[d].req&&"undefined"==typeof b[d])throw new Error(a+': required parameter "'+d+'" missing')}else{var f=0,g=0,h={};for(d in c)Object.hasOwnProperty.call(c,d)&&"undefined"!=typeof c[d].pos&&(h[c[d].pos]=d,"number"==typeof c[d].pos&&f++,"undefined"!=typeof c[d].req&&c[d].req&&g++);if(b.length<g)throw new Error(a+": invalid number of arguments (at least "+g+" required)");for(var i=0;f>i&&i<b.length;)params_check_validity(a,h[i],b[i],c[h[i]].valid,"value"),e[h[i]]=b[i],i++;if(i<b.length){if("undefined"==typeof h["..."])throw new Error(a+": too many arguments provided");for(var j=[];i<b.length;)j.push(b[i++]);params_check_validity(a,h["..."],j,c[h["..."]].valid,"value"),e[h["..."]]=j}}return e},ducky}); |
@@ -5,3 +5,3 @@ { | ||
"description": "Duck-Typed Value Handling for JavaScript", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"license": "MIT", | ||
@@ -25,11 +25,11 @@ "author": { | ||
"devDependencies": { | ||
"grunt": "~0.4.2", | ||
"grunt": "~0.4.4", | ||
"grunt-cli": "~0.1.13", | ||
"grunt-contrib-jshint": "~0.8.0", | ||
"grunt-contrib-uglify": "~0.3.2", | ||
"grunt-contrib-jshint": "~0.10.0", | ||
"grunt-contrib-uglify": "~0.4.0", | ||
"grunt-contrib-clean": "~0.5.0", | ||
"grunt-eslint": "~0.2.0", | ||
"grunt-mocha-test": "~0.8.2", | ||
"grunt-eslint": "~0.4.0", | ||
"grunt-mocha-test": "~0.10.2", | ||
"grunt-expand-include": "~0.9.6", | ||
"chai": "~1.8.1", | ||
"chai": "~1.9.1", | ||
"chai-fuzzy": "~1.4.0" | ||
@@ -36,0 +36,0 @@ }, |
@@ -110,3 +110,3 @@ | ||
primary | ::= | `/^(?:null|undefined|boolean|number|string|function|object)$/` | ||
class | ::= | `/^[A-Z][_a-zA-Z$0-9]\*$/` | ||
class | ::= | `/^[_a-zA-Z$][_a-zA-Z$0-9]\*(?:\.[_a-zA-Z$][_a-zA-Z$0-9]\*)\*$/` | ||
@@ -113,0 +113,0 @@ The special key `@` can be used to match an arbitrary hash element key. |
@@ -44,3 +44,3 @@ /* | ||
ast = this.parse_any(token); | ||
else if (symbol.match(/^[A-Z][_a-zA-Z$0-9]*$/)) | ||
else if (symbol.match(/^[_a-zA-Z$][_a-zA-Z$0-9]*(?:\.[_a-zA-Z$][_a-zA-Z$0-9]*)*$/)) | ||
ast = this.parse_class(token); | ||
@@ -132,3 +132,3 @@ else | ||
var clazz = token.peek(); | ||
if (!clazz.match(/^[A-Z][_a-zA-Z$0-9]*$/)) | ||
if (!clazz.match(/^[_a-zA-Z$][_a-zA-Z$0-9]*(?:\.[_a-zA-Z$][_a-zA-Z$0-9]*)*$/)) | ||
throw new Error("validate: parse error: invalid class type \"" + clazz + "\""); | ||
@@ -135,0 +135,0 @@ token.skip(); |
@@ -34,3 +34,3 @@ /* | ||
global.expect = global.chai.expect | ||
global.chai.Assertion.includeStack = true | ||
global.chai.config.includeStack = true | ||
@@ -37,0 +37,0 @@ var ducky = require("../lib/ducky.js") |
@@ -34,3 +34,3 @@ /* | ||
global.expect = global.chai.expect | ||
global.chai.Assertion.includeStack = true | ||
global.chai.config.includeStack = true | ||
@@ -37,0 +37,0 @@ var ducky = require("../lib/ducky.js") |
@@ -34,3 +34,3 @@ /* | ||
global.expect = global.chai.expect | ||
global.chai.Assertion.includeStack = true | ||
global.chai.config.includeStack = true | ||
@@ -37,0 +37,0 @@ var ducky = require("../lib/ducky.js") |
@@ -34,3 +34,3 @@ /* | ||
global.expect = global.chai.expect | ||
global.chai.Assertion.includeStack = true | ||
global.chai.config.includeStack = true | ||
@@ -37,0 +37,0 @@ var ducky = require("../lib/ducky.js") |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
118996
2269