From 5876c54c4cc7e5b8fbf34edaf0ee4992f3966f86 Mon Sep 17 00:00:00 2001 From: apl Date: Sat, 10 Jun 2023 16:43:36 +0800 Subject: [PATCH 01/13] handling , coordinate queries --- src/SearchControl.ts | 17 +++++++++++++++-- src/coords.ts | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/coords.ts diff --git a/src/SearchControl.ts b/src/SearchControl.ts index d26afc2f8..b696d1669 100644 --- a/src/SearchControl.ts +++ b/src/SearchControl.ts @@ -3,6 +3,7 @@ import { ControlPosition, FeatureGroup, MarkerOptions, Map } from 'leaflet'; import SearchElement from './SearchElement'; import ResultList from './resultList'; import debounce from './lib/debounce'; +import { validateCoords } from './coords'; import { createElement, @@ -373,7 +374,13 @@ const Control: SearchControl = { const { provider } = this.options; if (query.length) { - let results = await provider!.search({ query }); + var results = []; + const coords = validateCoords({query}); + if (coords) { + results = coords; + } else { + results = await provider!.search({query}); + } results = results.slice(0, this.options.maxSuggestions); this.resultList.render(results, this.options.resultFormat); } else { @@ -384,7 +391,13 @@ const Control: SearchControl = { async onSubmit(query) { const { provider } = this.options; - const results = await provider!.search(query); + var results = []; + const coords = validateCoords(query); + if (coords) { + results = coords; + } else { + results = await provider!.search(query); + } if (results && results.length > 0) { this.showResult(results[0], query); diff --git a/src/coords.ts b/src/coords.ts new file mode 100644 index 000000000..c9e9ab210 --- /dev/null +++ b/src/coords.ts @@ -0,0 +1,21 @@ +// @ts-nocheck + +export function validateCoords(query) { + const q = query.query.trim(); + const regex = /^(-?[0-9]*\.?\s*[0-9]*)\s*,?\s*(-?[0-9]*\.?[0-9]*)$/g; + var match = regex.exec(q); + if (match) { + var lat = Number(match[1]); + var lng = Number(match[2]); + if (( -90 < lat < 90) && ( -180 < lng < 180)) { + return [{ + x: lng, + y: lat, + label: q, + bounds: null, + raw: {} + }]; + } + } + return false; +} From dbb758935fba48045175b85086530f0960a8bc40 Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:16:08 +0200 Subject: [PATCH 02/13] Apply suggestions from code review --- src/SearchControl.ts | 2 +- src/coords.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SearchControl.ts b/src/SearchControl.ts index bdfed7641..460983f00 100644 --- a/src/SearchControl.ts +++ b/src/SearchControl.ts @@ -375,7 +375,7 @@ const Control: SearchControl = { if (query.length) { var results = []; - const coords = validateCoords({query}); + const coords = validateCoords(query); if (coords) { results = coords; } else { diff --git a/src/coords.ts b/src/coords.ts index c9e9ab210..bc5c8cdd7 100644 --- a/src/coords.ts +++ b/src/coords.ts @@ -1,7 +1,7 @@ // @ts-nocheck export function validateCoords(query) { - const q = query.query.trim(); + const q = query.trim(); const regex = /^(-?[0-9]*\.?\s*[0-9]*)\s*,?\s*(-?[0-9]*\.?[0-9]*)$/g; var match = regex.exec(q); if (match) { From 5da17fba06c286db338b2b18af77ce5f366bdc1e Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:30:06 +0200 Subject: [PATCH 03/13] Update src/SearchControl.ts --- src/SearchControl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SearchControl.ts b/src/SearchControl.ts index 460983f00..0a2995783 100644 --- a/src/SearchControl.ts +++ b/src/SearchControl.ts @@ -374,7 +374,7 @@ const Control: SearchControl = { const { provider } = this.options; if (query.length) { - var results = []; + const results = []; const coords = validateCoords(query); if (coords) { results = coords; From fe64548af1d5afb0ab3798a64a919bf140e29cac Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:30:12 +0200 Subject: [PATCH 04/13] Update src/SearchControl.ts --- src/SearchControl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SearchControl.ts b/src/SearchControl.ts index 0a2995783..ac906aea8 100644 --- a/src/SearchControl.ts +++ b/src/SearchControl.ts @@ -379,7 +379,7 @@ const Control: SearchControl = { if (coords) { results = coords; } else { - results = await provider!.search({query}); + results = await provider!.search({ query }); } results = results.slice(0, this.options.maxSuggestions); this.resultList.render(results, this.options.resultFormat); From 61697ac6cc3a714ee5499240d4609e20dc1652e5 Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:30:20 +0200 Subject: [PATCH 05/13] Update src/SearchControl.ts --- src/SearchControl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SearchControl.ts b/src/SearchControl.ts index ac906aea8..6dc2c915b 100644 --- a/src/SearchControl.ts +++ b/src/SearchControl.ts @@ -395,7 +395,7 @@ const Control: SearchControl = { var results = []; const coords = validateCoords(query); if (coords) { - results = coords; + results = coords; } else { results = await provider!.search(query); } From bad16a6cf9715c0f34e67708fe53acda7add9c63 Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:30:27 +0200 Subject: [PATCH 06/13] Update src/SearchControl.ts --- src/SearchControl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SearchControl.ts b/src/SearchControl.ts index 6dc2c915b..3018dfd91 100644 --- a/src/SearchControl.ts +++ b/src/SearchControl.ts @@ -392,7 +392,7 @@ const Control: SearchControl = { this.resultList.clear(); const { provider } = this.options; - var results = []; + const results = []; const coords = validateCoords(query); if (coords) { results = coords; From 18c6a59b53f9f8395a08b0a8d72549cc79dd72c2 Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:30:35 +0200 Subject: [PATCH 07/13] Update src/coords.ts --- src/coords.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coords.ts b/src/coords.ts index bc5c8cdd7..d2e5dbdb1 100644 --- a/src/coords.ts +++ b/src/coords.ts @@ -3,7 +3,7 @@ export function validateCoords(query) { const q = query.trim(); const regex = /^(-?[0-9]*\.?\s*[0-9]*)\s*,?\s*(-?[0-9]*\.?[0-9]*)$/g; - var match = regex.exec(q); + const match = regex.exec(q); if (match) { var lat = Number(match[1]); var lng = Number(match[2]); From 173cf03ea739e5c08ed6339dfbbcce296560b5fd Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:30:42 +0200 Subject: [PATCH 08/13] Update src/SearchControl.ts --- src/SearchControl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SearchControl.ts b/src/SearchControl.ts index 3018dfd91..05e9650ee 100644 --- a/src/SearchControl.ts +++ b/src/SearchControl.ts @@ -397,7 +397,7 @@ const Control: SearchControl = { if (coords) { results = coords; } else { - results = await provider!.search(query); + results = await provider!.search(query); } if (results && results.length > 0) { From 5428e82b9964310b0150f4553cdffeacccc9b55f Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:30:48 +0200 Subject: [PATCH 09/13] Update src/coords.ts --- src/coords.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coords.ts b/src/coords.ts index d2e5dbdb1..b6e81cd7a 100644 --- a/src/coords.ts +++ b/src/coords.ts @@ -1,7 +1,7 @@ // @ts-nocheck export function validateCoords(query) { - const q = query.trim(); + 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) { From 4fe8cde3d875ff372eb57233eea67de005f7290d Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:37:24 +0200 Subject: [PATCH 10/13] Apply suggestions from code review --- src/SearchControl.ts | 4 ++-- src/coords.ts | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/SearchControl.ts b/src/SearchControl.ts index 05e9650ee..9faaf15e9 100644 --- a/src/SearchControl.ts +++ b/src/SearchControl.ts @@ -374,7 +374,7 @@ const Control: SearchControl = { const { provider } = this.options; if (query.length) { - const results = []; + let results = []; const coords = validateCoords(query); if (coords) { results = coords; @@ -392,7 +392,7 @@ const Control: SearchControl = { this.resultList.clear(); const { provider } = this.options; - const results = []; + let results = []; const coords = validateCoords(query); if (coords) { results = coords; diff --git a/src/coords.ts b/src/coords.ts index b6e81cd7a..bb2b0711e 100644 --- a/src/coords.ts +++ b/src/coords.ts @@ -2,11 +2,11 @@ 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) { - var lat = Number(match[1]); - var lng = Number(match[2]); + 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]); + let lng = Number(match[2]); if (( -90 < lat < 90) && ( -180 < lng < 180)) { return [{ x: lng, From b54f341b8802e459b78bcb88db14e0434cf913c9 Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:44:06 +0200 Subject: [PATCH 11/13] Apply suggestions from code review --- src/coords.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coords.ts b/src/coords.ts index bb2b0711e..d013b92da 100644 --- a/src/coords.ts +++ b/src/coords.ts @@ -5,9 +5,9 @@ export function validateCoords(query) { 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]); - let lng = Number(match[2]); - if (( -90 < lat < 90) && ( -180 < lng < 180)) { + const lat = Number(match[1]); + const lng = Number(match[2]); + if (-90 < lat < 90 && -180 < lng < 180) { return [{ x: lng, y: lat, From 17efc782e7d26d284d1d249c2d20ccdc91a6bc0d Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:53:47 +0200 Subject: [PATCH 12/13] Apply suggestions from code review --- src/coords.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/coords.ts b/src/coords.ts index d013b92da..7998cced7 100644 --- a/src/coords.ts +++ b/src/coords.ts @@ -8,14 +8,16 @@ export function validateCoords(query) { const lat = Number(match[1]); const lng = Number(match[2]); if (-90 < lat < 90 && -180 < lng < 180) { - return [{ - x: lng, - y: lat, - label: q, - bounds: null, - raw: {} - }]; - } + return [ + { + x: lng, + y: lat, + label: q, + bounds: null, + raw: {} + }, + ]; } - return false; + } + return false; } From c44d8f52fd7033f907ea0da367b0b91ae5fb903d Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Wed, 4 Oct 2023 23:56:25 +0200 Subject: [PATCH 13/13] Apply suggestions from code review --- src/coords.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coords.ts b/src/coords.ts index 7998cced7..b874b8e30 100644 --- a/src/coords.ts +++ b/src/coords.ts @@ -14,9 +14,9 @@ export function validateCoords(query) { y: lat, label: q, bounds: null, - raw: {} - }, - ]; + raw: {}, + }, + ]; } } return false;