From 5cedaa95b3c36324bb1b1059c0537b522b57fb90 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Mar 2022 07:41:48 +0600 Subject: [PATCH 1/2] feat: imporved algorithm via replace method --- Ciphers/Atbash.js | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/Ciphers/Atbash.js b/Ciphers/Atbash.js index 8ab7105fe2..fb0289da2b 100644 --- a/Ciphers/Atbash.js +++ b/Ciphers/Atbash.js @@ -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 - * @param {String} str - string to be decrypted/encrypt - * @return {String} decrypted/encrypted string + * @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) */ -function Atbash (message) { - let decodedString = '' - for (let i = 0; i < message.length; i++) { - 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)) - } +const Atbash = (str) => { + if (typeof str !== 'string') { + throw new TypeError('Argument should be string') } - return decodedString -} -export { Atbash } + 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()) + }) +} -// > Atbash('HELLO WORLD') -// 'SVOOL DLIOW' +export default Atbash From d00a7bff18648b26b4cfdf6bccd81ced330b7180 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Mar 2022 07:43:10 +0600 Subject: [PATCH 2/2] test: added two test cases --- Ciphers/test/Atbash.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Ciphers/test/Atbash.test.js diff --git a/Ciphers/test/Atbash.test.js b/Ciphers/test/Atbash.test.js new file mode 100644 index 0000000000..4db2faa772 --- /dev/null +++ b/Ciphers/test/Atbash.test.js @@ -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') + }) +}) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy