fix(search): read hemisphere letters separated from the number (#1340)

Support spaced hemisphere coordinates while rejecting malformed numeric fragments and ambiguous component assignments. Include regression tests and runtime documentation.

Contribution by @rudycelekli.
This commit is contained in:
Rudy Mizrahi Celekli authored and GitHub committed 2026-10-24 23:39:51 -07:00
1 parent 5b8e6fbc38
commit b07352ea6a
4 files changed
+87 -6

No files matched your search

+28 -6
View File
@@ -47,15 +47,37 @@ function readComponent(text) {
};
}
/** Split on a comma, a semicolon, or whitespace — never on anything else. */
/**
* Split on a comma, a semicolon, or whitespace — never on anything else.
* A hemisphere letter stays with the number it names, so `40.7 N 74.0 W`
* is two components. Splitting on the space before that letter used to
* leave three pieces and the search declined the coordinate.
*/
function splitComponents(query) {
const text = String(query).trim();
if (!text) return null;
const parts = /[,;]/.test(text)
? text.split(/[,;]/)
: text.split(/\s+(?=[+-]?[.\d]|[NSEW]\s*[+-]?[.\d])/i);
const trimmed = parts.map((part) => part.trim());
return trimmed.length === 2 && trimmed.every(Boolean) ? trimmed : null;
if (/[,;]/.test(text)) {
const trimmed = text.split(/[,;]/).map((part) => part.trim());
return trimmed.length === 2 && trimmed.every(Boolean) ? trimmed : null;
}
const words = text.split(/\s+/);
// Each component has at most a number, a degree sign and one hemisphere.
if (words.length > 6) return null;
// Keep every character: extracting tokens would turn `1e3` or `1.2.3`
// into invented coordinates. A lone letter between numbers can name either
// component; require an unambiguous split rather than guessing its owner.
let result = null;
for (let boundary = words.length - 1; boundary > 0; boundary -= 1) {
const parts = [
words.slice(0, boundary).join(' '),
words.slice(boundary).join(' '),
];
if (parts.every((part) => readComponent(part))) {
if (result) return null;
result = parts;
}
}
return result;
}
/** Present a parsed pair the way a coordinate is normally written. */