Skip to content

Instantly share code, notes, and snippets.

View baetheus's full-sized avatar

Brandon Blaylock baetheus

View GitHub Profile
@baetheus
baetheus / Angular.ts
Created April 13, 2020 22:47
Angular helpers for @nll/dux test
import { Store as S, createStore, MetaReducer, RunOnce, RunEvery } from "./Store";
import { Reducer } from "./Reducers";
export interface StoreConfig<T> {
readonly reducers: Reducer<T>[];
readonly metaReducers: MetaReducer<T>[];
readonly runOnces: RunOnce<T>[];
readonly runEverys: RunEvery<T>[];
}
@baetheus
baetheus / bin.rs
Last active March 28, 2020 17:57
Playing around with nom and parsing
use nom::{
branch::alt,
bytes::complete::{tag, take_until, take_while},
character::{complete::line_ending, is_alphabetic},
combinator::{map, opt, rest},
sequence::tuple,
IResult,
};
use std::str::from_utf8;
@baetheus
baetheus / output.txt
Last active March 20, 2020 19:23
Rust Store
Print Listener: [1, 2]
SHOULD ONLY HAPPEN ONCE: [1, 2]
Print Listener: [1, 2, 1]
Print Listener: [1, 2, 1, 2]
Print Listener: [1, 2, 1, 2, 3]
Print Listener: [1, 2, 1, 2, 3, 4]
Final State: [1, 2, 1, 2, 3, 4]
@baetheus
baetheus / useTaskDatumEither.ts
Last active November 15, 2019 17:48
Preact hook for @nll/dux AsyncData in an fp-ts Task
import { DatumEither, failure, initial, toRefresh } from '@nll/datum/lib/DatumEither';
import { Task } from 'fp-ts/lib/Task';
import { Errors } from 'io-ts';
import { useEffect, useState } from 'preact/hooks';
export const useTaskDatumEither = <T>(task: Task<DatumEither<Errors, T>>) => {
const [state, setState] = useState<DatumEither<Errors, T>>(initial);
useEffect(() => {
// Setup "cancellation closure"
let linked = true;
@baetheus
baetheus / indexeddb.ts
Last active June 19, 2019 15:49
Quick and dirty wrapper for indexeddb using idb and io-ts
import { Either } from 'fp-ts/lib/Either';
import { openDB } from 'idb';
import { array, Type } from 'io-ts';
import { from, Observable, of, throwError } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
export const objectFactory = (
name: string,
keyPath: string,
option: 'readonly' | 'readwrite' | 'versionchange' = 'readonly',
@baetheus
baetheus / examples.ts
Created June 12, 2019 20:58
Building typescript syntax programatically
import * as ts from 'typescript';
// import { Option as Option } from 'fp-ts/lib/Option';
const a = ts.createImportDeclaration(
undefined,
undefined,
ts.createImportClause(
undefined,
ts.createNamedImports([
ts.createImportSpecifier(
@baetheus
baetheus / io-openapi-3.0.2.ts
Created June 5, 2019 20:56
io-ts codecs for openapi 3.0.2
import { Option } from 'fp-ts/lib/Option';
import * as t from 'io-ts';
import { createOptionFromNullable } from 'io-ts-types';
/**
* Semver RegExp from https://github.com/sindresorhus/semver-regex/blob/master/index.js
*/
const SEMVER_REGEX = /(?<=^v?|\sv?)(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*)(?:\.(?:[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*))*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?(?=$|\s)/gi;
interface SemverBrand {
@baetheus
baetheus / monocle-playground.ts
Created May 21, 2019 01:07
Playing with monocle-ts
import { array } from 'fp-ts/lib/Array';
import { fromNullable, none, Option, some } from 'fp-ts/lib/Option';
import { fromTraversable, Lens, Optional, Prism } from 'monocle-ts';
/* Interfaces */
interface Bug {
bugId: number;
description: Option<string>;
}
@baetheus
baetheus / useRxjs.ts
Last active January 13, 2020 09:08
useRxjs POC
import { some, none, fromNullable } from 'fp-ts/lib/Option';
import { Observable } from 'rxjs'
import { useEffect, useState } from 'react'
const useRxjs = <T>(obs: Observable<T>, init?: T) => {
const [state, setState] = useState(fromNullable(init));
const [errorState, setErrorState] = useState(none);
const [completeState, setCompleteState] = useState(false);
useEffect(() => obs
@baetheus
baetheus / imgp.component.ts
Created April 23, 2019 19:52
Image fallback
import { ChangeDetectionStrategy, Component, ElementRef, EventEmitter, Input, Output, Renderer2 } from '@angular/core';
import { fromEvent, merge, Observable, Subscription } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
const requestImage = (src: string) => {
const img = new Image();
img.src = src;
return merge(fromEvent(img, 'error'), fromEvent(img, 'load')).pipe(
map(evt => {
if (evt.type === 'error') {