You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For example: HY 2022 0003. It seems to fail at the second "sanity check" in OsGridRef.parse. The converted coordinates, if the sanity check removed, seem correct so I think it is just the sanity check which is wrong.
I'd suggest moving this check into the regular expression:
// validate format match = gridref.match(/^[HJNOST][A-Z]\s*[0-9]+\s*[0-9]+$/i); if (!match) throw new Error(invalid grid reference ‘${gridref}’`);
// get numeric values of letter references, mapping A->0, B->1, C->2, etc:
let l1 = gridref.toUpperCase().charCodeAt(0) - 'A'.charCodeAt(0);
let l2 = gridref.toUpperCase().charCodeAt(1) - 'A'.charCodeAt(0);
// shuffle down letters after 'I' since 'I' is not used in grid:
if (l1 > 7) l1--;
if (l2 > 7) l2--;
// convert grid letters into 100km-square indexes from false origin (grid square SV):
const e100km = ((l1 - 2) % 5) * 5 + (l2 % 5);
const n100km = (19 - Math.floor(l1 / 5) * 5) - Math.floor(l2 / 5);
`
This is not perfect since invalid grid references can sneak through (e.g. TC) but it is probably an improvement.
The text was updated successfully, but these errors were encountered:
Thanks for this. This has been lurking for a while, I guess there's not been so many users in Orkney / Shetland!
A more specific RegExp does seem the best way of doing this. Some meaningless watery grid references can still get through, but TC will be caught by the 'invalid easting' check in the constructor.
For example: HY 2022 0003. It seems to fail at the second "sanity check" in OsGridRef.parse. The converted coordinates, if the sanity check removed, seem correct so I think it is just the sanity check which is wrong.
I'd suggest moving this check into the regular expression:
// validate format match = gridref.match(/^[HJNOST][A-Z]\s*[0-9]+\s*[0-9]+$/i); if (!match) throw new Error(
invalid grid reference ‘${gridref}’`);`
This is not perfect since invalid grid references can sneak through (e.g. TC) but it is probably an improvement.
The text was updated successfully, but these errors were encountered: