Skip to content

Instantly share code, notes, and snippets.

@goldhand
goldhand / Monad.js
Last active February 5, 2018 22:13
A Monad class in Flow
export default class Monad<V> {
value: V;
static of = <T>(value: T): Monad<T> => new Monad(value);
constructor(value: V) {
this.value = value;
}
@goldhand
goldhand / compose.js
Created December 13, 2017 19:22
ATA JS (Day 2) Functions - Daily Assignment Solutions reviewed in class
const reduce = require("./reduce");
/**
* Performs right-to-left function composition. The rightmost function may have
* any arity; the remaining functions must be unary.
* compose(f, g)(x) >> f(g(x))
*
* @example
* const doubleNegative = compose(x => x * -1, x => x * 2);
* doubleNegative(5); // -10
@goldhand
goldhand / snippets.cson
Created August 24, 2017 22:21
Atom snippets for react components
'.source.js.jsx':
'ReactJS Component Unit Test Template':
'prefix': 'compspec'
'body': """
/**
* components/$1.spec.js
*/
import React from 'react';
import {shallow} from 'enzyme';
import $1 from './$1';
@goldhand
goldhand / SafeRender.jsx
Created August 19, 2017 21:45
Don't ruin everything when a component fails to render
/**
* components/SafeRender.js
*/
import React, {Component, PropTypes} from 'react';
import {getDisplayName} from '../utils';
import {Alert} from '../generic';
import Translate from './Translate';
import logger from '../logger';
const TRANSLATE_RENDER_FALLBACK = 'There was a an Error.';
@goldhand
goldhand / Combo.jsx
Created August 19, 2017 21:44
Combines two components into one
/**
* components/Combo.js
*/
import React, {Component, PropTypes} from 'react';
/**
* Gets the displayname of a Component
* @param {ReactClass} WrappedComponent - component to get display name from
* @returns {string} Display name of WrappedComponent
*/
@goldhand
goldhand / functional-utils.js
Created August 19, 2017 21:36
Functional utility functions
/**
* General purpose functional utilties
* @module utils/fn
*/
import hasProperty from './hasProperty';
export const map = (fn, arr) => {
if (typeof arr !== 'undefined') return Array.prototype.map.call(arr, fn);
return map.bind({}, fn);
};
@goldhand
goldhand / BiMap.js
Created July 10, 2017 22:19
A map object that swings both ways ;)
/**
* @module {Function} utils/bimap
* @flow
*/
type MapKeyType = typeof BiMap.FORWARD | typeof BiMap.REVERSE;
// always sets Array<value>
// merges instead of overwrites by default
export default class BiMap<K, V> {
stores: {
import hoistStatics from 'hoist-non-react-statics';
import React from 'react';
/**
* Allows two animation frames to complete to allow other components to update
* and re-render before mounting and rendering an expensive `WrappedComponent`.
*/
export default function deferComponentRender(WrappedComponent) {
class DeferredRenderWrapper extends React.Component {
constructor(props, context) {
@goldhand
goldhand / renderUpload.js
Created June 27, 2017 15:47
Render upload method on react class
class Upload extends Component {
/**
* Renders one of [renderProgress, renderEnd, renderUnknown] depending on
* the XMLHttpRequest.status
* @returns {Object} Video placeholder with progress bar
*/
@goldhand
goldhand / addasset.js
Created March 30, 2017 01:13
Webpack - add asset to compilation
apply(compiler) {
compiler.plugin('after-emit', (compilation, callback) => {
const file = 'some data';
compilation.assets['foo.js'] = {
source: () => file,
size: () => Buffer.byteLength(file),
};
});
}