class CaesarCipher {
constructor(shift) {
let alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
let shifted = alphabet.map((l, i) => alphabet[(i + shift) % alphabet.length]);
alphabet = alphabet.concat(alphabet.map(l => l.toUpperCase()));
shifted = shifted.concat(shifted.map(l => l.toUpperCase()));
this.encMap = Object.fromEntries(alphabet.map((l, i) => [l, shifted[i]]));
this.decMap = Object.fromEntries(shifted.map((l, i) => [l, alphabet[i]]));
}
encrypt(plaintext) {
return plaintext.split("").map(l => this.encMap[l] || l).join("");
}
decrypt(ciphertext) {
return ciphertext.split("").map(l => this.decMap[l] || l).join("");
}
}