Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
feat: Ciphers/MorseCode Algorithm#1315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
18 commits Select commitHold shift + click to select a range
fb9154d
[feat] New algorithm
mrmagic2020791df98
[test] Add new test for ParityOutlier.js
mrmagic2020c7eeff2
[fix] Reset indentation
mrmagic20202d342dc
[fix] Reset indentation
mrmagic2020568763d
[fix] Style changes
mrmagic20206df2a72
fix: improve code efficiency and a glitch
mrmagic20208202eb1
test: adds a new possible test case
mrmagic20202dfc2c0
fix: style fix
mrmagic2020fd4bda4
fix: delete redundant comments and else statements
mrmagic20204a4d7e3
[fix] style fix
mrmagic20202c0e619
feat: New algorithm
mrmagic2020e318e11
fix: fixed custom code symbols
mrmagic20205435e8d
test: add test for MorseCode
mrmagic202009e6fdd
test: add case with custom code symbols
mrmagic20206759113
delete files from main branch
mrmagic20208288b79
fix: style fix
mrmagic2020f16b07b
fix: style fix
mrmagic20206d2bde9
fix: delete unnecessary quotes
mrmagic2020File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('|. ||| |.. . .||| ...') | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.