-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalidate-hash.ts
More file actions
30 lines (27 loc) · 875 Bytes
/
validate-hash.ts
File metadata and controls
30 lines (27 loc) · 875 Bytes
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
import {BASE32} from '../utils';
/**
* Validates a Geohash and returns a boolean if valid, or throws an error if invalid.
*
* @param geohash The Geohash to be validated.
* @param flag Tells function to send up boolean if valid instead of throwing an error.
* @returns Boolean if Geohash is valid.
*/
export function validateHash(geohash: string, flag = false): boolean {
let error;
if (typeof geohash !== 'string') {
error = 'geohash must be a string';
} else if (geohash.length === 0) {
error = 'geohash cannot be the empty string';
} else {
for (const letter of geohash) {
if (BASE32.indexOf(letter) === -1) {
error = "geohash cannot contain '" + letter + "'";
}
}
}
if (typeof error !== 'undefined' && !flag) {
throw new Error("Invalid geohash '" + geohash + "': " + error);
} else {
return !error;
}
}