API参考
核心函数
encodeBase64(text: string, format?: string): string
将文本编码为Base64格式。
参数:
text
: 要编码的文本字符串format
: 输出格式(可选),默认为"standard"
返回值:
- 编码后的Base64字符串
示例:
const encoded = encodeBase64('Hello World');
// 返回: "SGVsbG8gV29ybGQ="
decodeBase64(text: string, format?: string): string
将Base64字符串解码为原始文本。
参数:
text
: 要解码的Base64字符串format
: 输入格式(可选),默认为"standard"
返回值:
- 解码后的原始文本
示例:
const decoded = decodeBase64('SGVsbG8gV29ybGQ=');
// 返回: "Hello World"
输出格式
标准格式 (standard)
使用RFC 4648标准Base64字符集:
- 字符集:A-Z, a-z, 0-9, +, /
- 填充字符:=
URL安全格式 (urlSafe)
使用URL安全的Base64变体:
- 字符集:A-Z, a-z, 0-9, -, _
- 无填充字符
无填充格式 (noPadding)
移除所有填充字符的标准Base64:
- 字符集:A-Z, a-z, 0-9, +, /
- 无填充字符
错误处理
常见错误类型
InvalidInputError
当输入不是有效的Base64字符串时抛出。
错误信息:
"Invalid Base64 input: character not in valid set"
DecodeError
当解码过程中发生错误时抛出。
错误信息:
"Failed to decode Base64 string"
FileSizeError
当文件大小超过限制时抛出。
错误信息:
"File size exceeds maximum limit"
配置选项
默认配置
const defaultConfig = {
maxFileSize: 10 * 1024 * 1024, // 10MB
supportedFormats: ['standard', 'urlSafe', 'noPadding'],
autoClear: true,
historyLimit: 100,
};
自定义配置
const customConfig = {
maxFileSize: 50 * 1024 * 1024, // 50MB
supportedFormats: ['standard'],
autoClear: false,
historyLimit: 50,
};
事件系统
转换事件
// 转换开始
on('convert:start', (data) => {
console.log('转换开始:', data);
});
// 转换完成
on('convert:complete', (result) => {
console.log('转换完成:', result);
});
// 转换错误
on('convert:error', (error) => {
console.error('转换错误:', error);
});
历史记录事件
// 历史记录添加
on('history:add', (record) => {
console.log('添加历史记录:', record);
});
// 历史记录清除
on('history:clear', () => {
console.log('历史记录已清除');
});