Skip to content

Instantly share code, notes, and snippets.

View granmoe's full-sized avatar
🔔
Maximizing the frame rate, resolution, and screen size of my mind 😑

Matt Granmoe granmoe

🔔
Maximizing the frame rate, resolution, and screen size of my mind 😑
View GitHub Profile
// must run in a context where Immutable is available, like in the console here: https://facebook.github.io/immutable-js/docs/#/
const flattenList = items => {
return items.reduce((result, item) => {
result = result.push(item)
return item.has('children') ? result.concat(flattenList(item.get('children'))) : result
}, Immutable.List())
}
const nestedList = [
@granmoe
granmoe / sagas-spec.js
Last active August 6, 2016 21:50
Example of an async saga and test of non-trivial complexity.
import test from 'tape';
import { put, call } from 'redux-saga/effects'
import { changeLOS } from './sagas'
import api from 'nowhere'
test('changeLOS Saga', assert => {
const gen = changeLOS()
assert.deepEqual(
@granmoe
granmoe / form-validation-and-utils-with-cached-functions.jsx
Last active May 9, 2017 02:04
Simple form validation through a Higher Order Component. Same as the other ones I made, except this caches functions so shouldComponentUpdate of the child doesn't always return true.
import React from 'react'
export default options => {
return WrappedComponent => class FormValidation extends React.Component {
constructor () {
super()
this.validate = options.validate
this.cachedFunctions = { onChange: {}, onBlur: {} }
this.state = options.fields.reduce((result, field) => {
@granmoe
granmoe / form-validation-and-utils.jsx
Last active September 23, 2020 03:59
Simple react form validation plus a few tiny utilities, via a higher order component
import React from 'react'
export default options => {
return WrappedComponent => class FormValidation extends React.Component {
constructor () {
super()
this.validate = options.validate
this.state = options.fields.reduce((result, field) => {
result.fields.push(field)
@granmoe
granmoe / form-validation.jsx
Last active April 3, 2016 00:59
Simple form validation implemented with a higher-order component
import React from 'react'
export default (options, WrappedComponent) => class extends React.Component {
constructor () {
super()
this.validate = options.validate
this.state = options.fields.reduce((result, field) => {
result.fields.push(field)
result.errors[field] = ''