Created
March 8, 2011 14:24
-
-
Save cybear/860316 to your computer and use it in GitHub Desktop.
just a basic morse codec
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
var Morse={ | |
toM:{ 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":"_____","/":"_.._.","+":"._._.","=":"_..._",".":"._._._",",":"__..__","?":"..__..","(":"_.__.",")":"_.__._","_":"_...._","\"":"._.._.","_":"..__._","'":".____.",":":"___...",";":"_._._.","$":"..._.._"," ":" "}, | |
fromM:null, | |
getfromM:function(){ | |
function inverseKeyValueObj(o){var r={},k;for(k in o)r[o[k]]=k;return r} | |
if(!this.fromM){ | |
this.fromM=inverseKeyValueObj(this.toM); | |
this.fromM.space=' '; | |
} | |
return this.fromM; | |
}, | |
toMorseString:function(s){ | |
var m=this.toM; | |
return s.toUpperCase().split('').map(function(c){return m[c]}).join(' '); | |
}, | |
morseToString:function(s){ | |
var arr=s.replace(/ /g,' space ').split(' '); | |
var fm=this.getfromM(); | |
return arr.map(function(c){return fm[c]}).join(''); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment