-
Notifications
You must be signed in to change notification settings - Fork 9
/
validator.js
70 lines (60 loc) · 1.83 KB
/
validator.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Generated by CoffeeScript 2.5.1
var Validator;
Validator = class Validator {
isValid(coordinates) {
var isValid, validationError;
isValid = true;
try {
this.validate(coordinates);
return isValid;
} catch (error) {
validationError = error;
isValid = false;
return isValid;
}
}
validate(coordinates) {
this.checkContainsNoLetters(coordinates);
this.checkValidOrientation(coordinates);
return this.checkNumbers(coordinates);
}
checkContainsNoLetters(coordinates) {
var containsLetters;
containsLetters = /(?![neswd])[a-z]/i.test(coordinates);
if (containsLetters) {
throw new Error('Coordinate contains invalid alphanumeric characters.');
}
}
checkValidOrientation(coordinates) {
var validOrientation;
validOrientation = /^[^nsew]*[ns]?[^nsew]*[ew]?[^nsew]*$/i.test(coordinates);
if (!validOrientation) {
throw new Error('Invalid cardinal direction.');
}
}
checkNumbers(coordinates) {
var coordinateNumbers;
coordinateNumbers = coordinates.match(/-?\d+(\.\d+)?/g);
this.checkAnyCoordinateNumbers(coordinateNumbers);
this.checkEvenCoordinateNumbers(coordinateNumbers);
return this.checkMaximumCoordinateNumbers(coordinateNumbers);
}
checkAnyCoordinateNumbers(coordinateNumbers) {
if (coordinateNumbers.length === 0) {
throw new Error('Could not find any coordinate number');
}
}
checkEvenCoordinateNumbers(coordinateNumbers) {
var isUnevenNumbers;
isUnevenNumbers = coordinateNumbers.length % 2;
if (isUnevenNumbers) {
throw new Error('Uneven count of latitude/longitude numbers');
}
}
checkMaximumCoordinateNumbers(coordinateNumbers) {
if (coordinateNumbers.length > 6) {
throw new Error('Too many coordinate numbers');
}
}
};
module.exports = Validator;