Files
JavaScript/Ciphers/Atbash.js
Fahim Faisaal 47c1c51bc9 merge: Improved ciphers (#954)
* feat: imporved algorithm via replace method

* test: added two test cases
2022-03-27 22:45:14 +05:30

23 lines
820 B
JavaScript

/**
* @function Atbash - Decrypt a Atbash cipher
* @description - The Atbash cipher is a particular type of monoalphabetic cipher formed by taking the alphabet and mapping it to its reverse, so that the first letter becomes the last letter, the second letter becomes the second to last letter, and so on.
* @param {string} str - string to be decrypted/encrypt
* @return {string} decrypted/encrypted string
* @see - [wiki](https://en.wikipedia.org/wiki/Atbash)
*/
const Atbash = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Argument should be string')
}
return str.replace(/[a-z]/gi, (char) => {
if (/[A-Z]/.test(char)) {
return String.fromCharCode(90 + 65 - char.charCodeAt())
}
return String.fromCharCode(122 + 97 - char.charCodeAt())
})
}
export default Atbash