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
Validate Base64 strings and access version information with comprehensive validation logic that handles multiple Base64 formats.
Validates whether a value is a properly formatted Base64 string.
/**
* Check if a value is a valid Base64 string
* @param src - A value to check (typically string, but accepts any type)
* @returns true if the value is a valid Base64 string, false otherwise
*/
function isValid(src: unknown): boolean;Usage Examples:
import { isValid } from "js-base64";
// Basic validation
console.log(isValid("SGVsbG8=")); // true - valid Base64
console.log(isValid("Hello")); // false - not Base64
console.log(isValid("")); // true - empty string is valid Base64 (represents empty data)
// Type checking
console.log(isValid(123)); // false - not a string
console.log(isValid(null)); // false - not a string
console.log(isValid(undefined)); // false - not a string
// Padding validation
console.log(isValid("SGVsbG8=")); // true - with proper padding
console.log(isValid("SGVsbG8")); // true - padding can be omitted
console.log(isValid("SGVsbG")); // true - partial padding omitted
// Format variations
console.log(isValid("SGVs bG8=")); // true - whitespace is allowed
console.log(isValid("SGVsbG8+")); // true - standard Base64 characters
console.log(isValid("SGVsbG8-")); // true - URL-safe Base64 characters
// Invalid formats
console.log(isValid("SGVs+-bG8=")); // false - can't mix standard and URL-safe
console.log(isValid("SGVs@bG8=")); // false - invalid character (@)
console.log(isValid("SGVs=====")); // false - too much padding
// Real-world usage
function processBase64Input(input: unknown): string | null {
if (isValid(input)) {
return decode(input as string);
}
return null;
}
// Form validation
function validateBase64Field(value: string): { isValid: boolean; error?: string } {
if (!value.trim()) {
return { isValid: true }; // Empty is valid
}
if (!isValid(value)) {
return {
isValid: false,
error: "Please enter a valid Base64 string"
};
}
return { isValid: true };
}Access the library version for compatibility checking and debugging.
/**
* Current version of the js-base64 library
*/
const version: string;
/**
* @deprecated Use lowercase 'version' instead
* Legacy uppercase version constant
*/
const VERSION: string;Usage Examples:
import { version, VERSION } from "js-base64";
// Check current version
console.log(version); // "3.7.8"
// Version compatibility checking
function checkCompatibility() {
const minVersion = "3.0.0";
const currentVersion = version;
// Simple version comparison (for more complex needs, use a semver library)
const [major, minor] = currentVersion.split('.').map(Number);
const [minMajor, minMinor] = minVersion.split('.').map(Number);
if (major > minMajor || (major === minMajor && minor >= minMinor)) {
console.log("Compatible version");
} else {
console.log("Version too old");
}
}
// Debugging information
function getDebugInfo() {
return {
library: "js-base64",
version: version,
environment: typeof window !== 'undefined' ? 'browser' : 'node',
hasBuffer: typeof Buffer !== 'undefined',
hasTextEncoder: typeof TextEncoder !== 'undefined'
};
}
// Legacy usage (deprecated)
console.log(VERSION); // "3.7.8" - same as version, but deprecatedThe isValid function performs comprehensive validation:
import { isValid, decode } from "js-base64";
// Validation with error details (custom wrapper)
function validateBase64WithDetails(input: unknown): {
valid: boolean;
reason?: string;
data?: string
} {
if (typeof input !== 'string') {
return { valid: false, reason: 'Input must be a string' };
}
if (!isValid(input)) {
return { valid: false, reason: 'Invalid Base64 format' };
}
try {
const decoded = decode(input);
return { valid: true, data: decoded };
} catch (error) {
return { valid: false, reason: 'Base64 decoding failed' };
}
}
// Usage in API validation
function apiEndpoint(request: { data: string }) {
const validation = validateBase64WithDetails(request.data);
if (!validation.valid) {
throw new Error(`Invalid input: ${validation.reason}`);
}
// Process the valid Base64 data
return { decoded: validation.data };
}import { isValid } from "js-base64";
// For large amounts of data, validate before expensive operations
function processBase64Data(inputs: string[]) {
// Pre-filter valid inputs
const validInputs = inputs.filter(isValid);
// Only decode valid inputs
return validInputs.map(decode);
}
// Quick validation for user input
function onUserInput(value: string) {
if (value.length > 1000000) {
// Skip validation for very large strings to avoid UI blocking
return false;
}
return isValid(value);
}