On se retrouve aujourd'hui pour la solution du précédent #KataOfTheWeek proposé par Jonathan en début de semaine !

En Javascript :

const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const BYTES_LEN = 24;
const BYTE_LEN = 8;
const ENCODE_64_LEN = 6;

class Base64 {

    static encode = (strToEncode) => {
        let binaryString = strToEncode.split('').reduce((acc, char) => {
            const binaryChar = char.charCodeAt(0).toString(2);
            return acc + '0'.repeat(BYTE_LEN - binaryChar.length) + binaryChar
        }, '');

        const missingBytes = (binaryString.length % BYTES_LEN) / BYTE_LEN;
        binaryString += (missingBytes === 1 ? '0000' : missingBytes === 2 ? '00' : '');

        let base64String = '';
        for (let i = 0; i < binaryString.length; i += ENCODE_64_LEN) {
            base64String += ALPHABET[parseInt(binaryString.substring(i, i + ENCODE_64_LEN), 2)];
        }
        return base64String + (missingBytes === 1 ? '==' : missingBytes === 2 ? '=' : '');
    };

    static decode = (str) => {
        const indexPaddingChar = str.indexOf('=');
        const nbPaddingChar = indexPaddingChar < 0 ? 0 : str.length - indexPaddingChar;

        const stringToDecode = nbPaddingChar > 0 ? str.slice(0, str.length - nbPaddingChar) : str;
        let binaryString = stringToDecode.split('').reduce((acc, char) => {
          const binaryCode = ALPHABET.indexOf(char).toString(2);
          return acc + '0'.repeat(ENCODE_64_LEN - binaryCode.length) + binaryCode;
        }, '');

        binaryString = binaryString.slice(0, binaryString.length - nbPaddingChar * 2);

        let decodedString = '';
        for (let i = 0; i < binaryString.length; i += BYTE_LEN) {
          decodedString += String.fromCharCode(parseInt(binaryString.substring(i, i + BYTE_LEN), 2));
        }
        return decodedString;
    };
};

const firstExample = 'SGVsbG8gdGFraW1hICE=';
const secondExample = 'Vml2ZSBsZSBjb25maW5lbWVudCAh';

const firstResult = Base64.decode(firstExample);
const secondResult = Base64.decode(secondExample);
console.log(`first example -> ${firstResult}`);
console.log(`second example -> ${secondResult}`);

const firstEncoded = Base64.encode(firstResult);
const secondEncoded = Base64.encode(secondResult);
console.assert(firstEncoded === firstExample, "Oups");
console.assert(secondEncoded === secondExample, "Ouch"); 

A bientôt pour un nouveau #KataOfTheWeek !