web_lib

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

commit fa399e085519d706898c9100700ad6fe756161c3
parent 469ccb00bb4e55c25a8f14cc4d7e969968a3d4fe
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Wed, 11 Sep 2024 11:49:57 +0000

utils: add/edit utils

Diffstat:
Autils/src/error.ts | 7+++++++
Mutils/src/index.ts | 5++++-
Autils/src/regex.ts | 8++++++++
Autils/src/time.ts | 8++++++++
Mutils/src/trade.ts | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Dutils/src/utils.ts | 27---------------------------
Autils/src/uuid.ts | 6++++++
7 files changed, 115 insertions(+), 30 deletions(-)

diff --git a/utils/src/error.ts b/utils/src/error.ts @@ -0,0 +1,7 @@ +import type { ErrorResponse } from "./types"; + +export function err_msg(e: unknown, append?: string): ErrorResponse { + const msg = (e as Error).message ? (e as Error).message : String(e); + const error = `${msg}${append ? ` ${append}` : ``}`; + return { error }; +}; diff --git a/utils/src/index.ts b/utils/src/index.ts @@ -1,7 +1,10 @@ import "./global.d.ts"; +export * from "./error" export * from "./geolocation" export * from "./jshashes" +export * from "./regex" +export * from "./time" export * from "./trade" export * from "./types" -export * from "./utils" +export * from "./uuid" diff --git a/utils/src/regex.ts b/utils/src/regex.ts @@ -0,0 +1,8 @@ +export const regex: Record<string, RegExp> = { + word_only: /^[a-zA-Z]+$/, + alpha: /[a-zA-Z ]$/, + num: /^[0-9]+$/, + alphanum: /[a-zA-Z0-9., ]$/, + price: /^\d+(\.\d+)?$/, + price_charset: /[0-9.]$/, +}; diff --git a/utils/src/time.ts b/utils/src/time.ts @@ -0,0 +1,7 @@ +export function time_now_ms(): number { + return Math.floor(new Date().getTime() / 1000); +}; + +export function time_created_on(): string { + return new Date().toISOString(); +}; +\ No newline at end of file diff --git a/utils/src/trade.ts b/utils/src/trade.ts @@ -1,3 +1,83 @@ export type TradeKey = `coffee` | `cacao` | `maca`; -export const trade_keys: TradeKey[] = [`coffee`, `cacao`, `maca`] as const; -\ No newline at end of file +export const trade_keys: TradeKey[] = [`coffee`, `cacao`, `maca`] as const; + +export function parse_trade_key(val?: string): TradeKey | undefined { + switch (val) { + case "coffee": + case "cacao": + case "maca": + return val; + default: + return undefined; + }; +}; + +export type TradeMassUnit = "kg" | "lb" | "g"; + +export type TradeKeyQuantity = { + label?: string; + qty_amt: number; + qty_unit: TradeMassUnit; +}; + +export const fmt_trade_quantity_val = (obj: TradeKeyQuantity): string => `${obj.qty_amt}-${obj.qty_unit}`; + +const trade_quantities_default: TradeKeyQuantity[] = [ + { + label: `bag`, + qty_amt: 10, + qty_unit: `kg` + }, + { + label: `bag`, + qty_amt: 5, + qty_unit: `kg` + }, + { + label: `bag`, + qty_amt: 25, + qty_unit: `kg` + }, +]; + +export const trade_quantities: Record<TradeKey, TradeKeyQuantity[]> = { + coffee: [ + { + label: `bag`, + qty_amt: 60, + qty_unit: `kg` + }, + { + label: `bag`, + qty_amt: 69, + qty_unit: `kg` + }, + { + label: `bag`, + qty_amt: 30, + qty_unit: `kg` + }, + { + label: `bag`, + qty_amt: 15, + qty_unit: `kg` + }, + ], + cacao: [ + ...trade_quantities_default + ], + maca: [ + { + label: `bag`, + qty_amt: 1, + qty_unit: `kg` + }, + { + label: `bag`, + qty_amt: 100, + qty_unit: `g` + }, + ...trade_quantities_default + ] +} +\ No newline at end of file diff --git a/utils/src/utils.ts b/utils/src/utils.ts @@ -1,27 +0,0 @@ -import { v4 } from "uuid"; -import type { ErrorResponse } from "./types"; - -export const regex: Record<string, RegExp> = { - word_only: /^[a-zA-Z]+$/, - alpha: /[a-zA-Z ]$/, - num: /^[0-9.]+$/, - alphanum: /[a-zA-Z0-9., ]$/, -}; - -export function uuidv4() { - return v4(); -}; - -export function time_now_ms(): number { - return Math.floor(new Date().getTime() / 1000); -}; - -export function time_created_on(): string { - return new Date().toISOString(); -}; - -export function err_msg(e: unknown, append?: string): ErrorResponse { - const msg = (e as Error).message ? (e as Error).message : String(e); - const error = `${msg}${append ? ` ${append}` : ``}`; - return { error }; -}; diff --git a/utils/src/uuid.ts b/utils/src/uuid.ts @@ -0,0 +1,5 @@ +import { v4 } from "uuid"; + +export function uuidv4() { + return v4(); +}; +\ No newline at end of file