Skip to content

Instantly share code, notes, and snippets.

@IrakliJani
Last active February 16, 2017 06:40
Show Gist options
  • Save IrakliJani/7f69d0073c00074c28334587e07867e3 to your computer and use it in GitHub Desktop.
Save IrakliJani/7f69d0073c00074c28334587e07867e3 to your computer and use it in GitHub Desktop.
// 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