Last active
February 16, 2017 06:40
-
-
Save IrakliJani/7f69d0073c00074c28334587e07867e3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// library imports 😜 wink, wink | |
const identity = x => x | |
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))) | |
function process (words, configs) { | |
const lengthFilter = length => word => | |
Array.isArray(length) | |
? word.length >= length[0] && word.length <= length[1] | |
: word.length === length | |
const strip = what => word => { | |
const regexes = { | |
vowels: /[aeiou]/gi, | |
consonants: /[^aeiou]/gi | |
} | |
return word.replace(regexes[what], '') | |
} | |
const upperCase = what => word => { | |
const indexes = { | |
first: 0, | |
last: word.length - 1 | |
} | |
return word | |
.split('') | |
.map((char, index) => indexes[what] === index | |
? char.toUpperCase() | |
: char | |
) | |
.join('') | |
} | |
return configs.map(config => | |
words | |
.filter(lengthFilter(config.length)) | |
.map(compose( | |
config.upperCase ? upperCase(config.upperCase) : identity, | |
config.strip ? strip(config.strip) : identity | |
)) | |
) | |
} | |
var words = [ | |
'spacejump', | |
'apples', | |
'graphics', | |
'javascript', | |
'peaches' | |
] | |
var configs = [ | |
{ | |
upperCase: 'first', | |
length: [6, 8] | |
}, | |
{ | |
upperCase: 'last', | |
length: [7, 9], | |
strip: 'vowels' | |
}, | |
{ | |
length: 10, | |
strip: 'consonants' | |
} | |
] | |
console.log(process(words, configs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment