Skip to content

Instantly share code, notes, and snippets.

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
}
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' }
// 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
~ ❯❯❯ 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.
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) {
@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",
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))
// 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
export default function reducer(state=initialState, action) {
switch(action.type) {
case defs.MAKE_PAYMENT_SUCCESS:
return state.withMutations(s =>
s.set('modalOpen', false)
.set('loading', false)
.setIn(['loans', action.loan.id], Immutable.fromJS(action.loan))
)
case defs.MAKE_PAYMENT_FAILED:
return state.withMutations(s =>
// actions/sync.js
export const makePaymentSent = () => ({
type: defs.MAKE_PAYMENT_SENT,
})
export const makePaymentFailed = (error) => ({
type: defs.MAKE_PAYMENT_FAILED,
error,
})
export const makePaymentSuccess = (loan) => ({
type: defs.MAKE_PAYMENT_SUCCESS,