UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 901 B
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */ import{ContentError as r}from"../error/ContentError.js";import{LengthError as t}from"../error/LengthError.js";const e="bdfghjklmnprstvz",o=[66,68,70,71,72,74,75,76,77,78,80,82,83,84,86,90],n="aiou",f=[65,73,79,85];export function fromBytes(r){if(1&r.length)throw t.mulOf(2,"bytes.length",r.length);let o="";for(let t=0;t<r.length;)o+=e[r[t]>>4&15]+n[r[t]>>2&3]+e[15&(r[t++]<<2|r[t]>>6)]+n[r[t]>>4&3]+e[15&r[t++]]+"-";return o.substring(0,o.length-1)}export function toBytes(t){const e=new Uint8Array(t.length/5*2);let n=0,h=0,l=0,i=-1;for(const s of t)if("-"!==s&&(1&~l++?(i=o.indexOf(95&s.charCodeAt(0)),h=h<<4|i,5===l&&(e[n++]=h>>8&255,e[n++]=255&h,l=0)):(i=f.indexOf(95&s.charCodeAt(0)),h=h<<2|i),i<0))throw new r("unknown",`src[${l-1}]`,s);if(0!=l)throw new r("Should be a multiple of 5, have leftovers","Size",l);return e.subarray(0,n)}