Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling of lat, lon coordinate queries #368

Merged
merged 15 commits into from
Oct 4, 2023
17 changes: 15 additions & 2 deletions src/SearchControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import SearchElement from './SearchElement';
import ResultList from './resultList';
import debounce from './lib/debounce';
import { validateCoords } from './coords';

import {
createElement,
addClassName,
removeClassName,
stopPropagation,

Check warning on line 12 in src/SearchControl.ts

View workflow job for this annotation

GitHub Actions / Eslint

'stopPropagation' is defined but never used
} from './domUtils';
import {
ENTER_KEY,
Expand All @@ -17,7 +18,7 @@
ARROW_DOWN_KEY,
ESCAPE_KEY,
} from './constants';
import AbstractProvider, { SearchResult } from './providers/provider';

Check warning on line 21 in src/SearchControl.ts

View workflow job for this annotation

GitHub Actions / Eslint

'AbstractProvider' is defined but never used
import { Provider } from './providers';

const defaultOptions: Omit<SearchControlProps, 'provider'> = {
Expand Down Expand Up @@ -282,7 +283,7 @@
);

this.container.appendChild(this.searchElement.form);
root!.appendChild(this.container);

Check warning on line 286 in src/SearchControl.ts

View workflow job for this annotation

GitHub Actions / Eslint

Forbidden non-null assertion
}

L.DomEvent.disableClickPropagation(this.searchElement.form);
Expand Down Expand Up @@ -373,7 +374,13 @@
const { provider } = this.options;

if (query.length) {
let results = await provider!.search({ query });
let results = [];
const coords = validateCoords(query);
if (coords) {
results = coords;
} else {
results = await provider!.search({ query });

Check warning on line 382 in src/SearchControl.ts

View workflow job for this annotation

GitHub Actions / Eslint

Forbidden non-null assertion
}
results = results.slice(0, this.options.maxSuggestions);
this.resultList.render(results, this.options.resultFormat);
} else {
Expand All @@ -385,7 +392,13 @@
this.resultList.clear();
const { provider } = this.options;

const results = await provider!.search(query);
let results = [];
const coords = validateCoords(query);
if (coords) {
results = coords;
} else {
results = await provider!.search(query);

Check warning on line 400 in src/SearchControl.ts

View workflow job for this annotation

GitHub Actions / Eslint

Forbidden non-null assertion
}

if (results && results.length > 0) {
this.showResult(results[0], query);
Expand Down
21 changes: 21 additions & 0 deletions src/coords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @ts-nocheck

export function validateCoords(query) {
const q = query.trim();
const regex = /^(-?[0-9]*\.?\s*[0-9]*)\s*,?\s*(-?[0-9]*\.?[0-9]*)$/g;
const match = regex.exec(q);
if (match) {
let lat = Number(match[1]);

Check failure on line 8 in src/coords.ts

View workflow job for this annotation

GitHub Actions / Eslint

'lat' is never reassigned. Use 'const' instead
smeijer marked this conversation as resolved.
Show resolved Hide resolved
let lng = Number(match[2]);

Check failure on line 9 in src/coords.ts

View workflow job for this annotation

GitHub Actions / Eslint

'lng' is never reassigned. Use 'const' instead
smeijer marked this conversation as resolved.
Show resolved Hide resolved
if (( -90 < lat < 90) && ( -180 < lng < 180)) {

Check failure on line 10 in src/coords.ts

View workflow job for this annotation

GitHub Actions / Eslint

Replace `····if·((·-90·<·lat·<·90)·&&·(·-180·<·lng·<·180))` with `if·(-90·<·lat·<·90·&&·-180·<·lng·<·180)`
smeijer marked this conversation as resolved.
Show resolved Hide resolved
return [{

Check failure on line 11 in src/coords.ts

View workflow job for this annotation

GitHub Actions / Eslint

Replace `············return·[{` with `······return·[`
smeijer marked this conversation as resolved.
Show resolved Hide resolved
x: lng,

Check failure on line 12 in src/coords.ts

View workflow job for this annotation

GitHub Actions / Eslint

Replace `········` with `{⏎··········`
smeijer marked this conversation as resolved.
Show resolved Hide resolved
y: lat,

Check failure on line 13 in src/coords.ts

View workflow job for this annotation

GitHub Actions / Eslint

Replace `················` with `··········`
smeijer marked this conversation as resolved.
Show resolved Hide resolved
label: q,

Check failure on line 14 in src/coords.ts

View workflow job for this annotation

GitHub Actions / Eslint

Replace `················` with `··········`
smeijer marked this conversation as resolved.
Show resolved Hide resolved
bounds: null,

Check failure on line 15 in src/coords.ts

View workflow job for this annotation

GitHub Actions / Eslint

Replace `················` with `··········`
smeijer marked this conversation as resolved.
Show resolved Hide resolved
raw: {}

Check failure on line 16 in src/coords.ts

View workflow job for this annotation

GitHub Actions / Eslint

Replace `················raw:·{}` with `··········raw:·{},`
smeijer marked this conversation as resolved.
Show resolved Hide resolved
}];

Check failure on line 17 in src/coords.ts

View workflow job for this annotation

GitHub Actions / Eslint

Replace `····}` with `},⏎······`
smeijer marked this conversation as resolved.
Show resolved Hide resolved
}
smeijer marked this conversation as resolved.
Show resolved Hide resolved
}
smeijer marked this conversation as resolved.
Show resolved Hide resolved
return false;
smeijer marked this conversation as resolved.
Show resolved Hide resolved
}
Loading