Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathAtbash.js
More file actions
Latest commit
24 lines (20 loc) · 842 Bytes
/
Copy pathAtbash.js
File metadata and controls
24 lines (20 loc) · 842 Bytes
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)
*/
constAtbash=(str)=>{
if(typeofstr!=='string'){
thrownewTypeError('Argument should be string')
}
returnstr.replace(/[a-z]/gi,(char)=>{
constcharCode=char.charCodeAt()
if(/[A-Z]/.test(char)){
returnString.fromCharCode(90+65-charCode)
}
returnString.fromCharCode(122+97-charCode)
})
}
exportdefaultAtbash