fix: Generic utilities (isRecord, coerceString, getErrorMessage, ensureAddress) have no shared home (#363)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
e53591de9d
commit
59ae30bb37
6 changed files with 63 additions and 35 deletions
11
packages/utils/package.json
Normal file
11
packages/utils/package.json
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "@harb/utils",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "src/index.ts",
|
||||
"types": "src/index.ts",
|
||||
"dependencies": {
|
||||
"viem": "^2.22.13"
|
||||
}
|
||||
}
|
||||
34
packages/utils/src/index.ts
Normal file
34
packages/utils/src/index.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { getAddress, isAddress, type Address } from 'viem';
|
||||
|
||||
export function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null;
|
||||
}
|
||||
|
||||
export function coerceString(value: unknown): string | null {
|
||||
if (typeof value === 'string') {
|
||||
const trimmed = value.trim();
|
||||
if (trimmed.length > 0) return trimmed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getErrorMessage(error: unknown, fallback: string): string {
|
||||
if (error instanceof Error) {
|
||||
const msg = coerceString(error.message);
|
||||
if (msg) return msg;
|
||||
}
|
||||
if (isRecord(error)) {
|
||||
const short = coerceString(error.shortMessage);
|
||||
if (short) return short;
|
||||
const msg = coerceString(error.message);
|
||||
if (msg) return msg;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
export function ensureAddress(value: string, label: string): Address {
|
||||
if (!value || !isAddress(value)) {
|
||||
throw new Error(`${label} is not a valid address`);
|
||||
}
|
||||
return getAddress(value);
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
"main": "src/index.ts",
|
||||
"types": "src/index.ts",
|
||||
"dependencies": {
|
||||
"@harb/utils": "*",
|
||||
"@wagmi/vue": "^0.2.8",
|
||||
"viem": "^2.22.13"
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue