|
| 1 | +import { acceptValueFor, bitsPerChar, valueAt } from '../bits' |
| 2 | +import puidEncoder from '../puidEncoder' |
| 3 | + |
| 4 | +// Named export so we can add decode later |
| 5 | +/** |
| 6 | + * Encode bytes into a string using the provided character set. |
| 7 | + * |
| 8 | + * ### Example (es module) |
| 9 | + * ```js |
| 10 | + * import { Chars, encode } from 'puid-js' |
| 11 | + * |
| 12 | + * const bytes = new Uint8Array([ |
| 13 | + * 0x09, 0x25, 0x84, 0x3c, 0xbd, 0xc0, 0x89, 0xeb, 0x61, 0x75, 0x81, 0x65, |
| 14 | + * 0x09, 0xb4, 0x9a, 0x54, 0x20 |
| 15 | + * ]) |
| 16 | + * encode(Chars.Safe64, bytes) |
| 17 | + * // => 'CSWEPL3AiethdYFlCbSaVC' |
| 18 | + * ``` |
| 19 | + * |
| 20 | + * ### Example (commonjs) |
| 21 | + * ```js |
| 22 | + * const { Chars, encode } = require('puid-js') |
| 23 | + * |
| 24 | + * const bytes = new Uint8Array([ |
| 25 | + * 0x09, 0x25, 0x84, 0x3c, 0xbd, 0xc0, 0x89, 0xeb, 0x61, 0x75, 0x81, 0x65, |
| 26 | + * 0x09, 0xb4, 0x9a, 0x54, 0x20 |
| 27 | + * ]) |
| 28 | + * encode(Chars.Safe64, bytes) |
| 29 | + * // => 'CSWEPL3AiethdYFlCbSaVC' |
| 30 | + * ``` |
| 31 | + */ |
| 32 | +const encode = (chars: string, bytes: Uint8Array): string => { |
| 33 | + const nBitsPerChar = bitsPerChar(chars) |
| 34 | + const nEntropyBits = 8 * bytes.length |
| 35 | + if (nEntropyBits === 0) return '' |
| 36 | + |
| 37 | + const encoder = puidEncoder(chars) |
| 38 | + // Use rejection sampling path for all charsets |
| 39 | + const acceptValue = acceptValueFor(chars) |
| 40 | + |
| 41 | + let offset = 0 |
| 42 | + const codes: number[] = [] |
| 43 | + while (offset + nBitsPerChar <= nEntropyBits) { |
| 44 | + const v = valueAt(offset, nBitsPerChar, bytes) |
| 45 | + const [accept, shift] = acceptValue(v) |
| 46 | + offset += shift |
| 47 | + if (accept) codes.push(encoder(v)) |
| 48 | + } |
| 49 | + return String.fromCharCode(...codes) |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Decode a string of characters back into bytes using the provided character set. |
| 54 | + * Pads the final partial byte with zeros if the bit-length is not a multiple of 8. |
| 55 | + * |
| 56 | + * ### Example (es module) |
| 57 | + * ```js |
| 58 | + * import { Chars, decode } from 'puid-js' |
| 59 | + * |
| 60 | + * const text = 'CSWEPL3AiethdYFlCbSaVC' |
| 61 | + * const bytes = decode(Chars.Safe64, text) |
| 62 | + * // => new Uint8Array([ |
| 63 | + * 0x09, 0x25, 0x84, 0x3c, 0xbd, 0xc0, 0x89, 0xeb, 0x61, 0x75, 0x81, 0x65, |
| 64 | + * 0x09, 0xb4, 0x9a, 0x54, 0x20 |
| 65 | + * ]) |
| 66 | + * ``` |
| 67 | + * |
| 68 | + * ### Example (commonjs) |
| 69 | + * ```js |
| 70 | + * const { Chars, decode } = require('puid-js') |
| 71 | + * |
| 72 | + * const text = 'CSWEPL3AiethdYFlCbSaVC' |
| 73 | + * const bytes = decode(Chars.Safe64, text) |
| 74 | + * // => new Uint8Array([ |
| 75 | + * 0x09, 0x25, 0x84, 0x3c, 0xbd, 0xc0, 0x89, 0xeb, 0x61, 0x75, 0x81, 0x65, |
| 76 | + * 0x09, 0xb4, 0x9a, 0x54, 0x20 |
| 77 | + * ]) |
| 78 | + * ``` |
| 79 | + */ |
| 80 | +const decode = (chars: string, text: string): Uint8Array => { |
| 81 | + const nBitsPerChar = bitsPerChar(chars) |
| 82 | + if (text.length === 0) return new Uint8Array(0) |
| 83 | + |
| 84 | + // Map charCode -> value index |
| 85 | + const map = new Map<number, number>() |
| 86 | + for (let i = 0; i < chars.length; i++) { |
| 87 | + map.set(chars.charCodeAt(i), i) |
| 88 | + } |
| 89 | + |
| 90 | + let acc = 0 |
| 91 | + let accBits = 0 |
| 92 | + const out: number[] = [] |
| 93 | + |
| 94 | + for (let i = 0; i < text.length; i++) { |
| 95 | + const code = text.charCodeAt(i) |
| 96 | + const val = map.get(code) |
| 97 | + if (val === undefined) throw new Error('Invalid character for charset') |
| 98 | + |
| 99 | + acc = (acc << nBitsPerChar) | val |
| 100 | + accBits += nBitsPerChar |
| 101 | + |
| 102 | + while (accBits >= 8) { |
| 103 | + const shift = accBits - 8 |
| 104 | + const byte = (acc >> shift) & 0xff |
| 105 | + out.push(byte) |
| 106 | + accBits -= 8 |
| 107 | + acc = acc & ((1 << accBits) - 1) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (accBits > 0) { |
| 112 | + // Pad remaining bits with zeros on the right |
| 113 | + out.push((acc << (8 - accBits)) & 0xff) |
| 114 | + } |
| 115 | + |
| 116 | + return new Uint8Array(out) |
| 117 | +} |
| 118 | + |
| 119 | +export { encode, decode } |
0 commit comments