Skip to content

Instantly share code, notes, and snippets.

@victorperin
Forked from anonymous/Batata
Last active August 20, 2022 03:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorperin/c7a57e040d474476e98c7be2fa796aa1 to your computer and use it in GitHub Desktop.
Save victorperin/c7a57e040d474476e98c7be2fa796aa1 to your computer and use it in GitHub Desktop.
const stringToExecute = 'sos';
const button = {
keyUp: () => console.log('up'),
keyDown: () => console.log('down'),
}
//////////////////////////////////////////
const app = {
morsers: {
me: {
keyDown: () => console.log('down'),
keyUp: () => console.log('up'),
}
}
}
const morseAlphabet = {
'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': '--..',
'0': '-----',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
'.': '.-.-.-',
',': '--..--',
':': '---...',
'?': '..--..',
'\'': '.----.',
'-': '-....-',
'/': '-..-.',
'"': '.-..-.',
'@': '.--.-.',
'=': '-...-',
'!': '---.',
' ': ' ',
}
const beepOptions = {
'.': () => makeSound(50),
'-': () => makeSound(150),
' ': () => makeSilence(2000),
}
const makeSilence = (delay) =>
new Promise( (resolve) =>
setTimeout(resolve, delay)
)
const makeSound = (delay) =>
new Promise( (resolve) => {
button.keyDown()
setTimeout(() => {
button.keyUp()
resolve()
}, delay)
})
const beepString = (string) =>
string.split('')
.reduce(
(promise, character) =>
promise
.then(beepOptions[character]),
Promise.resolve()
)
const stringToBeepString = (string) =>
string
.toLowerCase()
.split('')
.map( (character) => morseAlphabet[character] )
.join('')
.trim()
Promise.resolve(stringToExecute)
.then(stringToBeepString)
.then(beepString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment