Skip to content

Instantly share code, notes, and snippets.

View softwarebygabe's full-sized avatar

Gabe Szczepanek softwarebygabe

View GitHub Profile
const myHouse = new House(
House.WithRooms(5),
House.WithFloors(2),
House.WithMaterial('wood'),
await House.WithExternalData(),
)
@softwarebygabe
softwarebygabe / options_functional_async.ts
Last active January 12, 2023 02:45
How to write an async class constructor using functional options pattern
type Material = 'wood' | 'brick' | 'steel'
type HouseOption = (h: House) => void
class House {
private rooms: number
private floors: number
private material: Material
private externalData: any
type Material = 'wood' | 'brick' | 'steel'
interface HouseOptions {
rooms: number
floors: number
material: Material
bathrooms: number
squareFootage: number
address: string
constructionDate?: Date
type Material = 'wood' | 'brick' | 'steel'
class House {
private rooms: number
private floors: number
private material: Material
private bathrooms: number
private squareFootage: number
private address: string
type Material = 'wood' | 'brick' | 'steel'
type HouseOption = (h: House) => void
class House {
private rooms: number
private floors: number
private material: Material
constructor(...options: HouseOption[]) {
@softwarebygabe
softwarebygabe / options_config.ts
Created June 22, 2020 18:03
This is usually the go-to pattern when the amount of object properties increases
type Material = 'wood' | 'brick' | 'steel'
interface HouseOptions {
rooms: number
floors: number
material: Material
}
class House {
private rooms: number
@softwarebygabe
softwarebygabe / options_basic.ts
Created June 22, 2020 18:02
This would be a typical way to construct an object
type Material = 'wood' | 'brick' | 'steel'
class House {
private rooms: number
private floors: number
private material: Material
constructor(rooms: number, floors: number, material: Material) {
this.rooms = rooms
this.floors = floors
@softwarebygabe
softwarebygabe / Makefile
Last active October 30, 2019 14:11
sample makefile setup
.PHONY: help
help:
@echo ''
@echo 'Makefile'
@echo ''
@echo 'Usage'
@echo ''
@echo ' make run Run the project'
@echo ' make test Run the unit tests'
@echo ' make cover Run the unit tests and enforce coverage'
package foo_test
import (
"errors"
"testing"
"interfaces/foo"
)
type MockClient struct {
package foo
import (
"errors"
)
type IExternalClient interface {
GetData() (string, error)
}