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) • 1.26 kB
JavaScript
import{a as p}from"./chunk-EW6NS4PK.js";var y={1:0,2:1,3:2,4:3,5:4,6:5,7:6,8:7,9:8,A:9,B:10,C:11,D:12,E:13,F:14,G:15,H:16,J:17,K:18,L:19,M:20,N:21,P:22,Q:23,R:24,S:25,T:26,U:27,V:28,W:29,X:30,Y:31,Z:32,a:33,b:34,c:35,d:36,e:37,f:38,g:39,h:40,i:41,j:42,k:43,m:44,n:45,o:46,p:47,q:48,r:49,s:50,t:51,u:52,v:53,w:54,x:55,y:56,z:57},g="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".split("");function A(u){let a=p(u),l=0,r=0,i=0;for(;a[i]===0;)r++,i++;let h=a.slice(i),d=Math.round(a.length*138/100+1),n=[];h.forEach(e=>{let c=0,t=e;for(let o=d-1;(t>0||c<l)&&o!==-1;o--,c++)t+=(n[o]??0)*256,n[o]=Math.round(t%58),t=Math.floor(t/58);l=c});let s=Array.from({length:n.length+r});return r>0&&s.fill("1",0,r),n.forEach(e=>s.push(g[e])),s.join("")}function b(u){let a=u.trim().split(""),l=0,r=0,i=0;for(;a[i]==="1";)r++,i++;let h=a.slice(i),d=Math.round(u.length*733/1e3+1),n=[];h.forEach((e,c)=>{let t=y[e],o=0;if(t===void 0)throw new TypeError(`Invalid base58 char at index ${c} with value ${e}`);for(let f=d-1;(t>0||o<l)&&f!==-1;f--,o++)t+=58*(n[f]??0),n[f]=Math.round(t%256),t=Math.floor(t/256);l=o});let s=n.filter(e=>e!==void 0);if(r>0){let e=Array.from({length:r}).fill(0,0,r);return new Uint8Array([...e,...s])}return new Uint8Array(s)}export{A as a,b};