mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-07 02:05:08 +08:00
merge: Improved ciphers (#954)
* feat: imporved algorithm via replace method * test: added two test cases
This commit is contained in:
@ -1,30 +1,22 @@
|
|||||||
/*
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypt a Atbash cipher
|
* @function Atbash - Decrypt a Atbash cipher
|
||||||
* @param {String} str - string to be decrypted/encrypt
|
* @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.
|
||||||
* @return {String} decrypted/encrypted string
|
* @param {string} str - string to be decrypted/encrypt
|
||||||
|
* @return {string} decrypted/encrypted string
|
||||||
|
* @see - [wiki](https://en.wikipedia.org/wiki/Atbash)
|
||||||
*/
|
*/
|
||||||
function Atbash (message) {
|
const Atbash = (str) => {
|
||||||
let decodedString = ''
|
if (typeof str !== 'string') {
|
||||||
for (let i = 0; i < message.length; i++) {
|
throw new TypeError('Argument should be string')
|
||||||
if (/[^a-zA-Z]/.test(message[i])) {
|
|
||||||
decodedString += message[i]
|
|
||||||
} else if (message[i] === message[i].toUpperCase()) {
|
|
||||||
decodedString += String.fromCharCode(90 + 65 - message.charCodeAt(i))
|
|
||||||
} else {
|
|
||||||
decodedString += String.fromCharCode(122 + 97 - message.charCodeAt(i))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return decodedString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { Atbash }
|
return str.replace(/[a-z]/gi, (char) => {
|
||||||
|
if (/[A-Z]/.test(char)) {
|
||||||
|
return String.fromCharCode(90 + 65 - char.charCodeAt())
|
||||||
|
}
|
||||||
|
|
||||||
// > Atbash('HELLO WORLD')
|
return String.fromCharCode(122 + 97 - char.charCodeAt())
|
||||||
// 'SVOOL DLIOW'
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Atbash
|
||||||
|
17
Ciphers/test/Atbash.test.js
Normal file
17
Ciphers/test/Atbash.test.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import Atbash from '../Atbash'
|
||||||
|
|
||||||
|
describe('Testing Atbash function', () => {
|
||||||
|
it('Test - 1, passing the non-string as an argument', () => {
|
||||||
|
expect(() => Atbash(0x345)).toThrow()
|
||||||
|
expect(() => Atbash(123)).toThrow()
|
||||||
|
expect(() => Atbash(123n)).toThrow()
|
||||||
|
expect(() => Atbash(false)).toThrow()
|
||||||
|
expect(() => Atbash({})).toThrow()
|
||||||
|
expect(() => Atbash([])).toThrow()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Test - 2, passing all alphabets', () => {
|
||||||
|
expect(Atbash('HELLO WORLD')).toBe('SVOOL DLIOW')
|
||||||
|
expect(Atbash('The quick brown fox jumps over the lazy dog')).toBe('Gsv jfrxp yildm ulc qfnkh levi gsv ozab wlt')
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user