web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit 2ac4de88414062adb0ae5629891e335f7c096aef
parent 99a3376ca680716cfb402a28ac9d71366b7c7d80
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Tue,  8 Oct 2024 04:39:13 +0000

utils: edit geolocation functions using coordinate point type, add types, edit regex

Diffstat:
Mutils/src/currency.ts | 34++++++++++++++++++++++++++++------
Mutils/src/error.ts | 8++++++--
Mutils/src/geolocation.ts | 7++++---
Mutils/src/regex.ts | 5+++--
Mutils/src/trade.ts | 6------
Mutils/src/types.ts | 17+++++++++--------
6 files changed, 50 insertions(+), 27 deletions(-)

diff --git a/utils/src/currency.ts b/utils/src/currency.ts @@ -3,11 +3,25 @@ export const fiat_currencies: FiatCurrency[] = [`usd`, `eur`] as const; export type FiatCurrencyGlyphs = `dollar` | `eur`; +export type CurrencyPrice = { + symbol: string; + currency: FiatCurrency; + /** + * integer value + */ + val_i: number; + /** + * fractional value + */ + val_f: number; +}; + export function parse_currency(val?: string): FiatCurrency { - switch (val) { + const _val = val?.trim().toLowerCase() + switch (_val) { case "usd": case "eur": - return val; + return _val; default: return `usd`; }; @@ -24,14 +38,22 @@ export function parse_currency_glyph_key(val?: string): | `currency-${FiatCurren }; }; -export const fmt_currency_tuple = (locale: string, currency: string, amount: number): [string, string, string] => { +export const parse_currency_price = (locale: string, _currency: string, amount: number): CurrencyPrice => { + const currency = parse_currency(_currency); const fmt = new Intl.NumberFormat(locale, { style: 'currency', - currency: parse_currency(currency).toUpperCase(), + currency: currency.toUpperCase(), minimumFractionDigits: 2, }); const fmt_amt = fmt.format(amount); - const [a, b] = fmt_amt.split('.'); - return [a.charAt(0), a.slice(1), b.length > 1 ? b : '00']; + const [symbol_val_i, val_f] = fmt_amt.split('.'); + return { + symbol: symbol_val_i.charAt(0), + currency, + val_i: Number(symbol_val_i.slice(1)), + val_f: Number(val_f), + } + // + //return [a.charAt(0), a.slice(1), b.length > 1 ? b : '00']; }; diff --git a/utils/src/error.ts b/utils/src/error.ts @@ -1,7 +1,11 @@ -import type { ErrorResponse } from "./types"; +import type { ErrorMessage, ErrorResponse } from "./types"; -export function err_msg(e: unknown, append?: string): ErrorResponse { +export const handle_error = (e: unknown, append?: string): ErrorResponse => { const msg = (e as Error).message ? (e as Error).message : String(e); const error = `${msg}${append ? ` ${append}` : ``}`; return { error }; }; + +export const err_msg = <T extends string>(err: T): ErrorMessage<T> => { + return { err }; +}; diff --git a/utils/src/geolocation.ts b/utils/src/geolocation.ts @@ -1,7 +1,7 @@ -import ngeohash from "ngeohash"; +import * as ngeohash from "ngeohash"; import type { LocationPoint } from "./types"; -export type GeolocationCoordinates = { +export type GeolocationCoordinatesPoint = { lat: number; lng: number; } @@ -28,7 +28,8 @@ export const geohash_decode = (opts: { }; }; -export const location_geohash = (lat: number, lng: number): string => { +export const location_geohash = (point: GeolocationCoordinatesPoint): string => { + const { lat, lng } = point; const res = geohash_encode({ lat, lng }); return res; }; \ No newline at end of file diff --git a/utils/src/regex.ts b/utils/src/regex.ts @@ -3,8 +3,9 @@ export const regex: Record<string, RegExp> = { alpha: /[a-zA-Z ]$/, num: /^[0-9]+$/, alphanum: /[a-zA-Z0-9., ]$/, + alphanum_ch: /[a-zA-Z0-9., ]/, price: /^\d+(\.\d+)?$/, - price_charset: /[0-9.]$/, + price_ch: /[0-9.]$/, profile_name: /^[a-zA-Z0-9._]{1,30}$/, - profile_name_char: /[a-zA-Z0-9._]/, + profile_name_ch: /[a-zA-Z0-9._]/, }; diff --git a/utils/src/trade.ts b/utils/src/trade.ts @@ -89,20 +89,14 @@ export const trade_quantities: Record<TradeKey, TradeKeyQuantity[]> = { export function parse_trade_mass_tuple(val?: string): [number, TradeMassUnit, string] | undefined { if (!val) return; const vals = val.split('-'); - if (vals.length !== 3) return; - const mass = vals[0]; const mass_unit = vals[1]; const label = vals[2]; - const amt = parseInt(mass, 10); if (isNaN(amt) || amt <= 0) return; - const units = parse_trade_mass_unit(mass_unit); if (!units) return; - if (typeof label !== `string` || !label) return; - return [amt, units, label] } \ No newline at end of file diff --git a/utils/src/types.ts b/utils/src/types.ts @@ -1,14 +1,15 @@ -export type ErrorResponse = { - error: string; -}; +import type { GeolocationCoordinatesPoint } from "./geolocation"; -export type LocationPoint = { +export type ErrorResponse = { error: string; }; +export type ErrorMessage<T extends string> = { err: T }; + +export type ResultId = { id: string; }; +export type ResultsList<T> = { results: T[]; }; + +export type LocationPoint = GeolocationCoordinatesPoint & { lat: number; lng: number; - error: { - lat: number; - lng: number; - }; + error: GeolocationCoordinatesPoint; } export type NumberTuple = [number, number]; \ No newline at end of file