ng new -p --minimal true --service-worker true --view-encapsulation OnPush --routing true --style scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export enum SomeIds { | |
| ONE = 'one', | |
| TWO = 'two', | |
| } | |
| export enum SomeType { | |
| RED = 'red', | |
| BLUE = 'blue', | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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[]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { |