Skip to content

Instantly share code, notes, and snippets.

View baetheus's full-sized avatar

Brandon Blaylock baetheus

View GitHub Profile
@baetheus
baetheus / actionFactory.ts
Created January 26, 2018 17:55
Pat's Action Factory
export enum ActionState {
INIT = "[ActionState] INIT",
OK = "[ActionState] OK",
ERROR = "[ActionState] ERROR",
}
export interface BaseAction <G, A extends ActionState, P = void> {
readonly group: G;
readonly state: A;
@baetheus
baetheus / notes.md
Last active March 8, 2018 21:39
Angular CLI Notes

New App

ng new -p --minimal true --service-worker true --view-encapsulation OnPush --routing true --style scss

@baetheus
baetheus / tree.ts
Last active April 8, 2018 19:24
Tree Utilities
import { map, filter, reduce, assign } from 'lodash';
/**
* Map over a tree from the bottom-most nodes up. Applies transformation to deepest children,
* then to parents, and so on. You can even rename the property that contains the children.
*
* Usage:
* tmap(
* x => ({ ...x, value: x.value + 1 || 0 }),
* 'children',
@baetheus
baetheus / Complement.ts
Last active November 15, 2018 01:36
Complement Type
type Complement<A, B> = {
[Key in keyof A]: Key extends keyof B ? never : A[Key]
};
// Simple Cascade
const cascade = <A>(a: A) => <B>(b: Complement<B, A>): A & B =>
Object.assign({}, a, b);
const c1 = cascade({ key1: 1 });
const c2 = c1({ key1: 2 }); // Type error, key1 must be never
const c3 = c1({ key2: 2 }); // Has type { key1: number, key2: number }
@baetheus
baetheus / @useReduxFactory.ts
Last active November 18, 2018 02:45
A sample react hook to connect to redux without connect.
import { useContext, useEffect, Context, useState } from 'react';
import { Store } from 'redux';
/**
* Creates a useRedux hook.
*
* First function takes Context.
*
* Second function takes a selector and an optional comparator and
* returns the output of the selector and the store's dispatch function
@baetheus
baetheus / cascade.ts
Created November 18, 2018 03:54
Cascade
// A function that operates like a ternary operator but can be lazy
export type CascadePair<S, C = boolean> = [C, S];
export const cascade = <T>(...cases: CascadePair<T>[]) => {
const match = cases.find(c => c[0]);
return match !== undefined ? match[1] : undefined;
};
export const cascadeOrElse = <T>(def: T, ...cases: CascadePair<T>[]) => {
const match = cascade(...cases);
@baetheus
baetheus / index.html
Last active November 21, 2018 21:49
Parcel React Typescript Setup
npm i @nll/{css,rx-fsa} lodash react react-dom redux
npm i -D @nll/schematics-react @types/{lodash,node,react,react-dom,redux} parcel-bundler typescript
@baetheus
baetheus / rollup.config.js
Last active January 14, 2019 23:50
Rollup + Typescript + Serve + Preact
import del from 'rollup-plugin-delete';
import typescript from 'rollup-plugin-typescript';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import htmlTemplate from 'rollup-plugin-generate-html-template';
import copy from 'rollup-plugin-copy';
import serve from 'rollup-plugin-serve';
import { terser } from 'rollup-plugin-terser';
const createHash = () =>
@baetheus
baetheus / map.pipe.ts
Created January 8, 2019 09:01
Method Overload Issues
import { Pipe, PipeTransform } from '@angular/core';
import { map } from 'lodash';
import { tryCatchEither } from '@loda/utils';
interface MapTransform {
<T, D>(value: T, iteratee: (t: T) => D): D | T;
<T, D>(value: T[], iteratee: (t: T) => D): D[] | T[];
}
@baetheus
baetheus / app.tsx
Last active December 12, 2022 19:13
Preact Hooks Experiment for Typescript
import { h, FunctionalComponent, render, options } from 'preact';
import { handleVnode } from './hooks';
// Wireup experimental hooks
options.vnode = handleVnode;
import Test from './component';
export const Main: FunctionalComponent<any> = () => (