From 331a4d26cf76174de02924150ac1e3c8e9e32c5a Mon Sep 17 00:00:00 2001 From: Arnold Zhou <104749673+mrmagic2020@users.noreply.github.com> Date: Mon, 1 May 2023 16:17:47 +1000 Subject: [PATCH] feat: Ciphers/MorseCode Algorithm (#1315) * [feat] New algorithm * [test] Add new test for ParityOutlier.js * [fix] Reset indentation * [fix] Reset indentation * [fix] Style changes * fix: improve code efficiency and a glitch * test: adds a new possible test case * fix: style fix * fix: delete redundant comments and else statements * [fix] style fix * feat: New algorithm * fix: fixed custom code symbols * test: add test for MorseCode * test: add case with custom code symbols * delete files from main branch * fix: style fix * fix: style fix * fix: delete unnecessary quotes --- Ciphers/MorseCode.js | 85 ++++++++++++++++++++++++++++++++++ Ciphers/test/MorseCode.test.js | 16 +++++++ 2 files changed, 101 insertions(+) create mode 100644 Ciphers/MorseCode.js create mode 100644 Ciphers/test/MorseCode.test.js diff --git a/Ciphers/MorseCode.js b/Ciphers/MorseCode.js new file mode 100644 index 000000000..9c64a39a2 --- /dev/null +++ b/Ciphers/MorseCode.js @@ -0,0 +1,85 @@ +/** + * @author mrmagic2020 + * @description Enciphers a combination of letters, numbers and symbols into morse code. + * @see https://en.wikipedia.org/wiki/Morse_code + * @param {string} msg The message to be enciphered. + * @param {string} dot Symbol representing the dots. + * @param {string} dash Symbol representing the dash. + * @returns {string} Enciphered morse code. + * @example morse('Hello World!') = '**** * *-** *-** --- *-- --- *-* *-** -** -*-*--' + */ +const morse = (msg, dot = '*', dash = '-') => { + const key = { + A: '*-', + B: '-***', + C: '-*-*', + D: '-**', + E: '*', + F: '**-*', + G: '--*', + H: '****', + I: '**', + J: '*---', + K: '-*-', + L: '*-**', + M: '--', + N: '-*', + O: '---', + P: '*--*', + Q: '--*-', + R: '*-*', + S: '***', + T: '-', + U: '**-', + V: '***-', + W: '*--', + X: '-**-', + Y: '-*--', + Z: '--**', + 1: '*----', + 2: '**---', + 3: '***--', + 4: '****-', + 5: '*****', + 6: '-****', + 7: '--***', + 8: '---**', + 9: '----*', + 0: '-----', + '.': '*-*-*-', + ',': '--**--', + '?': '**--**', + '!': '-*-*--', + '\'': '*----*', + '"': '*-**-*', + '(': '-*--*', + ')': '-*--*-', + '&': '*-***', + ':': '---***', + ';': '-*-*-*', + '/': '-**-*', + _: '**--*-', + '=': '-***-', + '+': '*-*-*', + '-': '-****-', + $: '***-**-', + '@': '*--*-*' + } + + let newMsg = '' + + msg.toString().split('').forEach((e) => { + if (/[a-zA-Z]/.test(e)) { + newMsg += key[e.toUpperCase()].replaceAll('*', dot).replaceAll('-', dash) + } else if (Object.keys(key).includes(e)) { + newMsg += key[e].replaceAll('*', dot).replaceAll('-', dash) + } else { + newMsg += e + } + newMsg += ' ' + }) + + return newMsg.trim() +} + +export { morse } diff --git a/Ciphers/test/MorseCode.test.js b/Ciphers/test/MorseCode.test.js new file mode 100644 index 000000000..5dd4e0795 --- /dev/null +++ b/Ciphers/test/MorseCode.test.js @@ -0,0 +1,16 @@ +import { morse } from '../MorseCode' + +describe('Testing morse function', () => { + it('should return an enciphered string with a given input string', () => { + expect(morse('Hello World!')).toBe('**** * *-** *-** --- *-- --- *-* *-** -** -*-*--') + expect(morse('1+1=2')).toBe('*---- *-*-* *---- -***- **---') + }) + + it('should leave symbols that does not have its corresponding morse representation', () => { + expect(morse('© 2023 GitHub, Inc.')).toBe('© **--- ----- **--- ***-- --* ** - **** **- -*** --**-- ** -* -*-* *-*-*-') + }) + + it('should be able to accept custom morse code symbols', () => { + expect(morse('Nodejs', '.', '|')).toBe('|. ||| |.. . .||| ...') + }) +})