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 / classnames.js
Last active May 9, 2017 01:43
I saw this super simple, useful idea floating around in some projects and decided to implement the API myself instead of installing yet another "micro dependency." Plus, I can be more opinionated (assuming BEM, for example) when I use my own code.
export default function () {
let className = ''
let options = {}
const classNames = []
if (arguments.length > 1) {
className = arguments[0]
options = arguments[1]
classNames.push(className)
} else {
@granmoe
granmoe / get-time-of-day.js
Created May 9, 2017 01:36
Returns a message describing the time of day given a start and end hour. A small and humble chunk of code, but I thought my approach was kind of nifty.
function getTimeOfDay (start, end) {
if (start < 12 && end > 16) { return null } // not specific enough
// we don't care about the window from 22:00 - 06:00
const timesOfDay = Array(24)
.fill('Morning', 6, 12)
.fill('Afternoon', 12, 16)
.fill('Evening', 16, 22)
return Array.from(new Set(timesOfDay.slice(start, end)))
@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.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] = ''