A bunch of random, contrived functions with maybe some value. Mostly small proof-of-concepts.
Operates just like const sanitize = ({ id, email, date }) => ({ id, email, date })
but with destructure
you can store your included keys in an array, say, in your config.
const keysToInclude = ['id', 'email', 'date']
const sanitize = destructure(keysToInclude)
return getUsers().map(sanitize)
Has zero error handling, but is fun for getting to the bottom of a deeply nested JSON object (e.g. graphql
or DynamoDB
responses).
// contrived response
const GraphQLRes = {
data: {
users: [
{
id: 'mfix22',
friends: [
{
id: 'briandennis'
}
]
},
{
id: 'briandennis',
friends: [
{
id: 'jakedex'
}
]
}
]
}
}
const myFirstFriend = pluckDeep('data.users.0.friends.0.id')
console.log(myFirstFriend(GraphQLRes));
// -> briandennis
One-line left-to-rightcompose
const cap = str => str.toUpperCase()
const swear = str => str.replace(/(a|e|i|o|u)/ig, '*')
const exclaim = str => `${str}!`
const BLOW_UP = compose(cap, swear, exclaim)
console.log(BLOW_UP('Today is good day'))
// -> T*D*Y *S G**D D*Y!
const everyOtherLetter = countMap(x => String.fromCharCode(x + 65), 26 / 2, 2)
console.log(everyOtherLetter);
// -> [ 'A', 'C', 'E', 'G', 'I', 'K', 'M', 'O', 'Q', 'S', 'U', 'W', 'Y' ]
Wow thank you, very helpful 👍 🇹🇭