Skip to content

Commit

Permalink
Added support for unicode characters for Chinese and the others, v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdemirburak committed Oct 6, 2017
1 parent bfbbeb1 commit ff7c7c9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![npm-version]][npm] [![npm-downloads]][npm] [![travis-ci]][travis]

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

Live demo can be found at [morsify.net](https://morsify.net).

Expand Down Expand Up @@ -70,11 +70,13 @@ Set the priority option according to the list below.
- 10 => Japanese
- 11 => Korean
- 12 => Thai
- 13 => Unicode (Chinese and the others)

```js
var cyrillic = morsify.encode('Ленинград', { priority: 5 }) // .-.././-./../-./--./.-./.-/-..
var greek = morsify.decode('.../.-/--./.-/.--./.--', { priority: 6 }) // Σ Α Γ Α Π Ω
var hebrew = morsify.decode('––– –... ––– –. ––. .. .–.. –––', { dash: '', dot: '.', space: ' ', priority: 7 }) // ה ב ה נ ג י ל ה
var cyrillic = morsify.encode('Ленинград', { priority: 5 }); // .-.././-./../-./--./.-./.-/-..
var greek = morsify.decode('.../.-/--./.-/.--./.--', { priority: 6 }); // Σ Α Γ Α Π Ω
var hebrew = morsify.decode('––– –... ––– –. ––. .. .–.. –––', { dash: '', dot: '.', space: ' ', priority: 7 }); // ה ב ה נ ג י ל ה
var chinese = morsify.encode('你好', { priority: 13 }); // -..----.--...../-.--..-.-----.-
var characters = morsify.characters({ dash: '', dot: '' }); // {'1': {'A': '•–', ...}, ..., '11': {'ㄱ': '•–••', ...}}
var arabicAudio = morsify.audio('البُراق‎‎', { // generates the morse .-/.-../-.../.-./.-/--.- then generates the audio from it
unit: 0.1, // period of one unit, in seconds, 1.2 / c where c is speed of transmission, in words per minute
Expand All @@ -96,9 +98,6 @@ arabicAudio.stop(); // will stop playing morse audio

Contributions are welcome.

Currently, as a major drawback, Chinese characters are missing. Someone with the knowledge of
[Chinese telegraph code](https://en.wikipedia.org/wiki/Chinese_telegraph_code) can help to implement it.

## Generating Minified Files

Install node and npm following one of the techniques explained within
Expand Down
2 changes: 1 addition & 1 deletion dist/morsify.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "morsify",
"version": "0.4.0",
"version": "0.5.0",
"description": "Morse code translator and decoder which also generates audio.",
"keywords": [
"morse",
Expand All @@ -11,6 +11,10 @@
"morse decoder",
"morse encoder",
"morse translator",
"morse code translator",
"samuel morse",
"codigo morse",
"морзе",
"morsify"
],
"license": "MIT",
Expand Down
27 changes: 23 additions & 4 deletions src/morsify.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
'ไ': '01001', 'โ': '111', 'ำ': '00010', '่': '001', '้': '0001',
'๊': '11000', '๋':'01010', 'ั': '01101', '็': '11100', '์': '11001',
'ๆ': '10111', 'ฯ': '11010',
// duplicates
// 'ฃ': '1010', => duplicated with ข | 'ฅ': '101', 'ฆ': '101', => duplicated with ค | 'ฎ': '100', => duplicated with ด
// 'ฏ': '1', => duplicated with ต | 'ฐ': '10100', => duplicated with ถ | 'ธ': '10011', 'ฑ': '10011', 'ฒ': '10011', => duplicated with ท
// 'ณ': '10', => duplicated with ณ | 'ภ': '01100', => duplicated with พ | 'ฬ': '0100', => duplicated with ล
Expand Down Expand Up @@ -134,6 +133,23 @@
return swapped;
};

var unicodeToMorse = function (character) {
var ch = [];
for (var i = 0; i < character.length; i++) {
ch[i] = ('00' + character.charCodeAt(i).toString(16)).slice(-4);
}
return parseInt(ch.join(''), 16).toString(2);
};

var unicodeToHex = function (morse, options) {
morse = morse.replace(new RegExp('\\' + options.dot, 'g'), '0').replace(new RegExp('\\' + options.dash, 'g'), '1');
morse = parseInt(morse, 2);
if (isNaN(morse)) {
return options.invalid;
}
return decodeURIComponent(JSON.parse('"'+ '\\u' + morse.toString(16) +'"'));
};

var getOptions = function (options) {
options = options || {};
options.oscillator = options.oscillator || {};
Expand All @@ -158,18 +174,21 @@
var options = getOptions(opts);
return text.replace(/\s+/g, '').toLocaleUpperCase().split('').map(function(character) {
for (var set in characters) {
if (typeof characters[set][character] !== 'undefined') {
if (typeof characters[set] !== 'undefined' && typeof characters[set][character] !== 'undefined') {
return characters[set][character];
}
}
return options.invalid;
return parseInt(options.priority) === 13 ? unicodeToMorse(character) : options.invalid;
}).join(options.space).replace(/0/g, options.dot).replace(/1/g, options.dash);
};

var decode = function (morse, opts) {
var options = getOptions(opts), swapped = swapCharacters(options);
return morse.split(options.space).map(function(characters) {
return swapped[characters] || options.invalid;
if (typeof swapped[characters] !== 'undefined') {
return swapped[characters];
}
return parseInt(options.priority) === 13 ? unicodeToHex(characters, options) : options.invalid;
}).join(' ').replace(/\s+/g, ' ');
};

Expand Down
Loading

0 comments on commit ff7c7c9

Please sign in to comment.