mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-08 15:15:02 -04:00
feat(base64-string-converter): changes based on review comments, use config object instead of boolean argument.
This commit is contained in:
parent
e9d5429f30
commit
01e7cfaefb
2 changed files with 20 additions and 20 deletions
|
@ -8,32 +8,32 @@ describe('base64 utils', () => {
|
|||
expect(textToBase64('a')).to.eql('YQ==');
|
||||
expect(textToBase64('lorem ipsum')).to.eql('bG9yZW0gaXBzdW0=');
|
||||
expect(textToBase64('-1')).to.eql('LTE=');
|
||||
expect(textToBase64('<<<????????>>>')).to.eql('PDw8Pz8/Pz8/Pz8+Pj4=');
|
||||
expect(textToBase64('<<<????????>>>', { makeUrlSafe: false })).to.eql('PDw8Pz8/Pz8/Pz8+Pj4=');
|
||||
});
|
||||
it('should convert string into url safe base64', () => {
|
||||
expect(textToBase64('', true)).to.eql('');
|
||||
expect(textToBase64('a', true)).to.eql('YQ');
|
||||
expect(textToBase64('lorem ipsum', true)).to.eql('bG9yZW0gaXBzdW0');
|
||||
expect(textToBase64('<<<????????>>>', true)).to.eql('PDw8Pz8_Pz8_Pz8-Pj4');
|
||||
expect(textToBase64('', { makeUrlSafe: true })).to.eql('');
|
||||
expect(textToBase64('a', { makeUrlSafe: true })).to.eql('YQ');
|
||||
expect(textToBase64('lorem ipsum', { makeUrlSafe: true })).to.eql('bG9yZW0gaXBzdW0');
|
||||
expect(textToBase64('<<<????????>>>', { makeUrlSafe: true })).to.eql('PDw8Pz8_Pz8_Pz8-Pj4');
|
||||
});
|
||||
});
|
||||
|
||||
describe('base64ToText', () => {
|
||||
it('should convert base64 into text', () => {
|
||||
expect(base64ToText('')).to.eql('');
|
||||
expect(base64ToText('YQ==')).to.eql('a');
|
||||
expect(base64ToText('YQ==', { makeUrlSafe: false })).to.eql('a');
|
||||
expect(base64ToText('bG9yZW0gaXBzdW0=')).to.eql('lorem ipsum');
|
||||
expect(base64ToText('data:text/plain;base64,bG9yZW0gaXBzdW0=')).to.eql('lorem ipsum');
|
||||
expect(base64ToText('LTE=')).to.eql('-1');
|
||||
});
|
||||
|
||||
it('should convert url safe base64 into text', () => {
|
||||
expect(base64ToText('', true)).to.eql('');
|
||||
expect(base64ToText('YQ', true)).to.eql('a');
|
||||
expect(base64ToText('bG9yZW0gaXBzdW0', true)).to.eql('lorem ipsum');
|
||||
expect(base64ToText('data:text/plain;base64,bG9yZW0gaXBzdW0', true)).to.eql('lorem ipsum');
|
||||
expect(base64ToText('LTE', true)).to.eql('-1');
|
||||
expect(base64ToText('PDw8Pz8_Pz8_Pz8-Pj4', true)).to.eql('<<<????????>>>');
|
||||
expect(base64ToText('', { makeUrlSafe: true })).to.eql('');
|
||||
expect(base64ToText('YQ', { makeUrlSafe: true })).to.eql('a');
|
||||
expect(base64ToText('bG9yZW0gaXBzdW0', { makeUrlSafe: true })).to.eql('lorem ipsum');
|
||||
expect(base64ToText('data:text/plain;base64,bG9yZW0gaXBzdW0', { makeUrlSafe: true })).to.eql('lorem ipsum');
|
||||
expect(base64ToText('LTE', { makeUrlSafe: true })).to.eql('-1');
|
||||
expect(base64ToText('PDw8Pz8_Pz8_Pz8-Pj4', { makeUrlSafe: true })).to.eql('<<<????????>>>');
|
||||
});
|
||||
|
||||
it('should throw for incorrect base64 string', () => {
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
export { textToBase64, base64ToText, isValidBase64, removePotentialDataAndMimePrefix };
|
||||
|
||||
function textToBase64(str: string, urlSafe = false) {
|
||||
function textToBase64(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boolean } = {}) {
|
||||
const encoded = window.btoa(str);
|
||||
return urlSafe ? makeUriSafe(encoded) : encoded;
|
||||
return makeUrlSafe ? makeUriSafe(encoded) : encoded;
|
||||
}
|
||||
|
||||
function base64ToText(str: string, urlSafe = false) {
|
||||
if (!isValidBase64(str, urlSafe)) {
|
||||
function base64ToText(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boolean } = {}) {
|
||||
if (!isValidBase64(str, { makeUrlSafe: makeUrlSafe })) {
|
||||
throw new Error('Incorrect base64 string');
|
||||
}
|
||||
|
||||
let cleanStr = removePotentialDataAndMimePrefix(str);
|
||||
if (urlSafe) {
|
||||
if (makeUrlSafe) {
|
||||
cleanStr = unURI(cleanStr);
|
||||
}
|
||||
|
||||
|
@ -26,14 +26,14 @@ function removePotentialDataAndMimePrefix(str: string) {
|
|||
return str.replace(/^data:.*?;base64,/, '');
|
||||
}
|
||||
|
||||
function isValidBase64(str: string, urlSafe = false) {
|
||||
function isValidBase64(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boolean } = {}) {
|
||||
let cleanStr = removePotentialDataAndMimePrefix(str);
|
||||
if (urlSafe) {
|
||||
if (makeUrlSafe) {
|
||||
cleanStr = unURI(cleanStr);
|
||||
}
|
||||
|
||||
try {
|
||||
if (urlSafe) {
|
||||
if (makeUrlSafe) {
|
||||
return removePotentialPadding(window.btoa(window.atob(cleanStr))) === cleanStr;
|
||||
}
|
||||
return window.btoa(window.atob(cleanStr)) === cleanStr;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue