@a2zb/lib 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ export type Hex = `0x${string}`;
2
+ export declare const truncateHex: (hex: Hex) => string;
3
+ export declare const addrShort: (hex: Hex) => string;
@@ -0,0 +1,2 @@
1
+ export const truncateHex = (hex) => `${hex.slice(0, 4)}…${hex.slice(-4)}`;
2
+ export const addrShort = truncateHex;
@@ -0,0 +1,2 @@
1
+ export declare const resolveImage: (src: string) => Promise<string>;
2
+ export declare const getImageFromTokenURI: (tokenUri: string) => string;
@@ -0,0 +1,27 @@
1
+ const isSvg = (src) => {
2
+ return !!src && src.startsWith('data:image/svg');
3
+ };
4
+ const inlineSvgToBlobUrl = (src) => {
5
+ // removes prefix so we only have raw <svg ...>
6
+ const svg = src.replace('data:image/svg+xml;utf8,', '');
7
+ const blob = new Blob([svg], { type: 'image/svg+xml' });
8
+ return URL.createObjectURL(blob);
9
+ };
10
+ export const resolveImage = async (src) => {
11
+ if (!src)
12
+ return '';
13
+ // ONLY convert inline SVGs
14
+ if (isSvg(src))
15
+ return inlineSvgToBlobUrl(src);
16
+ // EVERYTHING ELSE: DO NOT FETCH
17
+ return src;
18
+ };
19
+ export const getImageFromTokenURI = (tokenUri) => {
20
+ // remove: data:application/json;base64,
21
+ const base64 = tokenUri.split(',')[1];
22
+ // decode base64 → json string
23
+ const json = atob(base64);
24
+ // parse json
25
+ const metadata = JSON.parse(json);
26
+ return metadata.image;
27
+ };
@@ -0,0 +1,2 @@
1
+ export declare const capitalize: (s: string) => string;
2
+ export declare const capitalizeWords: (s: string) => string;
@@ -0,0 +1,5 @@
1
+ export const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
2
+ export const capitalizeWords = (s) => s
3
+ .split(' ')
4
+ .map(w => capitalize(w))
5
+ .join(' ');
@@ -0,0 +1,9 @@
1
+ export declare const months: string[];
2
+ export type TimeUnit = "hour" | "day" | "month" | "week";
3
+ export declare const tsShort: (ts: number) => string;
4
+ export declare const tsSuperShort: (ts: number) => string;
5
+ export declare const tsLong: (ts: number) => string;
6
+ export declare const tsMonthNameUTC: (ts: number) => string;
7
+ export declare const hhmm: (ts: number) => string;
8
+ export declare const timeAgo: (ts: number) => string;
9
+ export declare const timeKey: (ts: number, unit: "hour" | "day" | "month" | "week") => string | undefined;
@@ -0,0 +1,72 @@
1
+ export const months = [
2
+ "January",
3
+ "February",
4
+ "March",
5
+ "April",
6
+ "May",
7
+ "June",
8
+ "July",
9
+ "August",
10
+ "September",
11
+ "October",
12
+ "November",
13
+ "December",
14
+ ];
15
+ export const tsShort = (ts) => {
16
+ const p = parts(ts);
17
+ return `${p.month} ${p.day} ${p.time}`;
18
+ };
19
+ export const tsSuperShort = (ts) => {
20
+ const p = parts(ts);
21
+ return `${p.month} ${p.day}`;
22
+ };
23
+ export const tsLong = (ts) => {
24
+ const p = parts(ts);
25
+ return `${p.yy}.${p.mm}.${p.dd} ${p.hh}:${p.min}`;
26
+ };
27
+ export const tsMonthNameUTC = (ts) => months[new Date(ts).getUTCMonth()];
28
+ export const hhmm = (ts) => {
29
+ return new Date(ts).toLocaleTimeString([], {
30
+ hour: "2-digit",
31
+ minute: "2-digit",
32
+ hour12: false,
33
+ });
34
+ };
35
+ const parts = (ts) => {
36
+ const d = new Date(ts);
37
+ return {
38
+ month: d.toLocaleString("en-US", { month: "short" }),
39
+ day: String(d.getDate()).padStart(2, "0"),
40
+ yy: String(d.getUTCFullYear()).slice(-2),
41
+ mm: String(d.getUTCMonth() + 1).padStart(2, "0"),
42
+ dd: String(d.getUTCDate()).padStart(2, "0"),
43
+ hh: String(d.getUTCHours()).padStart(2, "0"),
44
+ min: String(d.getUTCMinutes()).padStart(2, "0"),
45
+ time: d.toTimeString().slice(0, 5),
46
+ };
47
+ };
48
+ export const timeAgo = (ts) => {
49
+ const secs = Math.floor((Date.now() - ts) / 1000);
50
+ if (secs < 60)
51
+ return "just now";
52
+ const mins = Math.floor(secs / 60);
53
+ if (mins < 60)
54
+ return `${mins} min ago`;
55
+ const hours = Math.floor(mins / 60);
56
+ if (hours < 24)
57
+ return `${hours} hr ago`;
58
+ const days = Math.floor(hours / 24);
59
+ return `${days} day${days === 1 ? "" : "s"} ago`;
60
+ };
61
+ // never used for UI display only for charts => always UTC
62
+ export const timeKey = (ts, unit) => {
63
+ const d = new Date(ts);
64
+ switch (unit) {
65
+ case "hour":
66
+ return d.toISOString().slice(0, 13); // 2026-01-16T05
67
+ case "day":
68
+ return d.toISOString().slice(0, 10); // 2026-01-16
69
+ case "month":
70
+ return `${d.getUTCFullYear()}-${d.getUTCMonth()}`;
71
+ }
72
+ };
@@ -0,0 +1,4 @@
1
+ export type Page<T> = {
2
+ items: T[];
3
+ cursor: string | null;
4
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ export type Result<T> = {
2
+ ok: true;
3
+ data: T;
4
+ } | {
5
+ ok: false;
6
+ error: string;
7
+ };
8
+ export declare const unwrap: <T>(r: Result<T>) => T;
@@ -0,0 +1,5 @@
1
+ export const unwrap = (r) => {
2
+ if (!r.ok)
3
+ throw new Error(r.error);
4
+ return r.data;
5
+ };
@@ -0,0 +1,6 @@
1
+ export * from './fmt/hex';
2
+ export * from './fmt/image';
3
+ export * from './fmt/string';
4
+ export * from './fmt/time';
5
+ export * from './http/page';
6
+ export * from './http/result';
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from './fmt/hex';
2
+ export * from './fmt/image';
3
+ export * from './fmt/string';
4
+ export * from './fmt/time';
5
+ export * from './http/page';
6
+ export * from './http/result';
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@a2zb/lib",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "scripts": {
11
+ "build": "tsc"
12
+ }
13
+ }