UNPKG

byte-encodings

Version:

Utilities for encoding and decoding common formats like hex, base64, and varint. Ported from Deno's @std/encoding.

2 lines (1 loc) 681 B
var i=new TextEncoder().encode("0123456789abcdef"),a=new Uint8Array(128).fill(16);i.forEach((n,e)=>a[n]=e);new TextEncoder().encode("ABCDEF").forEach((n,e)=>a[n]=e+10);function d(n){return n*2}function y(n,e,r,t){for(;e<n.length;++e){let o=n[e];n[r++]=t[o>>4],n[r++]=t[o&15]}return r}function m(n,e,r,t){if((n.length-r)%2===1)throw new RangeError(`Cannot decode input as hex: Length (${n.length-r}) must be divisible by 2`);for(e+=1;e<n.length;e+=2)n[r++]=c(n[e-1],t)<<4|c(n[e],t);return r}function c(n,e){let r=e[n]??16;if(r===16)throw new TypeError(`Cannot decode input as hex: Invalid character (${String.fromCharCode(n)})`);return r}export{i as a,a as b,d as c,y as d,m as e};