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

+50
View File
@@ -52,6 +52,19 @@ test('hemisphere letters fix the axis, in either order', () => {
label: '40.0000° N, 74.0000° W',
});
assert.deepEqual(parseCoordinateQuery('40° N, 74° W')?.lat, 40);
// Written the way a coordinate is usually pasted, with no comma.
assert.deepEqual(parseCoordinateQuery('40.7128 N 74.0060 W'), {
lat: 40.7128,
lon: -74.006,
label: '40.7128° N, 74.0060° W',
});
assert.deepEqual(parseCoordinateQuery('40.7128° N 74.0060° W')?.lon, -74.006);
assert.deepEqual(parseCoordinateQuery('N 40.7128 W 74.0060'), {
lat: 40.7128,
lon: -74.006,
label: '40.7128° N, 74.0060° W',
});
assert.deepEqual(parseCoordinateQuery('74.0060 W 40.7128 N')?.lat, 40.7128);
// One letter is enough: it fixes its own axis, the other takes what is left.
assert.deepEqual(parseCoordinateQuery('40 N, -74')?.lon, -74);
assert.deepEqual(parseCoordinateQuery('-74, 40 N'), {
@@ -66,6 +79,21 @@ test('malformed and ambiguous coordinates are refused, never guessed', () => {
// The reviewer's case: trailing text must not be discarded.
'12junk, 34oops',
'12junk,34',
'12junk 34',
'1e3 5',
'1e3',
'1E3',
'1e-3',
'1.2.3',
'12-34',
'40 NW 74',
'40 N 74',
'40 W 74',
'40 E 74',
'°40 74',
'40°° 74',
'40 74°°',
'40 26 46 N 79 58 56 W',
'43.1731, -79.0384x',
'abc, def',
'Paris',
@@ -196,3 +224,25 @@ test('malformed or unsupported geo URIs are refused without losing their meaning
assert.equal(parseCoordinateQuery(query), null, query);
}
});
test('whitespace boundaries preserve degree signs and hemisphere ownership', () => {
for (const query of ['40 ° 74', '40° 74', '40 N, 74', 'N 40 74']) {
assert.equal(parseCoordinateQuery(query)?.lat, 40, query);
assert.equal(parseCoordinateQuery(query)?.lon, 74, query);
}
for (const query of ['40 ° N 74 ° W', 'N 40 ° W 74 °', '40\tN\n74\tW']) {
assert.equal(parseCoordinateQuery(query)?.lat, 40, query);
assert.equal(parseCoordinateQuery(query)?.lon, -74, query);
}
});
test('a signed plain component leaves a spaced letter with the other component', () => {
assert.deepEqual(
parseCoordinateQuery('-40 N 74'),
parseCoordinateQuery('-40, N 74'),
);
assert.deepEqual(
parseCoordinateQuery('40 N -74'),
parseCoordinateQuery('40 N, -74'),
);
});