Skip to content

Commit 18d4265

Browse files
committed
fixed AudioContext error on Chrome, removed unicode support.
1 parent 0d25bab commit 18d4265

File tree

6 files changed

+22
-63
lines changed

6 files changed

+22
-63
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![npm-version]][npm] [![npm-downloads]][npm] [![travis-ci]][travis]
44

55
Morse code encoder and decoder with no dependencies supports Latin, Cyrillic, Greek, Hebrew,
6-
Arabic, Persian, Japanese, Korean, Thai, and Unicode (Chinese and the others) characters with audio generation
6+
Arabic, Persian, Japanese, Korean and Thai characters with audio generation
77
functionality using the [Web Audio API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API).
88

99
See Morse Translator in action: [https://morsify.net](https://morsify.net)
@@ -72,14 +72,12 @@ Set the priority option according to the list below.
7272
- 10 => Japanese
7373
- 11 => Korean
7474
- 12 => Thai
75-
- 13 => Unicode (Chinese and the others)
7675

7776
```js
7877
const cyrillic = morsify.encode('Ленинград', { priority: 5 }); // .-.. . -. .. -. --. .-. .- -..
7978
const greek = morsify.decode('... .- --. .- .--. .--', { priority: 6 }); // ΣΑΓΑΠΩ
8079
const hebrew = morsify.decode('.. ––– . –––', { dash: '', dot: '.', priority: 7 }); // יהוה
8180
const japanese = morsify.encode('NEWS', { priority: 10, dash: '', dot: '', separator: ' ' }); // -・ ・ ・-- ・・・
82-
const chinese = morsify.encode('你好', { priority: 13 }); // -..----.--..... -.--..-.-----.-
8381
const characters = morsify.characters({ dash: '', dot: '' }); // {'1': {'A': '•–', ...}, ..., '11': {'ㄱ': '•–••', ...}}
8482
const arabicAudio = morsify.audio('البراق', { // generates the morse .- .-.. -... .-. .- --.- then generates the audio from it
8583
unit: 0.1, // period of one unit, in seconds, 1.2 / c where c is speed of transmission, in words per minute

dist/morsify.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -470,27 +470,6 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
470470
return swapped;
471471
};
472472

473-
var unicodeToMorse = function unicodeToMorse(character) {
474-
var ch = [];
475-
476-
for (var i = 0; i < character.length; i++) {
477-
ch[i] = ('00' + character.charCodeAt(i).toString(16)).slice(-4);
478-
}
479-
480-
return parseInt(ch.join(''), 16).toString(2);
481-
};
482-
483-
var unicodeToHex = function unicodeToHex(morse, options) {
484-
morse = morse.replace(new RegExp('\\' + options.dot, 'g'), '0').replace(new RegExp('\\' + options.dash, 'g'), '1');
485-
morse = parseInt(morse, 2);
486-
487-
if (isNaN(morse)) {
488-
return options.invalid;
489-
}
490-
491-
return decodeURIComponent(JSON.parse('"' + "\\u" + morse.toString(16) + '"'));
492-
};
493-
494473
var getOptions = function getOptions(options) {
495474
options = options || {};
496475
options.oscillator = options.oscillator || {};
@@ -526,7 +505,7 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
526505
}
527506
}
528507

529-
return parseInt(options.priority) === 13 ? unicodeToMorse(character) : options.invalid;
508+
return options.invalid;
530509
}).join(options.separator).replace(/0/g, options.dot).replace(/1/g, options.dash);
531510
};
532511

@@ -538,15 +517,19 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
538517
return swapped[characters];
539518
}
540519

541-
return parseInt(options.priority) === 13 ? unicodeToHex(characters, options) : options.invalid;
520+
return options.invalid;
542521
}).join('');
543522
};
544523

545-
var isBrowser = typeof window !== 'undefined';
546-
var AudioContext = isBrowser ? window.AudioContext || window.webkitAudioContext : null;
547-
var context = isBrowser ? new AudioContext() : null;
524+
var AudioContext = null;
525+
var context = null;
548526

549527
var audio = function audio(text, opts) {
528+
if (AudioContext === null && typeof window !== 'undefined') {
529+
AudioContext = window.AudioContext || window.webkitAudioContext;
530+
context = new AudioContext();
531+
}
532+
550533
var options = getOptions(opts);
551534
var morse = encode(text, opts);
552535
var oscillator = context.createOscillator();

dist/morsify.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "morsify",
3-
"version": "2.2.1",
3+
"version": "2.3.0",
44
"description": "Morse code translator, Morse encoder and decoder which can also generate audio.",
55
"keywords": [
66
"morsify",

src/morsify.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,6 @@
133133
return swapped;
134134
};
135135

136-
const unicodeToMorse = (character) => {
137-
const ch = [];
138-
for (let i = 0; i < character.length; i++) {
139-
ch[i] = ('00' + character.charCodeAt(i).toString(16)).slice(-4);
140-
}
141-
return parseInt(ch.join(''), 16).toString(2);
142-
};
143-
144-
const unicodeToHex = (morse, options) => {
145-
morse = morse.replace(new RegExp('\\' + options.dot, 'g'), '0').replace(new RegExp('\\' + options.dash, 'g'), '1');
146-
morse = parseInt(morse, 2);
147-
if (isNaN(morse)) {
148-
return options.invalid;
149-
}
150-
return decodeURIComponent(JSON.parse('"'+ '\\u' + morse.toString(16) +'"'));
151-
};
152-
153136
const getOptions = (options) => {
154137
options = options || {};
155138
options.oscillator = options.oscillator || {};
@@ -180,7 +163,7 @@
180163
return characters[set][character];
181164
}
182165
}
183-
return parseInt(options.priority) === 13 ? unicodeToMorse(character) : options.invalid;
166+
return options.invalid;
184167
}).join(options.separator).replace(/0/g, options.dot).replace(/1/g, options.dash);
185168
};
186169

@@ -190,15 +173,20 @@
190173
if (typeof swapped[characters] !== 'undefined') {
191174
return swapped[characters];
192175
}
193-
return parseInt(options.priority) === 13 ? unicodeToHex(characters, options) : options.invalid;
176+
return options.invalid;
194177
}).join('');
195178
};
196179

197-
const isBrowser = typeof window !== 'undefined';
198-
const AudioContext = isBrowser ? window.AudioContext || window.webkitAudioContext : null;
199-
const context = isBrowser ? new AudioContext() : null;
180+
let AudioContext = null;
181+
let context = null;
200182

201183
const audio = (text, opts) => {
184+
185+
if (AudioContext === null && typeof window !== 'undefined') {
186+
AudioContext = window.AudioContext || window.webkitAudioContext;
187+
context = new AudioContext();
188+
}
189+
202190
const options = getOptions(opts);
203191
const morse = encode(text, opts);
204192
const oscillator = context.createOscillator();

0 commit comments

Comments
 (0)