Skip to content

Instantly share code, notes, and snippets.

Avatar

Jason Byrne jasonbyrne

View GitHub Profile
@jasonbyrne
jasonbyrne / color-names.js
Created May 17, 2022
Standard HTML Color Names Array
View color-names.js
export const COLOR_NAMES = [
'aliceblue',
'antiquewhite',
'aqua',
'aquamarine',
'azure',
'beige',
'bisque',
'black',
'blanchedalmond',
@jasonbyrne
jasonbyrne / generate-svg.ts
Created May 17, 2022
Generate Placeholder SVG with Cloudflare Workers
View generate-svg.ts
// Credit:: https://github.com/cloudfour/simple-svg-placeholder
type SvgOptions = {
width: number
height: number
text: string
fontFamily: string
fontWeight: string
bgColor: string
textColor: string
@jasonbyrne
jasonbyrne / fetch-thumbnail.ts
Created May 17, 2022
Fetch Thumbnail, resize and serve with Cloudflare Worker
View fetch-thumbnail.ts
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
@jasonbyrne
jasonbyrne / hello-world.ts
Created May 17, 2022
CloudFlare Workers Router in TypeScript v2
View hello-world.ts
export function helloWorld(): Response {
return new Response('Hello World', {
status: 200,
})
}
@jasonbyrne
jasonbyrne / merge-objects.ts
Created May 2, 2022
Deep Merge and Shallow Merge
View merge-objects.ts
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 / human-readable-list.ts
Created May 2, 2022
Turn an array into a comma-separated string with and or or at the end
View human-readable-list.ts
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 / deep-partial.ts
Created May 2, 2022
DeepPartial in TypeScript
View deep-partial.ts
export type DeepPartial<T> = T extends Function
? T
: T extends object
? { [P in keyof T]?: DeepPartial<T[P]> | undefined }
: T | undefined;
@jasonbyrne
jasonbyrne / food.ts
Created Apr 14, 2022
Get and Change
View food.ts
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 / food.ts
Created Apr 14, 2022
TypeScript Generics
View food.ts
type FoodProperties = {
name: string;
description: string;
price: number;
section: "breakfast" | "lunch" | "dinner" | "dessert";
};
class FoodItem {
constructor(private opts: FoodProperties) {}
View breakfast.ts
type FoodProperties = {
name: string;
description: string;
price: number;
section: "breakfast" | "lunch" | "dinner" | "dessert";
};
class FoodItem {
constructor(private opts: FoodProperties) {}