mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-23 00:06:15 -04:00
60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
export { textToBase64, base64ToText, isValidBase64, removePotentialDataAndMimePrefix };
|
|
|
|
function textToBase64(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boolean } = {}) {
|
|
const encoded = window.btoa(str);
|
|
return makeUrlSafe ? makeUriSafe(encoded) : encoded;
|
|
}
|
|
|
|
function base64ToText(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boolean } = {}) {
|
|
if (!isValidBase64(str, { makeUrlSafe })) {
|
|
throw new Error('Incorrect base64 string');
|
|
}
|
|
|
|
let cleanStr = removePotentialDataAndMimePrefix(str);
|
|
if (makeUrlSafe) {
|
|
cleanStr = unURI(cleanStr);
|
|
}
|
|
|
|
try {
|
|
return window.atob(cleanStr);
|
|
}
|
|
catch (_) {
|
|
throw new Error('Incorrect base64 string');
|
|
}
|
|
}
|
|
|
|
function removePotentialDataAndMimePrefix(str: string) {
|
|
return str.replace(/^data:.*?;base64,/, '');
|
|
}
|
|
|
|
function isValidBase64(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boolean } = {}) {
|
|
let cleanStr = removePotentialDataAndMimePrefix(str);
|
|
if (makeUrlSafe) {
|
|
cleanStr = unURI(cleanStr);
|
|
}
|
|
|
|
try {
|
|
if (makeUrlSafe) {
|
|
return removePotentialPadding(window.btoa(window.atob(cleanStr))) === cleanStr;
|
|
}
|
|
return window.btoa(window.atob(cleanStr)) === cleanStr;
|
|
}
|
|
catch (err) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function makeUriSafe(encoded: string) {
|
|
return encoded.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
}
|
|
|
|
function unURI(encoded: string): string {
|
|
return encoded
|
|
.replace(/-/g, '+')
|
|
.replace(/_/g, '/')
|
|
.replace(/[^A-Za-z0-9+/]/g, '');
|
|
}
|
|
|
|
function removePotentialPadding(str: string) {
|
|
return str.replace(/=/g, '');
|
|
}
|