// Created on savesnippets.com ยท https://savesnippets.com/AxnNi2czruMCGj function toBase64(str) { const bytes = new TextEncoder().encode(str); let binary = ''; bytes.forEach((b) => (binary += String.fromCharCode(b))); return btoa(binary); } function fromBase64(b64) { const binary = atob(b64); const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0)); return new TextDecoder().decode(bytes); } // URL-safe variants (replace +/= with -_~) const toBase64Url = (s) => toBase64(s).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); const fromBase64Url = (s) => fromBase64(s.replace(/-/g, '+').replace(/_/g, '/')); // Usage console.log(toBase64('Hello ๐ŸŒ')); // SGVsbG8g8J+MjQ== console.log(fromBase64('SGVsbG8g8J+MjQ==')); // Hello ๐ŸŒ