Hex (cell) ID validation
Portur opened this issue · comments
Hi,
I need to define what a valid h3 index looks like before touching the library.
Ideally I'm looking for a javascript function approach like isValidID('8ff....')
.
The purpose is simply to validate the string
value within the bounds of h3
.
From my understanding, using the existing isValidCell
function would provide valid way to test this, however this would need the library.
Is there an approach to this without the library?
something like
function isValidHexID(string){
return string.match(\...\)
}
Thanks
Related uber/h3-js#79
There are a couple of levels you could check here, but I don't think you want to fully validate without the library.
- The simplest option is probably
typeof string === 'string' && /^8[0-9a-f]{14}$/.test(string)
- this just validates that the input string is a hexidecimal string of the appropriate length, and checks the first 8 bits for the expected value (which is the same for all cell indexes, but different for edge indexes or vertex indexes). - Beyond that, you'd need to validate that the different components of the index are valid together. This is complex, and probably not something you want to implement yourself unless absolutely necessary:
- Validate the base cell bits to be
0-121
- Determine the resolution of the cell
- Validate that the first
res
index digits (defined by 3-bit sequences) to be0-6
- Validate that the rest of the digits are
7
- Particularly tricky without the library - validate that, IFF any parent cell is a pentagon, we don't have any invalid
6
index digits in a child position. This requires knowing which twelve base cells are pentagons, and looking for the first non-pentagon child digit.
- Validate the base cell bits to be
Thank you very much for the explanation.
I think then I'm going to rely on the library to be safe.
Have a great day.