Skip to content

Instantly share code, notes, and snippets.

@bpetersen
bpetersen / gist:15031b503d6bbdc39132
Last active April 26, 2017 15:55
C# Easy compare with arbitrary strings
public class FormComparer : IComparer<Box>
{
public int Compare(Box x, Box y)
{
int xCompare = GetIntForSize(x.Size);
int yCompare = GetIntForSize(y.Size);
return xCompare.CompareTo(yCompare);
}
private static int GetIntForSize(string size)
@bpetersen
bpetersen / gist:20abc3557a4218d439583d00afac2fd5
Created June 20, 2016 22:49
Futurizing a promise api and composing futures.
const R = require('ramda')
const { Future } = require('ramda-fantasy')
const axios = require('axios')
const githubUrl = 'https://api.github.com/users/bpetersen'
const log = console.log.bind(console);
const error = console.error.bind(console);
const get = url => {
@bpetersen
bpetersen / Create comma separated list (no oxford comma)
Last active December 22, 2016 01:03
Use Ramda to create comma separated list (no oxford comma)
//const data = ['one'] // ['one']
//const data = ['one', 'two'] // ['one', ' and ', 'two']
//const data = ['one', 'two', 'three'] // ['one', ', ', 'two', ' and ', 'three']
const result = R.compose(
R.flatten,
R.intersperse(' and '),
R.reject(x => R.isEmpty(x) || R.isNil(x)),
R.map(R.intersperse(', '))
)([R.init(data), [R.last(data)]])
@bpetersen
bpetersen / connectedEither.js
Created March 2, 2017 20:35
Component that can render either a Left or Right depending on the redux state.
import React from 'react'
import { connect } from 'react-redux'
import { Redirect } from 'react-router'
const connectedEither = mapStateToProps => predicate => (Left, Right) => {
const component = (props) => {
if(!predicate(props))
{
return <Left {...props} />
const requireDir = require('require-dir')
const tasks = requireDir('./tasks', {recurse: true})
let sequentialTaskChain = Promise.resolve(console.log('starting nightly tasks'))
Object.keys(tasks).map(key => {
sequentialTaskChain = sequentialTaskChain.then(() => {
return Promise.resolve(() => console.log('starting task', key))
.then(tasks[key])
.then(() => console.log('finished task', key))
@bpetersen
bpetersen / test-utils.js
Created March 14, 2019 22:33
Wait for some condition with max retries and exponential backoff
const withMaxAttemptsAndBackoff = ({
waitTime = 100,
maxAttempts = 3,
}) => promise => (...args) => {
if (maxAttempts < 2) return promise(...args)
return promise(...args).catch(_ =>
wait(waitTime).then(() =>
withMaxAttemptsAndBackoff({
waitTime: Math.min(waitTime * 2, 5000),