Skip to content

Instantly share code, notes, and snippets.

View jasonbyrne's full-sized avatar

Jason Byrne jasonbyrne

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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:01
Menu with Basic Generic
class Menu<T> {
private items: T[] = [];
public addItem(item: T) {
this.items.push(item);
}
public getItems(): T[] {
return this.items;
}
@jasonbyrne
jasonbyrne / isAsyncCallback.s
Created April 3, 2022 12:13
Is this an async function?
export const isAsyncCallback = (func: Function): boolean => {
return (
func.constructor.name == 'AsyncFunction' ||
types.isAsyncFunction(func) ||
func.toString().indexOf('__awaiter(') > 0
);
};