File tree 2 files changed +101
-0
lines changed
2 files changed +101
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @author mrmagic2020
3
+ * @description Enciphers a combination of letters, numbers and symbols into morse code.
4
+ * @see https://en.wikipedia.org/wiki/Morse_code
5
+ * @param {string } msg The message to be enciphered.
6
+ * @param {string } dot Symbol representing the dots.
7
+ * @param {string } dash Symbol representing the dash.
8
+ * @returns {string } Enciphered morse code.
9
+ * @example morse('Hello World!') = '**** * *-** *-** --- *-- --- *-* *-** -** -*-*--'
10
+ */
11
+ const morse = ( msg , dot = '*' , dash = '-' ) => {
12
+ const key = {
13
+ A : '*-' ,
14
+ B : '-***' ,
15
+ C : '-*-*' ,
16
+ D : '-**' ,
17
+ E : '*' ,
18
+ F : '**-*' ,
19
+ G : '--*' ,
20
+ H : '****' ,
21
+ I : '**' ,
22
+ J : '*---' ,
23
+ K : '-*-' ,
24
+ L : '*-**' ,
25
+ M : '--' ,
26
+ N : '-*' ,
27
+ O : '---' ,
28
+ P : '*--*' ,
29
+ Q : '--*-' ,
30
+ R : '*-*' ,
31
+ S : '***' ,
32
+ T : '-' ,
33
+ U : '**-' ,
34
+ V : '***-' ,
35
+ W : '*--' ,
36
+ X : '-**-' ,
37
+ Y : '-*--' ,
38
+ Z : '--**' ,
39
+ 1 : '*----' ,
40
+ 2 : '**---' ,
41
+ 3 : '***--' ,
42
+ 4 : '****-' ,
43
+ 5 : '*****' ,
44
+ 6 : '-****' ,
45
+ 7 : '--***' ,
46
+ 8 : '---**' ,
47
+ 9 : '----*' ,
48
+ 0 : '-----' ,
49
+ '.' : '*-*-*-' ,
50
+ ',' : '--**--' ,
51
+ '?' : '**--**' ,
52
+ '!' : '-*-*--' ,
53
+ '\'' : '*----*' ,
54
+ '"' : '*-**-*' ,
55
+ '(' : '-*--*' ,
56
+ ')' : '-*--*-' ,
57
+ '&' : '*-***' ,
58
+ ':' : '---***' ,
59
+ ';' : '-*-*-*' ,
60
+ '/' : '-**-*' ,
61
+ _ : '**--*-' ,
62
+ '=' : '-***-' ,
63
+ '+' : '*-*-*' ,
64
+ '-' : '-****-' ,
65
+ $ : '***-**-' ,
66
+ '@' : '*--*-*'
67
+ }
68
+
69
+ let newMsg = ''
70
+
71
+ msg . toString ( ) . split ( '' ) . forEach ( ( e ) => {
72
+ if ( / [ a - z A - Z ] / . test ( e ) ) {
73
+ newMsg += key [ e . toUpperCase ( ) ] . replaceAll ( '*' , dot ) . replaceAll ( '-' , dash )
74
+ } else if ( Object . keys ( key ) . includes ( e ) ) {
75
+ newMsg += key [ e ] . replaceAll ( '*' , dot ) . replaceAll ( '-' , dash )
76
+ } else {
77
+ newMsg += e
78
+ }
79
+ newMsg += ' '
80
+ } )
81
+
82
+ return newMsg . trim ( )
83
+ }
84
+
85
+ export { morse }
Original file line number Diff line number Diff line change
1
+ import { morse } from '../MorseCode'
2
+
3
+ describe ( 'Testing morse function' , ( ) => {
4
+ it ( 'should return an enciphered string with a given input string' , ( ) => {
5
+ expect ( morse ( 'Hello World!' ) ) . toBe ( '**** * *-** *-** --- *-- --- *-* *-** -** -*-*--' )
6
+ expect ( morse ( '1+1=2' ) ) . toBe ( '*---- *-*-* *---- -***- **---' )
7
+ } )
8
+
9
+ it ( 'should leave symbols that does not have its corresponding morse representation' , ( ) => {
10
+ expect ( morse ( '© 2023 GitHub, Inc.' ) ) . toBe ( '© **--- ----- **--- ***-- --* ** - **** **- -*** --**-- ** -* -*-* *-*-*-' )
11
+ } )
12
+
13
+ it ( 'should be able to accept custom morse code symbols' , ( ) => {
14
+ expect ( morse ( 'Nodejs' , '.' , '|' ) ) . toBe ( '|. ||| |.. . .||| ...' )
15
+ } )
16
+ } )
You can’t perform that action at this time.
0 commit comments