UNPKG

base128-ascii

Version:

Smaller than base64, only use ASCII, can run in web browser.

1 lines 921 B
{class EncodeOutput{constructor(t){this.uint8Array=t}toString(){return(new TextDecoder).decode(this.uint8Array)}toJSTemplateLiterals(){return`\`${this.toString().replace(/[\r\\`]|\${|<\/script/g,t=>"\r"==t?"\\r":"<\/script"==t?"<\\/script":"\\"+t)}\``}}this.base128={EncodeOutput,encode(t){"string"==typeof t&&(t=(new TextEncoder).encode(t));for(var e=t.length,r=new Uint8Array(Math.ceil(e/7*8)),n=0,o=0;n<e;)r[o++]=t[n]>>1&127,r[o++]=127&(t[n++]<<6|t[n]>>2),r[o++]=127&(t[n++]<<5|t[n]>>3),r[o++]=127&(t[n++]<<4|t[n]>>4),r[o++]=127&(t[n++]<<3|t[n]>>5),r[o++]=127&(t[n++]<<2|t[n]>>6),r[o++]=127&(t[n++]<<1|t[n]>>7),r[o++]=127&t[n++];return new EncodeOutput(r)},decode(t){for(var e,r=t.length,n=new Uint8Array(Math.floor(r/8*7)),o=0,c=0,i=()=>(e=t.charCodeAt(o++))>127?e=0:e;o<r;)n[c++]=i()<<1|i()>>6,n[c++]=e<<2|i()>>5,n[c++]=e<<3|i()>>4,n[c++]=e<<4|i()>>3,n[c++]=e<<5|i()>>2,n[c++]=e<<6|i()>>1,n[c++]=e<<7|i();return n}}}