Suite of AT Protocol TypeScript libraries built on web standards
21
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 45 lines 1.2 kB view raw
1import bases, { type SupportedEncodings } from "./util.ts"; 2 3export type { SupportedEncodings }; 4 5/** 6 * Create a `Uint8Array` from the passed string 7 * 8 * Supports `utf8`, `utf-8`, `hex`, and any encoding supported by the multiformats module. 9 * 10 * Also `ascii` which is similar to node's 'binary' encoding. 11 */ 12export function fromString( 13 string: string, 14 encoding: SupportedEncodings = "utf8", 15): Uint8Array { 16 const base = bases[encoding]; 17 18 if (base == null) { 19 throw new Error(`Unsupported encoding "${encoding}"`); 20 } 21 22 // add multibase prefix 23 return base.decoder.decode(`${base.prefix}${string}`); // eslint-disable-line @typescript-eslint/restrict-template-expressions 24} 25 26/** 27 * Turns a `Uint8Array` into a string. 28 * 29 * Supports `utf8`, `utf-8` and any encoding supported by the multibase module. 30 * 31 * Also `ascii` which is similar to node's 'binary' encoding. 32 */ 33export function toString( 34 array: Uint8Array, 35 encoding: SupportedEncodings = "utf8", 36): string { 37 const base = bases[encoding]; 38 39 if (base == null) { 40 throw new Error(`Unsupported encoding "${encoding}"`); 41 } 42 43 // strip multibase prefix 44 return base.encoder.encode(array).substring(1); 45}