2025-01-28 21:04:37 +00:00
|
|
|
import * as nip19 from "nostr-tools/nip19"
|
2025-06-06 23:38:11 +00:00
|
|
|
import {range, DAY, hexToBytes, bytesToHex} from "@welshman/lib"
|
2025-01-28 21:04:37 +00:00
|
|
|
|
|
|
|
|
export const nsecEncode = (secret: string) => nip19.nsecEncode(hexToBytes(secret))
|
|
|
|
|
|
|
|
|
|
export const nsecDecode = (nsec: string) => {
|
|
|
|
|
const {type, data} = nip19.decode(nsec)
|
|
|
|
|
|
|
|
|
|
if (type !== "nsec") throw new Error(`Invalid nsec: ${nsec}`)
|
|
|
|
|
|
|
|
|
|
return bytesToHex(data)
|
|
|
|
|
}
|
2025-02-05 18:47:56 +00:00
|
|
|
|
2025-02-06 00:26:22 +00:00
|
|
|
export const day = (seconds: number) => Math.floor(seconds / DAY)
|
2025-02-05 18:47:56 +00:00
|
|
|
|
2025-02-05 23:15:50 +00:00
|
|
|
export const daysBetween = (start: number, end: number) => [...range(start, end, DAY)].map(day)
|
2025-05-07 22:04:35 +00:00
|
|
|
|
|
|
|
|
export const ucFirst = (s: string) => s.slice(0, 1).toUpperCase() + s.slice(1)
|
2026-01-23 23:35:54 +00:00
|
|
|
|
2026-02-05 20:51:59 +00:00
|
|
|
export const errorMessage = (err: unknown) => String(err).replace(/^.*Error: /, "")
|
|
|
|
|
|
2026-01-23 23:35:54 +00:00
|
|
|
export const buildUrl = (base: string | URL, ...pathname: string[]) => {
|
|
|
|
|
const url = new URL(base)
|
|
|
|
|
|
2026-01-26 18:08:43 +00:00
|
|
|
url.pathname = "/" + pathname.join("/")
|
2026-01-23 23:35:54 +00:00
|
|
|
|
|
|
|
|
return url.toString()
|
|
|
|
|
}
|
2026-02-16 16:16:03 +00:00
|
|
|
|
|
|
|
|
export const addPeriod = (s: string) => (s + ".").replace(/\.+$/, ".")
|