UNPKG

uint8arrays

Version:

Utility functions to make dealing with Uint8Arrays easier

44 lines 953 B
import { bases } from 'multiformats/basics'; function createCodec(name, prefix, encode, decode) { return { name, prefix, encoder: { name, prefix, encode }, decoder: { decode } }; } const string = createCodec('utf8', 'u', buf => { const decoder = new TextDecoder('utf8'); return 'u' + decoder.decode(buf); }, str => { const encoder = new TextEncoder(); return encoder.encode(str.substring(1)); }); const ascii = createCodec('ascii', 'a', buf => { let string = 'a'; for (let i = 0; i < buf.length; i++) { string += String.fromCharCode(buf[i]); } return string; }, str => { str = str.substring(1); const buf = new Uint8Array(str.length); for (let i = 0; i < str.length; i++) { buf[i] = str.charCodeAt(i); } return buf; }); const BASES = { utf8: string, 'utf-8': string, hex: bases.base16, latin1: ascii, ascii: ascii, binary: ascii, ...bases }; export default BASES;