UNPKG

uint8arrays

Version:

Utility functions to make dealing with Uint8Arrays easier

48 lines 1.13 kB
import { bases } from 'multiformats/basics'; import { allocUnsafe } from '#alloc'; 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 = allocUnsafe(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, binary: ascii, ...bases }; export default BASES; //# sourceMappingURL=bases.js.map