Add StationDataModule, implementing getNearestStations & a Geohash generator method
All checks were successful
Publish Package / build-and-publish (push) Successful in 6s

This commit is contained in:
2026-03-30 21:53:01 +01:00
parent ad355fe15e
commit fc74e933d6
5 changed files with 80 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
/**
* Checks if a string is a valid CRS
* Checks if a string is a valid CRS (Syntactically Only)
* using byte level checking for max performance
* @param CRS The CRS Code to be validated
*/
export const IsValidCrs = (CRS: string): boolean => {
if (CRS.length !== 3) return false;
@@ -15,8 +16,9 @@ export const IsValidCrs = (CRS: string): boolean => {
}
/**
* Checks if a string is a valid TIPLOC
* Checks if a string is a valid TIPLOC (Syntactically Only)
* using byte level checking for max performance
* @param TIPLOC The TIPLOC to be validated
*/
export const IsValidTiploc = (TIPLOC: string): boolean => {
const l = TIPLOC.length;
@@ -33,9 +35,10 @@ export const IsValidTiploc = (TIPLOC: string): boolean => {
}
/**
* Checks if a string is a valid PIS Code
* Checks if a string is a valid PIS Code (Syntactically Only)
* using byte level checking for max performance
* ONLY GWR (4-digit) CODES SUPPORTED
* @param PIS The PIS Code to be validated
*/
export const IsValidPis = (PIS: string): boolean => {
if (PIS.length !== 4) return false;
@@ -44,4 +47,15 @@ export const IsValidPis = (PIS: string): boolean => {
if (!(char >= 48 && char <= 57)) return false;
}
return true;
}
/**
* Validates Geohash string against standard b32 alphabet
* (Syntactically Only validation)
* @param hash The geohash to be validated
* @param maxLen Defaults to 6 - which is enforced by the server, should not need changing
*/
export const IsValidGeoHash = (hash: string, maxLen: number = 6): boolean => {
const geoHashRegex = new RegExp(`^[0-9bcdefghjkmnpqrstuvwxyz]{1,${maxLen}}$`, 'i');
return geoHashRegex.test(hash);
}

View File

@@ -1,6 +1,6 @@
import { ApiPisObject } from '@owlboard/api-schema-types';
import type { BaseClient, ApiResult } from '../lib/base.js';
import { IsValidCrs, IsValidTiploc, IsValidPis } from '../lib/validation.js';
import { IsValidCrs, IsValidPis } from '../lib/validation.js';
import { ValidationError } from '../lib/errors.js';
export class PisModule {

View File

@@ -0,0 +1,37 @@
import { ApiStationsNearestStations } from '@owlboard/api-schema-types';
import type { BaseClient, ApiResult } from '../lib/base.js';
import { IsValidGeoHash } from '../lib/validation.js';
import { ValidationError } from '../lib/errors.js';
import Geohash from 'latlon-geohash';
export class StationDataModule {
constructor(private client: BaseClient) {}
/**
* Generates a 6-char Geohash for the given co-ordinates
* @param lt Latitude
* @param ln Longitude
* @returns Geohash as string
*/
static generateGeohash(lt: number, ln: number): string {
return Geohash.encode(lt, ln, 6);
}
/**
*
* @param geohash Geohash as string (up to six characters), generate using this.generateGeohash()
* @returns Nearest Stations API Response (CRS, Name)
*/
async getNearestStations(geohash: string): Promise<ApiResult<ApiStationsNearestStations.StationsNearestStations[]>> {
if (!IsValidGeoHash(geohash)) {
throw new ValidationError("hash", "Invalid Geohash requested");
}
const path = `/stationData/nearest/${geohash}`;
return this.client.request<ApiStationsNearestStations.StationsNearestStations[]>(path, {
method: 'GET',
})
}
// getStationSate(crs: string){}
}