Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/utils/src/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { utf8Decode, utf8Encode } from './textCodec'

export class Base64 {
static #CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
static #REVERSE = Object.fromEntries([...Base64.#CHARS].map((c, i) => [c.charCodeAt(0), i]))

static #toBase64(bytes: Uint8Array): string {
let result = ''
Expand All @@ -19,10 +18,10 @@ export class Base64 {
const sanitized = input.replace(/[^A-Z0-9+/=]/gi, '')
const bytes: number[] = []
for (let i = 0; i < sanitized.length; i += 4) {
const enc1 = Base64.#REVERSE[sanitized[i].charCodeAt(0)]
const enc2 = Base64.#REVERSE[sanitized[i + 1].charCodeAt(0)]
const enc3 = sanitized[i + 2] === '=' ? 0 : Base64.#REVERSE[sanitized[i + 2].charCodeAt(0)]
const enc4 = sanitized[i + 3] === '=' ? 0 : Base64.#REVERSE[sanitized[i + 3].charCodeAt(0)]
const enc1 = Base64.#CHARS.indexOf(sanitized[i])
const enc2 = Base64.#CHARS.indexOf(sanitized[i + 1])
const enc3 = sanitized[i + 2] === '=' ? 0 : Base64.#CHARS.indexOf(sanitized[i + 2])
const enc4 = sanitized[i + 3] === '=' ? 0 : Base64.#CHARS.indexOf(sanitized[i + 3])
const b = (enc1 << 18) | (enc2 << 12) | (enc3 << 6) | enc4
bytes.push(b >> 16 & 0xFF)
if (sanitized[i + 2] !== '=') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// #region Classes
export class Base64 {
static CHARS
static REVERSE
static toBase64(_) {}
static fromBase64(_) {}
static encode(_) {}
Expand Down
Loading