Skip to content

Instantly share code, notes, and snippets.

View hartzis's full-sized avatar

(Brian) Emil Hartz hartzis

View GitHub Profile
@hartzis
hartzis / gist:54e1ff87a08fbb463b0483f02850e9df
Last active September 10, 2020 19:15
Set and Map lookup example
const interestSurcharge = new Set(['AL']);
const flagshipOriginationFee = new Set(['AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI',
'ID', 'IA', 'KS', 'KY', 'LA', 'ME', 'MS', 'MO', 'NM', 'ND', 'OH', 'OR',
'RI', 'SC', 'SD', 'UT', 'VA', 'WV', 'WI', 'WY']);
const documentPreparation = new Set(['IL']);
const nonrefundablePrepaidFinanceCharge = new Set(['IN']);
const notApplicable = new Set(['MD', 'MA', 'MN', 'MT', 'NV', 'NH', 'NJ', 'NY', 'NC', 'PA', 'VT']);
const feeNameMap = new Map([
@hartzis
hartzis / batchThrottleAsyncRequests.js
Last active March 3, 2022 00:42
batch and throttle async requests node/js
const MAX_BATCH_SIZE = 10;
const WAIT_TIME_MS = 1000;
const makeRequest = async (id) => {
try {
await fetch(`/api/${id}`)
} catch(error) { }
}
const batchesOfIds = _.chunk(Ids, MAX_BATCH_SIZE);
@hartzis
hartzis / Experiments.jsx
Last active January 31, 2019 21:23
A/B Testing react, experiment(s)
/*
* Component for displaying an experiment and bucket(s)
*/
/*******************************
* Option 1a - prop renders based on bucket names, ~ mimics <Flag />
*/
<Experiment
name="exp:test-message"
bucketA={({ recordInteraction }) => (<Message color="blue" onClick={() => recordInteraction('click')}>Welcome!</Message>)}
@hartzis
hartzis / restify-proxy.js
Last active August 3, 2023 19:18
Restify proxy pattern using http-proxy. Uses restify server.pre() to setup a pattern to handle proxy requests before all middleware.
/*
* "restify": "^7.2.0",
* "http-proxy": "^1.17.0",
*
* The restify server will proxy requests that match keys in a `proxies` config.
*
*/
const restify = require('restify');
const httpProxy = require('http-proxy');
@hartzis
hartzis / FeatureFlags.jsx
Last active October 25, 2018 20:48
featureFlag component examples
/*
* # 1
* HOC for retrieving and setting a featureFlag prop
*/
const withFlag = (name) => compose(
connect((state) => {
const value = featureFlagSelector(state, { featureFlag });
return { featureFlag, featureFlagValue };
@hartzis
hartzis / EventHelpers.js
Created January 18, 2017 04:25
Touch Event React Jest Enzyme Helpers
function createClientXY(x, y) {
return { clientX: x, clientY: y };
}
export function createStartTouchEventObject({ x = 0, y = 0 }) {
return { touches: [createClientXY(x, y)] };
}
export function createMoveTouchEventObject({ x = 0, y = 0}) {
return { changedTouches: [createClientXY(x, y)] };
@hartzis
hartzis / EventComponent.spec.js
Last active November 5, 2018 19:22
Touch Event Testing React Jest Enzyme
import React from 'react';
import EventComponent from './EventComponent';
import { mount } from 'enzyme';
import {
createStartTouchEventObject,
createMoveTouchEventObject
} from './EventHelpers.js';
describe('EventComponent', () => {
@hartzis
hartzis / EventComponent.js
Last active November 29, 2023 16:38
Touch Event Handling React Component
import React from 'react';
export default class EventComponent extends React.Component {
constructor(props) {
super(props);
this._onTouchStart = this._onTouchStart.bind(this);
this._onTouchMove = this._onTouchMove.bind(this);
this._onTouchEnd = this._onTouchEnd.bind(this);
# -------------------------------------------------------------------
# Git
# -------------------------------------------------------------------
alias ga='git add'
alias gp='git push'
alias gl='git log'
alias gs='git status'
alias gm='git commit -m'
alias gb='git branch'
alias gc='git checkout'
@hartzis
hartzis / Image.jsx
Last active April 15, 2019 23:17
react image component - utilizing recompose, handles errors
import React, { Component, PropTypes } from 'react';
import { setPropTypes, withState, lifecycle, mapProps, compose } from 'recompose';
export const ImageComponent = compose(
setPropTypes({ src: PropTypes.string.isRequired }),
withState('imgState', 'updateImgState', ({ src }) => ({ origSrc: src, currentSrc: src })),
lifecycle({
componentWillReceiveProps(nextProps) {
// if Image components src changes, make sure we update origSrc and currentSrc
if (nextProps.src !== nextProps.imgState.origSrc) {