import { Lazy } from "./resolve-once.js"; import { Result } from "./result.js"; import { ToUInt8, coerceIntoUint8 } from "./utils/coerce-uint8.js"; export type ToDecoder = ToUInt8 | string | Result; export type AsyncToDecoder = ToDecoder | Blob | Promise; export interface TxtEnDecoder { encode: (input: string) => Uint8Array; decode: (input?: ToDecoder) => string; asyncDecode: (input?: AsyncToDecoder) => Promise; } class TxtOps implements TxtEnDecoder { readonly encoder = new TextEncoder(); readonly decoder = new TextDecoder(); readonly encode = (str: string): Uint8Array => { return this.encoder.encode(str); }; readonly decode = (data?: ToDecoder): string => { if (!data) { return ""; } if (Result.Is(data)) { if (data.isErr()) { throw data.Err(); } // only for string let do coerceInto the work const unwrapped = data.unwrap(); if (typeof unwrapped === "string") { return this.decode(unwrapped); } } if (typeof data === "string") { return data; } return this.decoder.decode(coerceIntoUint8(data as ToUInt8).Ok()); }; readonly asyncDecode = async (data?: AsyncToDecoder): Promise => { if (!data) { return ""; } let resolved = await data; if (resolved instanceof Blob) { resolved = await resolved.arrayBuffer(); } return this.decode(resolved); }; } export const TxtEnDecoderSingleton: () => TxtEnDecoder = Lazy((): TxtEnDecoder => new TxtOps());