Created
April 21, 2018 15:29
-
-
Save carlosrberto/3ca71455f8f79f7757cf2088cec45c21 to your computer and use it in GitHub Desktop.
Functional Stuffs 1
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
const input = document.getElementById('input'); | |
const outPut = document.getElementById('output'); | |
const leftKeys = [ | |
'q', 'w', 'e', 'r', 't', | |
'a', 's', 'd', 'f', 'g', | |
'z', 'x', 'c', 'v', | |
]; | |
const rightKeys = [ | |
'y', 'u', 'i', 'o', 'p', | |
'h', 'j', 'k', 'l', 'ç', | |
'b', 'n', 'm', | |
]; | |
const pipe = function(first, ...others) { | |
return function() { | |
return others.reduce((acc, fn) => { | |
return fn.call(null, acc); | |
}, first.apply(null, arguments)); | |
} | |
} | |
const curry = function(fn) { | |
const partial = function(prevArgs = []) { | |
return function() { | |
const nextArgs = [...prevArgs, ...arguments]; | |
if((prevArgs.length + arguments.length) === fn.length) { | |
return fn.apply(null, nextArgs) | |
} else { | |
return partial(nextArgs); | |
} | |
} | |
} | |
return partial(); | |
}; | |
const removeSpaces = w => w.replace(/\s/g, ''); | |
const toLower = w => w.toLowerCase(); | |
const toArray = w => w.split(''); | |
const numWords = curry((l, w) => w.filter(k => l.includes(k)).length); | |
const getKeysDistribution = (word) => ({ | |
left: pipe(removeSpaces, toLower, toArray, numWords(leftKeys))(word), | |
right: pipe(removeSpaces, toLower, toArray, numWords(rightKeys))(word), | |
}); | |
input.addEventListener('input', (event) => { | |
outPut.value = JSON.stringify( | |
getKeysDistribution(input.value) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment