More search functionality

This commit is contained in:
Pieter Vander Vennet 2024-08-22 22:50:37 +02:00
parent 5d0de8520b
commit 1c46a65c84
25 changed files with 962 additions and 846 deletions

View file

@ -1,5 +1,6 @@
import GeocodingProvider, { GeoCodeResult, GeocodingOptions } from "./GeocodingProvider"
import { Utils } from "../../Utils"
import { ImmutableStore, Store } from "../UIEventSource"
/**
* A simple search-class which interprets possible locations
@ -17,28 +18,25 @@ export default class CoordinateSearch implements GeocodingProvider {
]
/**
*
* @param query
* @param options
*
* const ls = new CoordinateSearch()
* const results = await ls.search("https://www.openstreetmap.org/search?query=Brugge#map=11/51.2611/3.2217")
* const results = ls.directSearch("https://www.openstreetmap.org/search?query=Brugge#map=11/51.2611/3.2217")
* results.length // => 1
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate","source": "coordinateSearch"}
*
* const ls = new CoordinateSearch()
* const results = await ls.search("https://www.openstreetmap.org/#map=11/51.2611/3.2217")
* const results = ls.directSearch("https://www.openstreetmap.org/#map=11/51.2611/3.2217")
* results.length // => 1
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate","source": "coordinateSearch"}
*
* const ls = new CoordinateSearch()
* const results = await ls.search("51.2611 3.2217")
* const results = ls.directSearch("51.2611 3.2217")
* results.length // => 2
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate", "source": "coordinateSearch"}
* results[1] // => {lon: 51.2611, lat: 3.2217, display_name: "lon: 51.2611, lat: 3.2217", "category": "coordinate", "source": "coordinateSearch"}
*
*/
async search(query: string, options?: GeocodingOptions): Promise<GeoCodeResult[]> {
private directSearch(query: string): GeoCodeResult[] {
const matches = Utils.NoNull(CoordinateSearch.latLonRegexes.map(r => query.match(r))).map(m => <GeoCodeResult>{
lat: Number(m[1]),
@ -49,8 +47,7 @@ export default class CoordinateSearch implements GeocodingProvider {
})
const matchesLonLat = Utils.NoNull(CoordinateSearch.lonLatRegexes.map(r => query.match(r)))
const matchesLonLat = Utils.NoNull(CoordinateSearch.lonLatRegexes.map(r => query.match(r)))
.map(m => <GeoCodeResult>{
lat: Number(m[2]),
lon: Number(m[1]),
@ -58,12 +55,15 @@ export default class CoordinateSearch implements GeocodingProvider {
source: "coordinateSearch",
category: "coordinate"
})
return matches.concat(matchesLonLat)
}
suggest(query: string, options?: GeocodingOptions): Promise<GeoCodeResult[]> {
return this.search(query, options)
suggest(query: string): Store<GeoCodeResult[]> {
return new ImmutableStore(this.directSearch(query))
}
async search (query: string): Promise<GeoCodeResult[]> {
return this.directSearch(query)
}
}