commit 3370defc76916b0c86ce0bea2a38a5f97e5d367c
parent b7aa38037cd9b503c065b4fbe57ec9beb4a91e2f
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Sat, 2 Nov 2024 10:39:15 +0000
utils: update hash utils with @noble/hashes dependency. edit trade utils functions, params. add/edit units utils, types. add file utils. add list utils
Diffstat:
11 files changed, 189 insertions(+), 45 deletions(-)
diff --git a/utils/package.json b/utils/package.json
@@ -18,10 +18,11 @@
"access": "public"
},
"dependencies": {
+ "@noble/hashes": "^1.4.0",
"@nostr-dev-kit/ndk": "^2.10.0",
"convert": "^5.5.1",
- "jshashes": "^1.0.8",
"ngeohash": "0.6.3",
- "uuid": "^10.0.0"
+ "uuid": "^10.0.0",
+ "zod": "^3.23.8"
}
}
\ No newline at end of file
diff --git a/utils/src/file.ts b/utils/src/file.ts
@@ -0,0 +1,25 @@
+import type { FileBytesFormat } from "./types";
+
+export const parse_file_name = (file_path: string): string => {
+ const file_path_dirs = file_path.split(`/`);
+ if (file_path_dirs.length > 0) return file_path_dirs[file_path_dirs.length - 1];
+ return ``
+};
+
+export const format_file_bytes = (num_bytes: number, format: FileBytesFormat): string => {
+ if (num_bytes < 0) throw new Error(`Number of bytes cannot be negative`);
+ let factor: number;
+ switch (format) {
+ case 'kb':
+ factor = 1024;
+ break;
+ case 'mb':
+ factor = 1024 ** 2;
+ break;
+ case 'gb':
+ factor = 1024 ** 3;
+ break;
+ }
+ const result = num_bytes / factor;
+ return `${result.toFixed(2)} ${format.toUpperCase()}`;
+};
diff --git a/utils/src/global.d.ts b/utils/src/global.d.ts
@@ -1,6 +0,0 @@
-declare module 'jshashes' {
- export class SHA256 {
- b64(input: string): string;
- b64_hmac(key: string, input: string): string;
- }
-}
diff --git a/utils/src/hash.ts b/utils/src/hash.ts
@@ -0,0 +1,28 @@
+import { hmac } from '@noble/hashes/hmac';
+import { sha256 } from '@noble/hashes/sha2';
+
+export const hash_sha256 = (msg: string): Uint8Array => {
+ const result = sha256(msg);
+ return result;
+};
+
+export const hash_sha256_bin = (msg_data: number[]): Uint8Array => {
+ const result = sha256
+ .create()
+ .update(Uint8Array.from(msg_data))
+ .digest();
+ return result;
+};
+
+export const hash_hmac = (key: string, msg: string): Uint8Array => {
+ const result = hmac(sha256, key, msg);
+ return result;
+};
+
+export const hash_hmac_bin = (key_data: number[], msg_data: number[]): Uint8Array => {
+ const result = hmac
+ .create(sha256, Uint8Array.from(key_data))
+ .update(Uint8Array.from(msg_data))
+ .digest();
+ return result;
+};
diff --git a/utils/src/index.ts b/utils/src/index.ts
@@ -1,8 +1,10 @@
export * from "./currency"
export * from "./error"
+export * from "./file"
export * from "./geolocation"
-export * from "./jshashes"
+export * from "./hash"
+export * from "./list"
export * from "./math"
export * from "./nostr/lib"
export * from "./nostr/ndk"
@@ -14,4 +16,3 @@ export * from "./types"
export * from "./units"
export * from "./uuid"
export * from "./window"
-
diff --git a/utils/src/jshashes.ts b/utils/src/jshashes.ts
@@ -1,19 +0,0 @@
-import * as jshashes from "jshashes";
-
-export function hash(str: string): string {
- const hasher = new jshashes.SHA256();
- const hash = hasher.b64(str);
- return String(hash);
-};
-
-export function hash_obj(obj: any): string {
- const hasher = new jshashes.SHA256();
- const hash = hasher.b64(JSON.stringify(obj));
- return String(hash);
-};
-
-export function hashHmac(l: string, hmac_iv: string): string {
- const hasher = new jshashes.SHA256();
- const hash = hasher.b64_hmac(hmac_iv, l);
- return String(hash);
-};
diff --git a/utils/src/list.ts b/utils/src/list.ts
@@ -0,0 +1,16 @@
+export const list_remove_index = <T>(array: T[], index: number): T[] => {
+ if (index < 0 || index >= array.length) throw new Error(`Index out of bounds`);
+ return [...array.slice(0, index), ...array.slice(index + 1)];
+};
+
+export const list_move_index = <T>(array: T[], index_start: number, index_end: number): T[] => {
+ if (index_start < 0 || index_start >= array.length || index_end < 0 || index_end >= array.length) throw new Error(`Index out of bounds`);
+ const item = array[index_start];
+ const newArray = array.filter((_, index) => index !== index_start);
+ const adjustedIndexEnd = index_end > index_start ? index_end - 1 : index_end;
+ return [
+ ...newArray.slice(0, adjustedIndexEnd),
+ item,
+ ...newArray.slice(adjustedIndexEnd)
+ ];
+};
diff --git a/utils/src/trade.ts b/utils/src/trade.ts
@@ -1,13 +1,28 @@
-import { type MassUnit, parse_trade_mass_unit } from "./units";
+import { type MassUnit, parse_mass_unit } from "./units";
export type TradeKey = `coffee` | `cacao` | `maca`;
-export type TradeQuantity = {
- label: string;
+export type TradeQuantityMass = {
mass: number;
mass_unit: MassUnit;
+}
+export type TradeQuantity = TradeQuantityMass & {
+ label: string;
};
+export const fmt_trade_quantity_tup = (obj: TradeQuantity): string => `${obj.mass}-${obj.mass_unit}-${obj.label}`;
+
+export const parse_trade_quantity_tup = (sel_key: string): TradeQuantity | undefined => {
+ const [qty_mass, qty_mass_u, qty_label] = sel_key.split(`-`);
+ if (!qty_mass || !qty_mass_u || !qty_label) return undefined;
+ return {
+ mass: parseInt(qty_mass),
+ mass_unit: parse_mass_unit(qty_mass_u), //@todo
+ label: qty_label
+ }
+};
+
+
export const trade_keys: TradeKey[] = [`coffee`, `cacao`, `maca`] as const;
const trade_quantity_default: TradeQuantity[] = [
@@ -35,6 +50,7 @@ export type TradeParam = {
key: Record<TradeKey, {
quantity: TradeQuantity[];
process: string[];
+ flavor: string[];
}>;
};
export const trade: TradeParam = {
@@ -69,7 +85,67 @@ export const trade: TradeParam = {
`dry`,
`pulped_natural`,
`carbonic_maceration`
- ]
+ ],
+ flavor: [
+ `natural`,
+ `bergamot`,
+ `jasmine honeysuckle`,
+ `orange`,
+ `blueberry`,
+ `apricot`,
+ `black tea`,
+ `raspberry`,
+ `nougat`,
+ `milk chocolate`,
+ `peach`,
+ `vanilla`,
+ `berry`,
+ `nut`,
+ `brown sugar`,
+ `grape`,
+ `raisin`,
+ `red apple`,
+ `sweet bread pastry`,
+ `pineapple`,
+ `star fruit`,
+ `mango`,
+ `grapefruit`,
+ `nectarine`,
+ `red grape`,
+ `maple syrup`,
+ `dark chocolate`,
+ `orange blossom`,
+ `marshmallow`,
+ `mandarin`,
+ `dried dates`,
+ `prune`,
+ `green apple`,
+ `licorice/anise`,
+ `cranberry`,
+ `caramel`,
+ `chocolate`,
+ `lemon`,
+ `golden raisin`,
+ `black cherry`,
+ `plum`,
+ `black currant`,
+ `roses`,
+ `cola`,
+ `banana`,
+ `red currant`,
+ `white grape`,
+ `green tea`,
+ `lychee`,
+ `tamarind`,
+ `dried fig`,
+ `green grape`,
+ `sugar cane`,
+ `cherry`,
+ `magnolia`,
+ `tropical fruit`,
+ `ctrius`,
+ `stronefruit`
+ ],
},
cacao: {
quantity: [
@@ -83,7 +159,8 @@ export const trade: TradeParam = {
`cocoa_powder`,
`cocoa_butter`,
`chocolate`
- ]
+ ],
+ flavor: []
},
maca: {
quantity: [
@@ -105,13 +182,12 @@ export const trade: TradeParam = {
`roasted`,
`gelatinized`,
`capsules`
- ]
+ ],
+ flavor: []
}
}
};
-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":
@@ -133,7 +209,7 @@ export function parse_trade_mass_tuple(val?: string): [number, MassUnit, string]
const label = vals[2];
const amt = parseInt(mass, 10);
if (isNaN(amt) || amt <= 0) return;
- const units = parse_trade_mass_unit(mass_unit);
+ const units = parse_mass_unit(mass_unit);
if (!units) return;
if (typeof label !== `string` || !label) return;
return [amt, units, label]
diff --git a/utils/src/types.ts b/utils/src/types.ts
@@ -16,4 +16,5 @@ export type LocationPoint = GeolocationCoordinatesPoint & {
error: GeolocationCoordinatesPoint;
}
-export type NumberTuple = [number, number];
-\ No newline at end of file
+export type NumberTuple = [number, number];
+export type FileBytesFormat = `kb` | `mb` | `gb`;
+\ No newline at end of file
diff --git a/utils/src/units.ts b/utils/src/units.ts
@@ -1,12 +1,32 @@
import convert from "convert";
+import { z } from "zod";
-export type MassUnit = "kg" | "lb" | "g";
+export const MassUnitSchema = z.union([
+ z.literal(`kg`),
+ z.literal(`lb`),
+ z.literal(`g`),
+]);
-export function parse_trade_mass_unit(val?: string): MassUnit | undefined {
+export const mass_units: MassUnit[] = [`kg`, `lb`, `g`] as const;
+
+export type MassUnit = z.infer<typeof MassUnitSchema>;
+
+export function parse_mass_unit(val: string): MassUnit {
+ switch (val) {
+ case `kg`:
+ case `lb`:
+ case `g`:
+ return val;
+ default:
+ return `kg`;
+ };
+};
+
+export function parse_mass_unit_u(val?: string): MassUnit | undefined {
switch (val) {
- case "kg":
- case "lb":
- case "g":
+ case `kg`:
+ case `lb`:
+ case `g`:
return val;
default:
return undefined;
diff --git a/utils/tsconfig.json b/utils/tsconfig.json
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"strict": true,
+ "target": "es2021",
"lib": [
"es2021",
"dom"