Skip to content

Instantly share code, notes, and snippets.

import produce, {Draft} from 'immer';
import {useReducer, useMemo} from 'react';
type ActionCreator<State, Payload> = (
payload: Payload
) => {type: string; payload: Payload};
type CreatedAction<State, Payload> = ActionCreator<State, Payload> & AnyAction;
type Handler<State, Payload> = (state: Draft<State>, payload: Payload) => void;
type Handlers<State> = {
[actionType: string]: Handler<State, any>;
{
"amount": "0.00",
"resptext": "Wrong currency for merch",
"cardproc": "NASH",
"acctid": "1",
"respcode": "32",
"defaultacct": "Y",
"merchid": "820000000192",
"token": "9545666483645454",
"respproc": "PPS",
type GuardedResult<ReturnType, DefaultValueType = ReturnType> = ReturnType | DefaultValueType;
type GuardedFn<ReturnType, DefaultValueType = ReturnType> = (
...args
) => GuardedResult<ReturnType, DefaultValueType>;
function guard<ReturnType, DefaultValueType = ReturnType>(
fn: GuardedFn<ReturnType, DefaultValueType>,
defaultValue: GuardedResult<ReturnType, DefaultValueType>
) {
const guarded: GuardedFn<ReturnType, DefaultValueType> = (...args) => {
type composeFn<T> = (x: T) => T | Promise<T>;
function compose<T>(...fns: composeFn<T>[]) {
return async (x: T | Promise<T>): Promise<T> =>
fns.reduce((v, f) => (async () => f(await v))(), x);
}
const parse = guard(input => JSON.parse(input), {});
/**
* - Will always be an object
* - Don't have to clutter with try/catch
* - Can test if the returned value is default value to know success
* - In development errors are logged to console and browser notifications sent
* - In development notifications mesages are cached to not spam developer
* - In production, errors are sent to error logging service
*/
const usePostMessageHandler = (messageKey, callback) => {
useEffect(() => {
const receiveMessage = event => {
if (event.data === messageKey) {
callback(event);
}
};
window.addEventListener("message", receiveMessage, false);
return () => {
(function e(t, n, r) {
function s(o, u) {
if (!n[o]) {
if (!t[o]) {
var a = typeof require == "function" && require;
if (!u && a) return a(o, !0);
if (i) return i(o, !0);
throw new Error("Cannot find module '" + o + "'");
}
var f = (n[o] = { exports: {} });
expect.extend({
toHaveBeenCalledWithSnapshot: received => {
expect(received).toHaveBeenCalled();
expect(received.mock.calls).toMatchSnapshot();
return {
message: () => '',
pass: true,
};
},
class Node {
constructor(value){
this.value = value;
this.left = null;
this.right = null;
}
insert(value){
if(value < this.value){
if(!this.left){
this.left = new Node(value);
@justinobney
justinobney / BetterSemanticConfirm.js
Last active August 27, 2018 20:43
BetterSemanticConfirm.js
import React, { Component } from "react";
import { Button, Modal } from "semantic-ui-react";
export class Confirm extends Component {
state = { open: false };
static defaultProps = {
onClose: () => {}
};
_handleClose = () => {
this.setState({ open: false });