Yet another Base64 transcoder in pure-JS with comprehensive UTF-8 support and cross-platform compatibility
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core functionality for converting UTF-8 strings to/from Base64 with proper Unicode handling and support for both standard and URL-safe variants.
Converts a UTF-8 string to Base64 with optional URL-safe encoding.
/**
* Converts a UTF-8-encoded string to a Base64 string
* @param src - The UTF-8 string to encode
* @param urlsafe - If true, make the result URL-safe (RFC4648 §5)
* @returns Base64 encoded string
*/
function encode(src: string, urlsafe?: boolean): string;
/**
* Alias for encode function
*/
function toBase64(src: string, urlsafe?: boolean): string;Usage Examples:
import { encode, toBase64 } from "js-base64";
// Basic encoding
const basic = encode("Hello World!");
console.log(basic); // "SGVsbG8gV29ybGQh"
// UTF-8 encoding (handles Unicode properly)
const utf8 = encode("小飼弾");
console.log(utf8); // "5bCP6aO85by+"
// URL-safe encoding
const urlSafe = encode("小飼弾", true);
console.log(urlSafe); // "5bCP6aO85by-" (note the trailing -)
// Using alias
const alias = toBase64("Hello World!");
console.log(alias); // "SGVsbG8gV29ybGQh"Convenience function for URL-safe Base64 encoding (RFC4648 §5).
/**
* Converts a UTF-8-encoded string to URL-safe Base64 RFC4648 §5
* @param src - The UTF-8 string to encode
* @returns URL-safe Base64 encoded string (uses - and _ instead of + and /)
*/
function encodeURI(src: string): string;
/**
* Alias for encodeURI function
*/
function encodeURL(src: string): string;Usage Examples:
import { encodeURI, encodeURL } from "js-base64";
const text = "Hello+World/Test=";
// URL-safe encoding
const urlSafe = encodeURI(text);
console.log(urlSafe); // "SGVsbG8rV29ybGQvVGVzdD0"
// Using alias
const alias = encodeURL(text);
console.log(alias); // "SGVsbG8rV29ybGQvVGVzdD0"
// Comparison with regular encoding
const regular = encode(text);
console.log(regular); // "SGVsbG8rV29ybGQvVGVzdD0=" (with padding)
const urlSafeExplicit = encode(text, true);
console.log(urlSafeExplicit); // "SGVsbG8rV29ybGQvVGVzdD0" (no padding)Converts a Base64 string back to UTF-8, supporting both standard and URL-safe formats.
/**
* Converts a Base64 string to a UTF-8 string
* @param src - Base64 string (both normal and URL-safe are supported)
* @returns Decoded UTF-8 string
*/
function decode(src: string): string;
/**
* Alias for decode function
*/
function fromBase64(src: string): string;Usage Examples:
import { decode, fromBase64 } from "js-base64";
// Basic decoding
const decoded = decode("SGVsbG8gV29ybGQh");
console.log(decoded); // "Hello World!"
// UTF-8 decoding
const utf8Decoded = decode("5bCP6aO85by+");
console.log(utf8Decoded); // "小飼弾"
// URL-safe decoding (automatically detected)
const urlSafeDecoded = decode("5bCP6aO85by-");
console.log(urlSafeDecoded); // "小飼弾"
// Works with or without padding
const withoutPadding = decode("SGVsbG8gV29ybGQ");
const withPadding = decode("SGVsbG8gV29ybGQ=");
console.log(withoutPadding); // "Hello World"
console.log(withPadding); // "Hello World"
// Using alias
const alias = fromBase64("SGVsbG8gV29ybGQh");
console.log(alias); // "Hello World!"
// Handles whitespace
const withSpaces = decode("SGVs bG8g V29y bGQh");
console.log(withSpaces); // "Hello World!"