Skip to content

Instantly share code, notes, and snippets.

🤓
Building a new standard JS library: conductor

WaldoJeffers

🤓
Building a new standard JS library: conductor
Block or report user

Report or block WaldoJeffers

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View withParamTransform.js
// 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 Aug 24, 2018
Dynamic initialRouteName for React Navigation
View withDynamicInitialRouteName.js
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 Jun 3, 2018
Get homeworld stats about Star Wars characters
View homeworld_stats_with_conductor.js
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
Filter an object using an asynchronous predicate with conductor
View filter_object_async_with_conductor.js
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 }
View filter_object_async_without_conductor.js
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
Filter using an asynchronous predicate with conductor
View filter_async_with_conductor.js
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
Filter an array using an asynchronous predicate
View filter_async_without_conductor.js
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
Reduce an object without conductor
View reduce_object_without_conductor.js
const obj = { books: 10, ruler: 1, pencils: 5 }
const add = (a, b) => a + b
const total = Object.values(obj).reduce(add, 0) // 16
View conductor_example_filter_object_async_init.js
const obj = { books: 10, ruler: 1, pencils: 5 }
const isEven = x => Promise.resolve(x % 2 === 0)
View conductor_example_filter_async_2.js
const result = await Promise
.all(numbers.map(isEven)))
.then(results => numbers.filter((_, index) => results[index]))
// [4]
You can’t perform that action at this time.