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
// SUBJECT
// Write a function withParamTransform which,
// for any function f,
// for any parameter transformation such as (a, b, c) => (a, c, b) (inverting 2 parameters)
// satisfies the following condition:
withParamTransform(f)[(a, b, c) => (a, c, b)](1, 2, 3) === f(1, 3, 2)
// A more realistic example
const minus = (a, b) => a - b
minus(5, 2) === 3 // true
@WaldoJeffers
WaldoJeffers / withDynamicInitialRouteName.js
Created August 24, 2018 08:42
Dynamic initialRouteName for React Navigation
import { renderNothing, lifecycle } from 'recompose';
/**
* HOC to navigate to a dynamically computed route name
* React Navigation does currently not accept a function to determine
* the initial route at runtime
*/
const withDynamicInitialRouteName = Navigator => (screens, config = {}) => {
if (typeof config.initialRouteName === 'function') {
const getInitialRouteName = config.initialRouteName;
@WaldoJeffers
WaldoJeffers / homeworld_stats_with_conductor.js
Last active June 3, 2018 10:39
Get homeworld stats about Star Wars characters
import { compose, get, mergeWith, reduce } from 'conductor'
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}`)
const getHomeworldName = compose(get('name'), fetchJSON, get('homeworld'), fetchCharacter)
const toObj = (key, value) => ({ [key]: value })
const countOne = name => toObj(name, 1)
const countHomeworld = compose(countOne, getHomeworldName)
const mergeByAdding = mergeWith(add)
@WaldoJeffers
WaldoJeffers / filter_object_async_with_conductor.js
Last active May 30, 2018 20:28
Filter an object using an asynchronous predicate with conductor
import { filter } from 'conductor'
const obj = { books: 10, ruler: 1, pencils: 5 }
const isEven = x => Promise.resolve(x % 2 === 0)
const result = await filter(isEven, obj) // { books: 10 }
const obj = { books: 10, ruler: 1, pencils: 5 }
const isEven = x => Promise.resolve(x % 2 === 0)
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 }
@WaldoJeffers
WaldoJeffers / filter_async_with_conductor.js
Created May 26, 2018 16:26
Filter using an asynchronous predicate with conductor
import { filter } from 'conductor'
const numbers = [3, 1, 4]
const isEven = x => Promise.resolve(x % 2 === 0)
const result = await filter(isEven, numbers) // [4]
@WaldoJeffers
WaldoJeffers / filter_async_without_conductor.js
Created May 26, 2018 16:20
Filter an array using an asynchronous predicate
const numbers = [3, 1, 4]
const isEven = x => Promise.resolve(x => x % 2 === 0)
const result = await Promise
.all(numbers.map(isEven)))
.then(results => numbers.filter((_, index) => results[index]))
// [4]
@WaldoJeffers
WaldoJeffers / reduce_object_without_conductor.js
Last active May 26, 2018 18:19
Reduce an object without conductor
const obj = { books: 10, ruler: 1, pencils: 5 }
const add = (a, b) => a + b
const total = Object.values(obj).reduce(add, 0) // 16
const obj = { books: 10, ruler: 1, pencils: 5 }
const isEven = x => Promise.resolve(x % 2 === 0)
const result = await Promise
.all(numbers.map(isEven)))
.then(results => numbers.filter((_, index) => results[index]))
// [4]