Skip to content

Instantly share code, notes, and snippets.

View jimbol's full-sized avatar
🕵️‍♂️
Working on a secret project

Jim Hall jimbol

🕵️‍♂️
Working on a secret project
View GitHub Profile
@jimbol
jimbol / feature-flags.md
Last active March 30, 2017 13:20
A simple feature flag implementation

Feature flags

A simple feature flag implementation

Setup

A hash is provided for the current environment in the application setup.

type Features = { [FLAG_NAME]: Boolean | Function };
const features: Features = (NODE_ENV === 'production') ? productionFeatures : testingFeatures;
export featureFlag = setUpFeatureFlags(features, platform);
@jimbol
jimbol / hash-reducer-helpers.js
Created March 28, 2017 21:34
Helpers for hash reducers
const update = (id, hash, changes) => {
const newHash = hash;
const updateEntity = { ...newHash[id], ...changes };
newHash[id] = updateEntity;
return newHash;
};
const replace = (id, nextEntity, hash) => {
let newHash = hash;
newHash = remove(id, newHash);
@jimbol
jimbol / make-optimistic.js
Created March 27, 2017 21:20
Action-based optimistic reducer enhancer
// ACTION FORMAT
// const scope = 'MY_SCOPE';
// const createAction = (id, payload) => ({
// type: 'START_LOADING',
// payload,
// meta: {
// scope,
// type: EXPECT,
// id,
// },
@jimbol
jimbol / transformer.js
Last active March 27, 2017 22:02
Transform data with dependencies and overrides
const lookUp = ({ hash, ids }) => ids.map((id) => hash[id]);
createTransformer({
hash: getHash,
ids: getIds,
}, lookUp, []);
const createTransformer = (dependencies, transformer, fallbackValue) => {
return function(state, props, overrides) {
const keys = Object.keys(dependencies);
@jimbol
jimbol / partial-reducers.js
Last active February 6, 2017 16:06
Partial reducers
// my-reducer.js
function myReducer(plugins, state, action) {
// ...
}
// reducers.js
import { myReducer } from './my-reducer';
export const reducers = {
myReducers,
@jimbol
jimbol / eq-eq-null.js
Created January 31, 2017 15:10
equals equals null
0 == null; // false
'' == null; // false
undefined == null; // true
@jimbol
jimbol / selector-dep-injection.js
Created January 27, 2017 14:03
Selector dependency injection
// It gets hard to test combined selectors after a while.
// The dependency chains run deep. To get around this we
// can test the callback function seperately from the rest
// of the selector, but that leaves a testing gap.
// In order to test that createSelector gets called with the
// right stuff, we could do something like this
// WARNING UNTESTED CODE
@jimbol
jimbol / README.md
Last active November 16, 2016 15:21
Redux-Saga Effect Runner

Testing with Effect Runner

A Redux-Saga test runner for Mocha

Goals

  • Switching the order of instructions should be easy
  • Associate instructions with their results
  • Changing instruction results should be easy

Guide

@jimbol
jimbol / features.js
Last active November 15, 2016 17:07
Features module concept
// Having some sort of feature flags allows us to
// - write centralized logic around when to show features
// - develop features in smaller pieces behind feature flags
// - turn on/off features at will
// In a view, effect, whereever
import features from '../features'
if (features('showFooBar')) {
// display FooBar
@jimbol
jimbol / spy-on-effects.js
Last active December 21, 2016 14:29
Spying on redux-saga effects
// In test file
const spy = effectSpy(generator)
.next('init')
.next('getToken', token) // name and pass in yield values
.next('getUpdatedFooBars', fooBars)
.next('putFooBars');
// in beforeEach
mySpyRun = spy.run({