From 489544da0a3d479910fbea020d3be3d0d10681bf Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Mon, 28 Mar 2022 21:33:34 +0600 Subject: [PATCH 1/3] chore: added helper variable --- Ciphers/Atbash.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Ciphers/Atbash.js b/Ciphers/Atbash.js index fb0289da2b..fb0750503f 100644 --- a/Ciphers/Atbash.js +++ b/Ciphers/Atbash.js @@ -11,11 +11,13 @@ const Atbash = (str) => { } return str.replace(/[a-z]/gi, (char) => { + const charCode = char.charCodeAt() + if (/[A-Z]/.test(char)) { - return String.fromCharCode(90 + 65 - char.charCodeAt()) + return String.fromCharCode(90 + 65 - charCode) } - return String.fromCharCode(122 + 97 - char.charCodeAt()) + return String.fromCharCode(122 + 97 - charCode) }) } From 68cb5ad790b99c125909eebc7d7eb13e43d78e8c Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Tue, 29 Mar 2022 06:50:12 +0600 Subject: [PATCH 2/3] feat: added new rotation option --- Ciphers/CaesarsCipher.js | 51 ++++++++++++++---------------- Ciphers/test/CaesarsCipher.test.js | 16 ++++++++++ 2 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 Ciphers/test/CaesarsCipher.test.js diff --git a/Ciphers/CaesarsCipher.js b/Ciphers/CaesarsCipher.js index 20e40c1ec4..2eed471a50 100644 --- a/Ciphers/CaesarsCipher.js +++ b/Ciphers/CaesarsCipher.js @@ -1,35 +1,32 @@ /** - * Caesar's Cipher - also known as the ROT13 Cipher is when - * a letter is replaced by the one that is 13 spaces away - * from it in the alphabet. If the letter is in the first half - * of the alphabet we add 13, if it's in the latter half we - * subtract 13 from the character code value. + * @function caesarsCipher + * @description - In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence. + * @see - [wiki](https://en.wikipedia.org/wiki/Caesar_cipher) + * @param {string} str - string to be encrypted + * @param {number} rotation - the number of rotation, expect real number ( > 0) + * @return {string} - decrypted string */ +const caesarsCipher = (str, rotation) => { + if (typeof str !== 'string' || !Number.isInteger(rotation) || rotation < 0) { + throw new TypeError('Arguments are invalid') + } -/** - * Decrypt a ROT13 cipher - * @param {String} str - string to be decrypted - * @return {String} decrypted string - */ -function rot13 (str) { - const response = [] - const strLength = str.length + const alphabets = new Array(26) + .fill() + .map((_, index) => String.fromCharCode(97 + index)) // generate all lower alphabets array a-z - for (let i = 0; i < strLength; i++) { - const char = str.charCodeAt(i) + const cipherMap = alphabets.reduce( + (map, char, index) => map.set(char, alphabets[(rotation + index) % 26]), + new Map() + ) - if (char < 65 || (char > 90 && char < 97) || char > 122) { - response.push(str.charAt(i)) - } else if ((char > 77 && char <= 90) || (char > 109 && char <= 122)) { - response.push(String.fromCharCode(str.charCodeAt(i) - 13)) - } else { - response.push(String.fromCharCode(str.charCodeAt(i) + 13)) + return str.replace(/[a-z]/gi, (char) => { + if (/[A-Z]/.test(char)) { + return cipherMap.get(char.toLowerCase()).toUpperCase() } - } - return response.join('') -} -export { rot13 } + return cipherMap.get(char) + }) +} -// > rot13('Uryyb Jbeyq') -// 'Hello World' +export default caesarsCipher diff --git a/Ciphers/test/CaesarsCipher.test.js b/Ciphers/test/CaesarsCipher.test.js new file mode 100644 index 0000000000..d78fe5cfc4 --- /dev/null +++ b/Ciphers/test/CaesarsCipher.test.js @@ -0,0 +1,16 @@ +import caesarsCipher from '../CaesarsCipher' + +describe('Testing the caesarsCipher function', () => { + it('Test - 1, Testing for invalid types', () => { + expect(() => caesarsCipher(false, 3)).toThrow() + expect(() => caesarsCipher('false', -1)).toThrow() + expect(() => caesarsCipher('true', null)).toThrow() + }) + + it('Test - 2, Testing for valid string and rotation', () => { + expect(caesarsCipher('middle-Outz', 2)).toBe('okffng-Qwvb') + expect(caesarsCipher('abcdefghijklmnopqrstuvwxyz', 3)).toBe('defghijklmnopqrstuvwxyzabc') + expect(caesarsCipher('Always-Look-on-the-Bright-Side-of-Life', 5)).toBe('Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj') + expect(caesarsCipher('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 23)).toBe('QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD') + }) +}) From addf7212645a393989ce0c8ae4606ff8c20eaf51 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Tue, 29 Mar 2022 07:09:46 +0600 Subject: [PATCH 3/3] Revert "chore: added helper variable" This reverts commit 489544da0a3d479910fbea020d3be3d0d10681bf. --- Ciphers/Atbash.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Ciphers/Atbash.js b/Ciphers/Atbash.js index fb0750503f..fb0289da2b 100644 --- a/Ciphers/Atbash.js +++ b/Ciphers/Atbash.js @@ -11,13 +11,11 @@ const Atbash = (str) => { } 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(90 + 65 - char.charCodeAt()) } - return String.fromCharCode(122 + 97 - charCode) + return String.fromCharCode(122 + 97 - char.charCodeAt()) }) }