-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.js
More file actions
150 lines (141 loc) · 4.46 KB
/
Copy pathcheck.js
File metadata and controls
150 lines (141 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//TODO: add support for range operators and condition verbiage (/greater/ than or /equal/ to)
//TODO: certain checks should disable others (for instance, if can be cohersed to a number disable number check)
//TODO: add ability to return list of checks and pass or failure
window.arrg = window.arrg || {};
(function(arrg) {
"use strict";
var jsNumPat = "[-+]?(?:(?:\\d+)|(?:\\d+\\.\\d+)|(?:\\.\\d+))(?:e[-+]?\\d+)?",
jsCompPat = "(?:(?:>=?)|(?:<=?))",
jsCompRegex = new RegExp(jsCompPat),
types = {
"array": {
"reg": /arr/i,
"check": function(arg) { return arg instanceof Array; }
},
"object": {
"reg": /object/i,
"check": function(arg) { return typeof(arg) === 'object'; }
},
"string": {
"reg": /str/i,
"check": function(arg) { return typeof(arg) === 'string'; }
},
"arguments": {
"reg": /arg/i,
"check": function(arg) { return (Object.prototype.toString.call(arg).indexOf('rguments') > 0) }
},
"number": {
"reg": /num/i,
"check": function(arg) { return typeof(arg) === 'number'; }
},
"regexp": {
"reg": /regex/i,
"check": function(arg) { return arg instanceof RegExp; }
},
"integer": {
"reg": /int/i,
"check": function(arg) { return (arg >> 0) === arg; }
},
"float": {
"reg": /float/i,
"check": function(arg) { return (arg >> 0) !== arg; }
},
"natural": {
"reg": /nat/i,
"check": function(arg) { return (types["integer"].check(arg) && arg > 0); }
},
"positive": {
"reg": /pos/i,
"check": function(arg) { return arg > 0; }
},
"whole": {
"reg": /whole/i,
"check": function(arg) { return (types["integer"].check(arg) && arg > -1); }
},
"negative": {
"reg": /neg/i,
"check": function(arg) { return arg < 0; }
},
"between": {
"reg": /between(-?[0-9]+)And(-?[0-9]+)/i,
"match": true,
"check": function(arg,match) { return false; }
},
"num-comparison": {
"reg": new RegExp(jsCompPat+'\\s*('+jsNumPat+')|('+jsNumPat+')\\s*'+jsCompPat),
"match": true,
"check": function(arg,match) {
//console.log(match);
var comp = match[0].match(jsCompRegex)[0],
lhs = (match[1]) ? true : false, //-- test if arg should go on left hand side of oper
ret = false,
log = "";
switch(comp) {
case ">":
ret = (lhs) ? (arg > match[1]) : (match[2] > arg);
break;
case ">=":
ret = (lhs) ? (arg >= match[1]) : (match[2] >= arg);
break;
case "<":
ret = (lhs) ? (arg < match[1]) : (match[2] < arg);
break;
case "<=":
ret = (lhs) ? (arg <= match[1]) : (match[2] <= arg);
break;
}
log = (ret) ? "[ ok ] arrg: compare " : "[fail] arrg: compare ";
log += (lhs) ? "("+arg+" "+comp+" "+match[1]+")" : "("+match[2]+" "+comp+" "+arg+")";
(ret) ? console.info(log) : console.error(log);
return ret;
}
},
"num-range-verbal": {
"reg": new RegExp('(?:between|from)\\s*('+jsNumPat+')\\s*(inclusive|exclusive)?\\s*(?:and|to)\\s*('+jsNumPat+')\\s*(inclusive|exclusive)?'),
"match": true,
"check": function(arg, match) {
var pass = true;
//console.log(match);
pass = (types["num-comparison"].check(arg, [">"+(/incl/i.test(match[2]) ? '=':'')+match[1],match[1],null])) ? pass : false;
pass = (types["num-comparison"].check(arg, ["<"+(/incl/i.test(match[4]) ? '=':'')+match[3],match[3],null])) ? pass : false;
return pass;
}
},
"num-compare-verbal": {
"reg": /blahblahblah/,
"check": function() {return true;}
}
};
//-- for internal use when I know which ref - doesn't work for conditionals types
function checkType(typeRef, arg) {
return types[typeRef].check(arg);
}
function checkTypes(typeStr, arg) {
var pass = true,
check = true,
curr = null;
console.info("arrg: checking if arg("+arg+") obeys \""+typeStr+"\"");
for(var i in types) {
curr = types[i];
//console.log(i);
if(curr.reg && curr.reg.test(typeStr)) {
// the comparison checks need the match information
check = (curr.match) ? curr.check(arg, typeStr.match(curr.reg)) : curr.check(arg);
(check) ?
console.info("[ ok ] arrg: typeCheck("+i+")") :
console.error("[fail] arrg: typeCheck("+i+")");
pass = (check) ? pass : false;
}
//if(!check) { break; } // bail if a check fails - needs to be optional
}
return pass;
}
//-- add to global namespace
arrg.check = checkTypes;
arrg.Types = types;
})(window.arrg);
if (typeof define === 'function' && define.amd) {
define(function () {
return window.arrg;
});
}