commit b7aa38037cd9b503c065b4fbe57ec9beb4a91e2f
parent 23c017d81adeb8229a04f4a7f19bd082a061c0ad
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Mon, 28 Oct 2024 03:21:48 +0000
utils: add math and units utils. edit trade, regex. edit global.d.ts
Diffstat:
8 files changed, 138 insertions(+), 67 deletions(-)
diff --git a/utils/package.json b/utils/package.json
@@ -19,6 +19,7 @@
},
"dependencies": {
"@nostr-dev-kit/ndk": "^2.10.0",
+ "convert": "^5.5.1",
"jshashes": "^1.0.8",
"ngeohash": "0.6.3",
"uuid": "^10.0.0"
diff --git a/utils/src/global.d.ts b/utils/src/global.d.ts
@@ -1 +1,6 @@
-declare module "jshashes";
-\ No newline at end of file
+declare module 'jshashes' {
+ export class SHA256 {
+ b64(input: string): string;
+ b64_hmac(key: string, input: string): string;
+ }
+}
diff --git a/utils/src/index.ts b/utils/src/index.ts
@@ -1,9 +1,9 @@
-import "./global.d.ts";
export * from "./currency"
export * from "./error"
export * from "./geolocation"
export * from "./jshashes"
+export * from "./math"
export * from "./nostr/lib"
export * from "./nostr/ndk"
export * from "./nostr/types"
@@ -11,5 +11,7 @@ export * from "./regex"
export * from "./time"
export * from "./trade"
export * from "./types"
+export * from "./units"
export * from "./uuid"
export * from "./window"
+
diff --git a/utils/src/math.ts b/utils/src/math.ts
@@ -0,0 +1,3 @@
+export const round_to_5 = (num: number): number => {
+ return Math.round(num / 5) * 5;
+};
diff --git a/utils/src/regex.ts b/utils/src/regex.ts
@@ -9,5 +9,5 @@ export const regex: Record<string, RegExp> = {
price_ch: /[0-9.]$/,
profile_name: /^[a-zA-Z0-9._]{3,30}$/,
profile_name_ch: /[a-zA-Z0-9._]/,
- trade_product_key: /^(?:[a-zA-Z0-9]+(?:\s+[a-zA-Z0-9]+){0,2})?$/,
+ trade_product_key: /^(?:[a-zA-Z0-9]+(?:\s+[a-zA-Z0-9]+){0,2})$/
};
diff --git a/utils/src/trade.ts b/utils/src/trade.ts
@@ -1,38 +1,16 @@
-export type TradeKey = `coffee` | `cacao` | `maca`;
-
-export const trade_keys: TradeKey[] = [`coffee`, `cacao`, `maca`] as const;
+import { type MassUnit, parse_trade_mass_unit } from "./units";
-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 TradeKey = `coffee` | `cacao` | `maca`;
-export function parse_trade_mass_unit(val?: string): TradeMassUnit | undefined {
- switch (val) {
- case "kg":
- case "lb":
- case "g":
- return val;
- default:
- return undefined;
- };
-};
-export type TradeKeyQuantity = {
+export type TradeQuantity = {
label: string;
mass: number;
- mass_unit: TradeMassUnit;
+ mass_unit: MassUnit;
};
-export const fmt_trade_quantity_val = (obj: TradeKeyQuantity): string => `${obj.mass}-${obj.mass_unit}-${obj.label}`;
+export const trade_keys: TradeKey[] = [`coffee`, `cacao`, `maca`] as const;
-const trade_quantities_default: TradeKeyQuantity[] = [
+const trade_quantity_default: TradeQuantity[] = [
{
label: `bag`,
mass: 10,
@@ -50,43 +28,103 @@ const trade_quantities_default: TradeKeyQuantity[] = [
},
];
-export const trade_quantities: Record<TradeKey, TradeKeyQuantity[]> = {
- coffee: [
- {
- label: `bag`,
- mass: 60,
- mass_unit: `kg`
- },
- {
- label: `bag`,
- mass: 69,
- mass_unit: `kg`
- },
- {
- label: `bag`,
- mass: 30,
- mass_unit: `kg`
- },
- ],
- cacao: [
- ...trade_quantities_default
- ],
- maca: [
- {
- label: `bag`,
- mass: 1,
- mass_unit: `kg`
+export type TradeParam = {
+ default: {
+ quantity: TradeQuantity[];
+ },
+ key: Record<TradeKey, {
+ quantity: TradeQuantity[];
+ process: string[];
+ }>;
+};
+export const trade: TradeParam = {
+ default: {
+ quantity: trade_quantity_default,
+ },
+ key: {
+ coffee: {
+ quantity: [
+ {
+ label: `bag`,
+ mass: 60,
+ mass_unit: `kg`
+ },
+ {
+ label: `bag`,
+ mass: 69,
+ mass_unit: `kg`
+ },
+ {
+ label: `bag`,
+ mass: 30,
+ mass_unit: `kg`
+ },
+ ],
+ process: [
+ `washed`,
+ `natural`,
+ `honey`,
+ `semi_washed`,
+ `wet_hulled`,
+ `dry`,
+ `pulped_natural`,
+ `carbonic_maceration`
+ ]
},
- {
- label: `bag`,
- mass: 100,
- mass_unit: `g`
+ cacao: {
+ quantity: [
+ ...trade_quantity_default
+ ],
+ process: [
+ `raw`,
+ `fermented`,
+ `dried`,
+ `roasted`,
+ `cocoa_powder`,
+ `cocoa_butter`,
+ `chocolate`
+ ]
},
- ...trade_quantities_default
- ]
-}
+ maca: {
+ quantity: [
+ {
+ label: `bag`,
+ mass: 1,
+ mass_unit: `kg`
+ },
+ {
+ label: `bag`,
+ mass: 100,
+ mass_unit: `g`
+ },
+ ...trade_quantity_default
+ ],
+ process: [
+ `raw`,
+ `powdered`,
+ `roasted`,
+ `gelatinized`,
+ `capsules`
+ ]
+ }
+ }
+};
+
+export const fmt_trade_quantity_sel_val = (obj: TradeQuantity): string => `${obj.mass}-${obj.mass_unit}`;
+
+export function parse_trade_key(val?: string): TradeKey | undefined {
+ switch (val) {
+ case "coffee":
+ case "cacao":
+ case "maca":
+ return val;
+ default:
+ return undefined;
+ };
+};
+
-export function parse_trade_mass_tuple(val?: string): [number, TradeMassUnit, string] | undefined {
+export function parse_trade_mass_tuple(val?: string): [number, MassUnit, string] | undefined {
if (!val) return;
const vals = val.split('-');
if (vals.length !== 3) return;
diff --git a/utils/src/units.ts b/utils/src/units.ts
@@ -0,0 +1,22 @@
+import convert from "convert";
+
+export type MassUnit = "kg" | "lb" | "g";
+
+export function parse_trade_mass_unit(val?: string): MassUnit | undefined {
+ switch (val) {
+ case "kg":
+ case "lb":
+ case "g":
+ return val;
+ default:
+ return undefined;
+ };
+};
+
+export const mass_g = (units: MassUnit, amt: number): number => {
+ return convert(amt, units).to(`g`);
+};
+
+export const mass_tf = (units_from: MassUnit, units_to: MassUnit, amt: number): number => {
+ return convert(amt, units_from).to(units_to);
+};
+\ No newline at end of file
diff --git a/utils/tsconfig.json b/utils/tsconfig.json
@@ -9,7 +9,7 @@
"outDir": "./dist",
"rootDir": "./src",
"noEmit": true,
- "esModuleInterop": true
+ "esModuleInterop": true,
},
"include": [
"src"