Skip to content

Instantly share code, notes, and snippets.

View bakerface's full-sized avatar

Chris Baker bakerface

  • Test and Controls International
  • Taylorsville, Kentucky
View GitHub Profile
@bakerface
bakerface / abort.ts
Created November 5, 2020 13:49
A tiny typescript implementation of the AbortController API
export type AbortListener = () => void;
export interface AbortSignal {
readonly addEventListener: (type: "abort", fn: AbortListener) => void;
readonly removeEventListener: (type: "abort", fn: AbortListener) => void;
readonly aborted: boolean;
}
export interface AbortController {
readonly abort: () => void;
@bakerface
bakerface / aof.ts
Created November 4, 2020 13:29
TypeScript append-only file stream using async iterators
import fs from "fs";
import util from "util";
import { chan } from "./chan";
import { split } from "./split";
const open = util.promisify(fs.open);
const write = util.promisify(fs.write);
const close = util.promisify(fs.close);
export type Append = (line: string) => void;
@bakerface
bakerface / caseOf.test.ts
Created August 22, 2020 13:37
Pattern matching for types at runtime in TypeScript
import { CaseOf, TypePattern, caseOf } from "./caseOf";
function success<T>(value: T): T {
return value;
}
const errorPattern: TypePattern<never> = {
Array() {
throw new TypeError("Array");
},
@bakerface
bakerface / index.ts
Created May 23, 2020 22:48
Simple Testing with Event Sourcing
export * from "./pattern-of";
export * from "./test";
export * from "./union-of";
@bakerface
bakerface / npm-refresh.js
Last active August 1, 2019 12:12
A script to refresh npm dependencies
#!/usr/bin/env node
const child_process = require("child_process");
const fs = require("fs");
function spawn(command = "", args = []) {
return new Promise((resolve, reject) => {
const proc = child_process.spawn(command, args, {
stdio: "inherit"
});
@bakerface
bakerface / cancellation-token.ts
Last active March 4, 2024 22:51
CancellationToken implementation in Typescript
export type CancellationSubscriber = (err: Error) => void;
export type Unsubscribe = () => void;
export interface CancellationToken {
subscribe(subscriber: CancellationSubscriber): Unsubscribe;
}
export class CancellationError extends Error {
public readonly name = "CancellationError";
public readonly message = "The operation was cancelled";
@bakerface
bakerface / rotate.js
Last active June 17, 2019 13:00
rotate
#!/usr/bin/env node
const fs = require("fs");
function usage() {
console.error("rotate v1.0.0");
console.error();
console.error("$ rotate -r <file>");
console.error("Rotates a file with using a random offset.");
console.error();
@bakerface
bakerface / schema.ts
Last active July 7, 2021 11:03
Schemas in typescript
export interface ArrayOfSchema<T> {
readonly type: "arrayOf";
readonly payload: T;
}
function arrayOfSchema<T>(payload: T): ArrayOfSchema<T> {
return { type: "arrayOf", payload };
}
export interface BooleanSchema {
@bakerface
bakerface / store.ts
Last active May 25, 2019 15:53
An exercise in implementing redux, from scratch, in typescript
export interface Store<State, Action> extends MiddlewareStore<State, Action> {
readonly subscribe: Subscribe;
}
export interface MiddlewareStore<State, Action> {
readonly dispatch: Dispatch<Action>;
readonly getState: GetState<State>;
}
export type Subscribe = (fn: Subscriber) => Unsubscribe;
@bakerface
bakerface / starter.js
Last active June 21, 2019 11:33
A script to generate a typescript starter project
#!/usr/bin/env node
const child_process = require("child_process");
const fs = require("fs");
const path = require("path");
const util = require("util");
const templates = [];
templates.push({