Skip to content

Instantly share code, notes, and snippets.

View WaldoJeffers's full-sized avatar
🤓
Building a new standard JS library: conductor

WaldoJeffers

🤓
Building a new standard JS library: conductor
View GitHub Profile
const toObj = (key, value) => ({ [key]: value }) // toObj('hello', 'world) === { hello: 'world' }
const countOne = name => toObj(name, 1) // countOne('Tatooine') === { Tatooine: 1 }
import { compose, get } from 'conductor'
const getHomeworldName = compose(get('name'), fetchJSON, get('homeworld'), fetchCharacter)
// getHomeworldName(1) == { name: 'Tatooine', ... }
const character_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const fetchJSON = url => fetch(url).then(res => res.json())
const fetchCharacter = id => fetchJSON(`https://swapi.co/api/people/${id}`)
// fetchCharacter(1) == { name: 'Luke Skywalker', homeworld: 'https://swapi.co/api/planets/1/' }
{
Alderaan: {
brown: 1
},
Naboo: {
red: 1
},
Stewjon: {
'blue-gray': 1,
Tatooine: {
const homeworld_stats = { Alderaan: 1, Naboo: 1, Stewjon: 1, Tatooine: 7 }
@WaldoJeffers
WaldoJeffers / reduce_object_with_conductor.js
Last active May 26, 2018 18:16
Reduce an object with conductor
import { reduce } from 'conductor'
const obj = { books: 10, ruler: 1, pencils: 5 }
const add = (a, b) => a + b
const total = reduce(add, 0, obj) // 16
const add = (a, b) => a + b
const total = Object.values(obj).reduce(add, 0) // 16
const obj = { books: 10, ruler: 1, pencils: 5 }
const result = await Promise
.all(Object.values(obj).map(isEven))
.then(results => Object.keys(obj).reduce((acc, key, index) => {
if (results[index]){
acc[key] = obj[key]
}
return acc
}, {}) // { books: 10 }