This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
morse-pro.js
256 lines (245 loc) · 7.57 KB
/
morse-pro.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*!
This code is © Copyright Stephen C. Phillips, 2018.
Email: steve@scphillips.com
*/
/*
Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
You may not use this work except in compliance with the Licence.
You may obtain a copy of the Licence at: https://joinup.ec.europa.eu/community/eupl/
Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Licence for the specific language governing permissions and limitations under the Licence.
*/
/**
* Basic methods to translate Morse code.
*/
if (typeof(String.prototype.trim) === "undefined") {
String.prototype.trim = function() {
return String(this).replace(/^\s+|\s+$/g, '');
};
}
var text2morseH = {
'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': "-----",
'.': ".-.-.-",
',': "--..--",
':': "---...",
'?': "..--..",
'\'': ".----.",
'-': "-....-",
'/': "-..-.",
'(': "-.--.",
')': "-.--.-",
'"': ".-..-.",
'@': ".--.-.",
'=': "-...-",
'&': ".-...",
'+': ".-.-.",
'!': "-.-.--",
' ': "/" //Not morse but helps translation
};
var morse2textH = {};
var prosign2morseH = {
'<AA>': '.-.-',
'<AR>': '.-.-.',
'<AS>': '.-...',
'<BK>': '-...-.-',
'<BT>': '-...-', // also <TV>
'<CL>': '-.-..-..',
'<CT>': '-.-.-',
'<DO>': '-..---',
'<KN>': '-.--.',
'<SK>': '...-.-', // also <VA>
'<VA>': '...-.-',
'<SN>': '...-.', // also <VE>
'<VE>': '...-.',
'<SOS>': '...---...'
};
var morsepro2textH = {};
var text2morseproH = {};
for (var text in text2morseH) {
text2morseproH[text] = text2morseH[text];
morse2textH[text2morseH[text]] = text;
morsepro2textH[text2morseH[text]] = text;
}
for (var sign in prosign2morseH) {
text2morseproH[sign] = prosign2morseH[sign];
morsepro2textH[prosign2morseH[sign]] = sign;
}
var tidyText = function(text) {
text = text.toUpperCase();
text = text.trim();
text = text.replace(/\s+/g, ' ');
return text;
};
/**
* Translate text to morse in '..- .. / --' form.
* If something in the text is untranslatable then it is surrounded by hash-signs ('#') and a hash is placed in the morse.
* @param {string} text - alphanumeric message
* @param {boolean} useProsigns - true if prosigns are to be used (default is true)
* @return {{message: string, morse: string, hasError: boolean}}
*/
export function text2morse(text, useProsigns = true) {
text = tidyText(text);
var ret = {
morse: "",
message: "",
hasError: false
};
if (text === "") {
return ret;
}
var tokens = [];
var prosign;
var token_length;
while (text.length > 0) {
token_length = 1;
if (useProsigns) {
prosign = text.match(/^<...?>/); // array of matches
if (prosign) {
token_length = prosign[0].length;
}
}
tokens.push(text.slice(0, token_length));
text = text.slice(token_length, text.length);
}
var dict;
if (useProsigns) {
dict = text2morseproH;
} else {
dict = text2morseH;
}
var i, c, t;
for (i = 0; i < tokens.length; i++) {
t = tokens[i];
c = dict[t];
if (c === undefined) {
ret.message += "#" + t + "#";
ret.morse += "# ";
ret.hasError = true;
} else {
ret.message += t;
ret.morse += c + " ";
}
}
ret.morse = ret.morse.slice(0, ret.morse.length - 1);
return ret;
}
/**
* Translate text to morse in 'Di-di-dah dah' form.
* @param {string} text - alphanumeric message
* @param {boolean} useProsigns - true if prosigns are to be used (default is true)
* @return {string}
*/
export function text2ditdah(text, useProsigns) {
// TODO: deal with errors in the translation
var ditdah = text2morse(text, useProsigns).morse + ' '; // get the dots and dashes
ditdah = ditdah.replace(/\./g, 'di~').replace(/\-/g, 'dah~'); // do the basic job
ditdah = ditdah.replace(/~/g, '-'); // replace placeholder with dash
ditdah = ditdah.replace(/\- /g, ' '); // remove trailing dashes
ditdah = ditdah.replace(/di /g, 'dit '); // use 'dit' at end of letter
ditdah = ditdah.replace(/ \/ /g, ', '); // do punctuation
ditdah = ditdah.replace(/^d/, 'D'); // do capitalisation
ditdah = ditdah.replace(/ $/, ''); // remove the space we added
ditdah = ditdah.replace(/([th])$/, '$1.'); // add full-stop if there is anything there
return ditdah;
}
/**
* Canonicalise morse text.
* Canonical form matches [.-/ ]*, has single spaces between characters, has words separated by ' / ', and has no spaces at the start or end.
* A single '/' may be returned by this function.
* @param {string} morse - Morse code matching [.-_/| ]*
* @return {string} Morse code in canonical form matching [.-/ ]*
*/
var tidyMorse = function(morse) {
morse = morse.replace(/\|/g, "/"); // unify the word separator
morse = morse.replace(/\//g, " / "); // make sure word separators are spaced out
morse = morse.replace(/\s+/g, " "); // squash multiple spaces into single spaces
morse = morse.replace(/(\/ )+\//g, "/"); // squash multiple word separators
morse = morse.replace(/_/g, "-"); // unify the dash character
morse = morse.replace(/^\s+/, ""); // remove initial whitespace
morse = morse.replace(/\s+$/, ""); // remove trailing whitespace
return morse;
};
/**
* Translate morse to text. Canonicalise the morse first.
* If something in the morse is untranslatable then it is surrounded by hash-signs ('#') and a hash is placed in the text.
* @param {string} morse - morse message using [.-_/| ] characters
* @param {boolean} useProsigns - true if prosigns are to be used (default is true)
* @return {{message: string, morse: string, hasError: boolean}}
*/
export function morse2text(morse, useProsigns = true) {
morse = tidyMorse(morse);
var ret = {
morse: "",
message: "",
hasError: false
};
if (morse === "") {
return ret;
}
var tokens = morse.split(" ");
var dict;
if (useProsigns) {
dict = morsepro2textH;
} else {
dict = morse2textH;
}
var c, t;
for (var i = 0; i < tokens.length; i++) {
t = tokens[i];
c = dict[t];
if (c === undefined) {
ret.morse += "#" + t + "# ";
ret.message += "#";
ret.hasError = true;
} else {
ret.morse += t + " ";
ret.message += c;
}
}
ret.morse = ret.morse.slice(0, ret.morse.length - 1);
return ret;
}
/**
* Determine whether a string is most likely morse code.
* @param {string} input - the text
* @return {boolean} - true if the string only has Morse characters in after executing tidyMorse
*/
export function looksLikeMorse(input) {
input = tidyMorse(input);
return (input.match(/^[/.-][ /.-]*$/) !== null);
}