generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBase64.ts
35 lines (28 loc) · 921 Bytes
/
Base64.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Coder } from "./Coder";
export class Base64Encoder implements Coder {
from = "text";
to = "base64";
transform(text: string): string {
return btoa(text);
}
checkInput(text: string): boolean {
// Check if the input is a string and not null or undefined
return typeof text === "string" && text.length > 0;
}
}
export class Base64Decoder implements Coder {
from = "base64";
to = "text";
transform(text: string): string {
try {
return atob(text);
} catch (e) {
throw new Error("Invalid Base64 input");
}
}
checkInput(text: string): boolean {
// Check if the input is a valid Base64 string
const base64Regex = /^[A-Za-z0-9+/=]+$/;
return typeof text === "string" && base64Regex.test(text) && text.length % 4 === 0;
}
}