Last active
December 18, 2015 10:39
-
-
Save lxe/5770450 to your computer and use it in GitHub Desktop.
http://morseroulette.com/ text-to-morse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function morse(s, speed) { | |
s = s.toLowerCase().replace(/[^\w ]/g, ''); | |
var codes = { | |
'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' : '----.' | |
} | |
var speed = speed || 200 | |
, toneDelay = dot = speed | |
, letterDelay = dash = speed * 3 | |
, $handle = $('.handle'); | |
function tone(len) { | |
return function(next) { | |
$handle.mousedown(); | |
setTimeout(function() { | |
$handle.mouseup(); | |
setTimeout(next, toneDelay) | |
}, len); | |
} | |
} | |
function tones(c) { | |
return function(next) { | |
console.log(c); | |
var fn = function() { | |
setTimeout(next, letterDelay); | |
}; | |
if (c === ' ') return fn(); | |
var code = codes[c]; | |
code.split('').reverse().forEach(function(l) { | |
var t = tone(l === '-' ? dash : dot); | |
fn = t.bind({ }, fn); | |
}); | |
return fn(); | |
} | |
} | |
var fn = function() { | |
console.log('done!'); | |
}; | |
s.split('').reverse().forEach(function(c) { | |
var t = tones(c); | |
fn = t.bind({ }, fn); | |
}); | |
return fn(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment