Skip to content

Instantly share code, notes, and snippets.

View jasonbyrne's full-sized avatar

Jason Byrne jasonbyrne

View GitHub Profile
@jasonbyrne
jasonbyrne / types.ts
Created April 14, 2022 04:03
Things at a Restaurant
class Drink {
name: string;
price: number;
ounces: number;
}
class Food {
name: string;
price: number;
type: "appetizer" | "entree";
@jasonbyrne
jasonbyrne / menu.ts
Created April 14, 2022 04:10
Menu with Generic Constraint
class Menu<T extends Food | Drink> {
private items: T[] = [];
public addItem(item: T) {
this.items.push(item);
}
public getItems(): T[] {
return this.items;
}
@jasonbyrne
jasonbyrne / breakfast.ts
Created April 14, 2022 04:20
Eggs Benedict
type FoodProperties = {
name: string;
description: string;
price: number;
section: "breakfast" | "lunch" | "dinner" | "dessert";
};
class FoodItem {
constructor(private opts: FoodProperties) {}
@jasonbyrne
jasonbyrne / food.ts
Created April 14, 2022 04:41
TypeScript Generics
type FoodProperties = {
name: string;
description: string;
price: number;
section: "breakfast" | "lunch" | "dinner" | "dessert";
};
class FoodItem {
constructor(private opts: FoodProperties) {}
@jasonbyrne
jasonbyrne / food.ts
Created April 14, 2022 04:49
Get and Change
class FoodItem {
constructor(private opts: FoodProperties) {}
public get<T extends keyof FoodProperties>(property: T): FoodProperties[T] {
return this.opts[property];
}
public change<T extends keyof FoodProperties>(
property: T,
value: FoodProperties[T]
@jasonbyrne
jasonbyrne / deep-partial.ts
Created May 2, 2022 13:01
DeepPartial in TypeScript
export type DeepPartial<T> = T extends Function
? T
: T extends object
? { [P in keyof T]?: DeepPartial<T[P]> | undefined }
: T | undefined;
@jasonbyrne
jasonbyrne / human-readable-list.ts
Created May 2, 2022 13:03
Turn an array into a comma-separated string with and or or at the end
export const humanReadableList = (
array: unknown[],
join: ',' | ';' = ',',
finalJoin = 'and',
): string => {
if (!Array.isArray(array) || array.length == 0) return '';
if (array.length == 1) return String(array[0]);
const arr = array.slice(0),
last = arr.pop();
return array.length > 2
@jasonbyrne
jasonbyrne / merge-objects.ts
Created May 2, 2022 13:04
Deep Merge and Shallow Merge
export const shallowMerge = <T extends object = Record<string, any>>(
...objects: T[]
): T => {
return objects.reduce((prev, cur) => ({ ...prev, ...cur }), {} as T);
};
export const deepMerge = <T extends object = Record<string, any>>(
target: T,
...sources: object[]
): T => {
@jasonbyrne
jasonbyrne / hello-world.ts
Created May 17, 2022 11:32
CloudFlare Workers Router in TypeScript v2
export function helloWorld(): Response {
return new Response('Hello World', {
status: 200,
})
}
@jasonbyrne
jasonbyrne / fetch-thumbnail.ts
Created May 17, 2022 11:37
Fetch Thumbnail, resize and serve with Cloudflare Worker
import { IncomingRequest } from '../incoming-request'
const buckets: { [key: string]: string } = {
'bucket-name': 'https://bucket-url/',
}
interface RequestInitWithCf extends RequestInit {
cf: RequestInitCfProperties & {
image: BasicImageTransformations & {
quality?: number | undefined