UNPKG

@uzmoi/clockwork-base32

Version:

A fast and minimal implementation of Clockwork Base32 for TypeScript

31 lines (30 loc) 868 B
/** * Encode to Clockwork Base32. * * @see https://gist.github.com/szktty/228f85794e4187882a77734c89c384a8 */ export const encode = (u8array) => { let string = ""; let state = 0; let offset = 0; for (let i = 0; i < u8array.length; i++) { state = (state << 8) | u8array[i]; offset += 8 - 5; string += encodeSymbols[(state >> offset) & 0b11111]; if (offset > 5) { offset -= 5; string += encodeSymbols[(state >> offset) & 0b11111]; } } if (offset > 0) { string += encodeSymbols[(state << (5 - offset)) & 0b11111]; } return string; }; // deno-fmt-ignore export const encodeSymbols = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z", ];