forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Atbash.js
24 lines (20 loc) · 842 Bytes
/
Atbash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @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) => {
const charCode = char.charCodeAt()
if (/[A-Z]/.test(char)) {
return String.fromCharCode(90 + 65 - charCode)
}
return String.fromCharCode(122 + 97 - charCode)
})
}
export default Atbash