Skip to content

Commit 5b2ffb9

Browse files
committed
Use search methods from @neaps/tide-database
1 parent 9c7f581 commit 5b2ffb9

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

packages/neaps/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
},
3636
"dependencies": {
3737
"@neaps/tide-database": "0.3",
38-
"@neaps/tide-predictor": "^0.4.0",
39-
"geolib": "^3.3.4"
38+
"@neaps/tide-predictor": "^0.4.0"
4039
}
4140
}

packages/neaps/src/index.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { getDistance } from "geolib";
2-
import { stations, type Station } from "@neaps/tide-database";
1+
import {
2+
stations,
3+
near,
4+
nearest,
5+
type Station,
6+
type Position,
7+
type NearOptions,
8+
type NearestOptions,
9+
} from "@neaps/tide-database";
310
import tidePredictor, { type TimeSpan, type ExtremesInput } from "@neaps/tide-predictor";
4-
import type { GeolibInputCoordinates } from "geolib/es/types";
511

612
type Units = "meters" | "feet";
713
type PredictionOptions = {
@@ -34,41 +40,39 @@ const defaultUnits: Units = "meters";
3440
* datum: 'MLLW', // optional, defaults to MLLW if available
3541
* })
3642
*/
37-
export function getExtremesPrediction(options: GeolibInputCoordinates & ExtremesOptions) {
43+
export function getExtremesPrediction(options: NearestOptions & ExtremesOptions) {
3844
return nearestStation(options).getExtremesPrediction(options);
3945
}
4046

4147
/**
4248
* Get timeline prediction using the nearest station to the given position.
4349
*/
44-
export function getTimelinePrediction(options: GeolibInputCoordinates & TimelineOptions) {
50+
export function getTimelinePrediction(options: NearestOptions & TimelineOptions) {
4551
return nearestStation(options).getTimelinePrediction(options);
4652
}
4753

4854
/**
4955
* Get water level at a specific time using the nearest station to the given position.
5056
*/
51-
export function getWaterLevelAtTime(options: GeolibInputCoordinates & WaterLevelOptions) {
57+
export function getWaterLevelAtTime(options: NearestOptions & WaterLevelOptions) {
5258
return nearestStation(options).getWaterLevelAtTime(options);
5359
}
5460

5561
/**
5662
* Find the nearest station to the given position.
5763
*/
58-
export function nearestStation(position: GeolibInputCoordinates) {
59-
return stationsNear(position, 1)[0];
64+
export function nearestStation(options: NearestOptions) {
65+
const data = nearest(options);
66+
if (!data) throw new Error(`No stations found with options: ${JSON.stringify(options)}`);
67+
return useStation(...data);
6068
}
6169

6270
/**
6371
* Find stations near the given position.
6472
* @param limit Maximum number of stations to return (default: 10)
6573
*/
66-
export function stationsNear(position: GeolibInputCoordinates, limit = 10) {
67-
return stations
68-
.map((station) => ({ station, distance: getDistance(position, station) }))
69-
.sort((a, b) => a.distance - b.distance)
70-
.slice(0, limit)
71-
.map(({ station, distance }) => useStation(station, distance));
74+
export function stationsNear(options: NearOptions) {
75+
return near(options).map(([station, distance]) => useStation(station, distance));
7276
}
7377

7478
/**

0 commit comments

Comments
 (0)