Skip to content

Instantly share code, notes, and snippets.

View baetheus's full-sized avatar

Brandon Blaylock baetheus

View GitHub Profile
@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 / @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 / 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 / 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 / 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 / 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 / typeNarrowingProblem1.ts
Last active January 23, 2018 21:10
typeNarrowingProblem1
export enum SomeIds {
ONE = 'one',
TWO = 'two',
}
export enum SomeType {
RED = 'red',
BLUE = 'blue',
}
@baetheus
baetheus / rec-mergeMap.ts
Created November 14, 2017 16:44
Recursive rxjs mergeMap
function repeat(n: number): Observable<number> {
return of(n).pipe(
mergeMap(o => of(o).pipe(
merge(repeat(n+1))
))
);
}
repeat(0).subscribe(n => console.log('Got me a number', n), e => console.error(e), () => console.log('All done'));
@baetheus
baetheus / actionWrapper.ts
Last active January 23, 2018 23:08
POC for reducing action boilerplate
// This is where the magic is
export class PayloadAction<T, P = undefined> {
readonly type: T;
constructor(public readonly payload: P) {};
}
// These would be existing interfaces
export interface SomePayload {
stuff: string;
things: number[];
@baetheus
baetheus / cancellableEffectFactory.ts
Last active November 7, 2022 14:15
A factory function POC for cancellable ngrx effects
import { Action, Effect } from "@ngrx/store";
import { Observable } from "rxjs/Observable";
import { of } from "rxjs/observable/of";
import "rxjs/add/operator/switchMap";
import "rxjs/add/operator/takeUntil";
import "rxjs/add/operator/filter";
import "rxjs/add/operator/map";
// For POC typechecking force payloads on Actions.
export interface CancellableAction<T> extends Action {