Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
raklaptudirm merged 18 commits intoTheAlgorithms:masterfrommrmagic2020:cipher_morse
May 1, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
18 commits
Select commitHold shift + click to select a range
fb9154d
[feat] New algorithm
mrmagic2020Apr 27, 2023
791df98
[test] Add new test for ParityOutlier.js
mrmagic2020Apr 27, 2023
c7eeff2
[fix] Reset indentation
mrmagic2020Apr 27, 2023
2d342dc
[fix] Reset indentation
mrmagic2020Apr 27, 2023
568763d
[fix] Style changes
mrmagic2020Apr 27, 2023
6df2a72
fix: improve code efficiency and a glitch
mrmagic2020Apr 27, 2023
8202eb1
test: adds a new possible test case
mrmagic2020Apr 27, 2023
2dfc2c0
fix: style fix
mrmagic2020Apr 27, 2023
fd4bda4
fix: delete redundant comments and else statements
mrmagic2020Apr 27, 2023
4a4d7e3
[fix] style fix
mrmagic2020Apr 27, 2023
2c0e619
feat: New algorithm
mrmagic2020Apr 28, 2023
e318e11
fix: fixed custom code symbols
mrmagic2020Apr 28, 2023
5435e8d
test: add test for MorseCode
mrmagic2020Apr 28, 2023
09e6fdd
test: add case with custom code symbols
mrmagic2020Apr 28, 2023
6759113
delete files from main branch
mrmagic2020Apr 28, 2023
8288b79
fix: style fix
mrmagic2020Apr 28, 2023
f16b07b
fix: style fix
mrmagic2020Apr 28, 2023
6d2bde9
fix: delete unnecessary quotes
mrmagic2020Apr 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletionsCiphers/MorseCode.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 }
16 changes: 16 additions & 0 deletionsCiphers/test/MorseCode.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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('|. ||| |.. . .||| ...')
})
})

[8]ページ先頭

©2009-2025 Movatter.jp