Skip to content

Instantly share code, notes, and snippets.

// mutators.js
export const closeModal = s => s.set('modalOpen', false)
export const stopLoading = s => s.set('loading', false)
export const updateLoan = loan => s => s.setIn(['loans', loan.id], Immutable.fromJS(loan))
export const setPaymentError = error => s => s.set('paymentError', error)
// utils.js
const applyFn = (state, fn) => fn(state)
export const pipe = (fns, state) =>
state.withMutations(s => fns.reduce(applyFn, s))
// reducer.js
describe('App Reducer', () => {
it('MAKE_PAYMENT_SUCCESS', () => {
const state = Immutable.Map()
const loan = Symbol()
const mutations = [
expect.spyOn('mutate', 'updateLoan').andCall(loan => s => s)
expect.spyOn('mutate', 'closeModal').andCall(s => s)
expect.spyOn('mutate', 'stopLoading').andCall(s => s)
]
const result = reducer(state, actions.makePaymentSuccess(loan))
@ccorcos
ccorcos / memoize.py
Created April 16, 2014 01:53
memoize class for memorizing / caching function calls in python along with reset and reset all memoized functions
import collections
import functools
memoized = []
def clearAllMemoized():
[m.reset() for m in memoized]
@ccorcos
ccorcos / react test
Last active November 28, 2016 22:51
~/code ❯❯❯ mkdir react-test
~/code ❯❯❯ cd react-test
~/c/react-test ❯❯❯ npm init -y
Wrote to /Users/chetcorcos/code/react-test/package.json:
{
"name": "react-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
class WithGps extends PureComponent {
constructor(props) {
super(props)
// see if maybe you have it cached
const gps = GeoService.get()
// this will be effective on the first render
this.state = { gps }
// wait for the gps location
if (!gps) {
~ ❯❯❯ cd /tmp
/tmp ❯❯❯ git clone git@github.com:ccorcos/doug.git
Cloning into 'doug'...
remote: Counting objects: 631, done.
remote: Compressing objects: 100% (459/459), done.
remote: Total 631 (delta 237), reused 0 (delta 0), pack-reused 164
Receiving objects: 100% (631/631), 207.74 KiB | 0 bytes/s, done.
Resolving deltas: 100% (290/290), done.
Checking connectivity... done.
// A Stream is an abstraction over strings and arrays so that we don't have to
// keep chopping them up everywhere eating up CPU. An iterable is either a
// string or an array. The cursor is an index that marks the beginning of the
// stream and the length is the amount left in the Stream.
export class Stream {
constructor(iterable, cursor, length) {
this.iterable = iterable
this.cursor = cursor || 0
this.length = length === undefined
? iterable.length - this.cursor
export function makePayment(loanId, amount, paymentMethodId) {
return dispatch => {
dispatch(makePaymentSent())
fetch(`/api/loans/${loanId}/payments`, {
headers: new Headers({
'Content-Type': 'application/json',
}),
credentials: 'same-origin',
method: 'POST',
body: JSON.stringify({
const char = c => input => {
if (input[0] === c) {
return {success: true, rest: input.slice(1)}
}
return {success: false, rest: input}
}
char('a')('abc')
// => { success: true, rest: 'bc' }
const sequence = parsers => input => {
let next = input
for (var i = 0; i < parsers.length; i++) {
const parser = parsers[i]
const {success, rest} = parser(next)
if (!success) {
return {success, rest}
}
next = rest
}