Files
exercise-typescript/typescript/atbash-cipher/atbash-cipher.ts

22 lines
598 B
TypeScript

const originalAlphabet = 'abcdefghijklmnopqrstuvwxyz'
const cipherAlphabet = 'zyxwvutsrqponmlkjihgfedcba'
export function encode(plainText: string): string {
return plainText
.toLowerCase()
.split('')
.map(c => c.match(/\d/) ? c : cipherAlphabet[originalAlphabet.indexOf(c)])
.filter(Boolean)
.map((c, i) => (i + 1) % 5 === 0 ? `${c} ` : c)
.join("")
.trim()
}
export function decode(cipherText: string): string {
return cipherText
.split('')
.map(c => c.match(/\d/) ? c : originalAlphabet[cipherAlphabet.indexOf(c)])
.filter(Boolean)
.join("")
}