forked from benjaminplee/QuickPiet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
validations.js
47 lines (43 loc) · 1.19 KB
/
validations.js
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
Validations = {
given: function(args) {
if(!args) {
throw new SyntaxError("No arguments given!");
}
return true;
},
minLength: function(args, length) {
if(args.length < length) {
throw new SyntaxError("Expected '" + args + "' to be of length " + length);
}
return true;
},
numberSequence: function(args, length) {
var results = [];
jQuery.each(args.split(' '), function(index, value) {
var parsed = parseInt(value);
if(isNaN(parsed)) {
throw new SyntaxError("'" + args + "' is not composed of parseable integers");
}
results.push(parseInt(value));
});
if(length && results.length != length) {
var clause = length == 1 ? (length + " integer,") : (length + " integers,");
throw new SyntaxError("Expected " + clause + " but found " + results.length);
}
return results;
},
positive: function(values) {
jQuery.each(values, function(index, value) {
if(value < 0) {
throw new SyntaxError("Cannot give negative numbers");
}
});
},
invalidCharsCheck: function(args, re) {
var i = args.search(re);
if(i == -1) {
return true;
}
throw new SyntaxError("Invalid character '" + args[i] + "' in '" + args + "'");
}
};