From 075d35fddb853a8e71831ea2d87d33693608adcc Mon Sep 17 00:00:00 2001 From: Fahim Faisaal <57553028+fahimfaisaal@users.noreply.github.com> Date: Tue, 29 Mar 2022 14:54:21 +0600 Subject: [PATCH] merge: Improved `ROT13` function (#964) * feat: improved memoize function used Map instead of object & used the JSON.stringfy method for generate a valid string as a key * docs: modified documentation * style: format with standard * docs: modified stringify doc * refactor: remove two repetition implementation * feat: added validation, test codes * chore: remove useless words * feat: added types for jest * chore: added link box * feat: added new validation test casses & methods * style: formated with standard * feat: added parse method & test cases * docs: added js docs * chore: added default import export * feat: imporved algorithm via replace method * test: added two test cases * feat: added jest type for suggestions * feat: added `reduceRight` & `trim` method * chore: added helper variable * feat: added new rotation option * Revert "chore: added helper variable" This reverts commit 489544da0a3d479910fbea020d3be3d0d10681bf. * remove: yarn lock * chore: fix grammer * feat: remove revert * chore: added new line --- Ciphers/ROT13.js | 33 ++++++++++++++++++++------------- Ciphers/test/ROT13.test.js | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 Ciphers/test/ROT13.test.js diff --git a/Ciphers/ROT13.js b/Ciphers/ROT13.js index e644b2028..60208a3e1 100644 --- a/Ciphers/ROT13.js +++ b/Ciphers/ROT13.js @@ -1,17 +1,24 @@ /** - * Transcipher a ROT13 cipher - * @param {String} text - string to be encrypted - * @return {String} - decrypted string + * @function ROT13 + * @description - ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. ROT13 is a special case of the Caesar cipher which was developed in ancient Rome. Because there are 26 letters (2×13) in the basic Latin alphabet, ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption. + * @see - [wiki](https://en.wikipedia.org/wiki/ROT13) + * @param {String} str - string to be decrypted + * @return {String} decrypted string */ -const ROT13 = (text) => { - const originalCharacterList = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' - const toBeMappedCharaterList = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm' - const index = x => originalCharacterList.indexOf(x) - const replace = x => index(x) > -1 ? toBeMappedCharaterList[index(x)] : x - return text.split('').map(replace).join('') +function ROT13 (str) { + if (typeof str !== 'string') { + throw new TypeError('Argument should be string') + } + + return str.replace(/[a-z]/gi, (char) => { + const charCode = char.charCodeAt() + + if (/[n-z]/i.test(char)) { + return String.fromCharCode(charCode - 13) + } + + return String.fromCharCode(charCode + 13) + }) } -export { ROT13 } - -// > ROT13('The quick brown fox jumps over the lazy dog') -// 'Gur dhvpx oebja sbk whzcf bire gur ynml qbt' +export default ROT13 diff --git a/Ciphers/test/ROT13.test.js b/Ciphers/test/ROT13.test.js new file mode 100644 index 000000000..82aa0837a --- /dev/null +++ b/Ciphers/test/ROT13.test.js @@ -0,0 +1,18 @@ +import ROT13 from '../ROT13' + +describe('Testing ROT13 function', () => { + it('Test - 1, passing a non-string as an argument', () => { + expect(() => ROT13(0x345)).toThrow() + expect(() => ROT13(123)).toThrow() + expect(() => ROT13(123n)).toThrow() + expect(() => ROT13(false)).toThrow() + expect(() => ROT13({})).toThrow() + expect(() => ROT13([])).toThrow() + }) + + it('Test - 2, passing a string as an argument', () => { + expect(ROT13('Uryyb Jbeyq')).toBe('Hello World') + expect(ROT13('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')).toBe('NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm') + expect(ROT13('The quick brown fox jumps over the lazy dog')).toBe('Gur dhvpx oebja sbk whzcf bire gur ynml qbt') + }) +})