Skip to content

Instantly share code, notes, and snippets.

View raphtlw's full-sized avatar
🙃
chilling

raphtlw raphtlw

🙃
chilling
View GitHub Profile
@raphtlw
raphtlw / gerr.ts
Created February 18, 2023 09:01
go-like error handlers via higher order functions
function safe<P extends ReadonlyArray<unknown>, R>(
func: (...params: P) => R
): (...params: P) => [R, null] | [null, unknown] {
return (...params: P) => {
try {
return [func(...params), null];
} catch (e) {
return [null, e];
}
};
@raphtlw
raphtlw / context.tsx
Last active July 30, 2022 12:30
Inline functional context builder
import {
useContext,
createContext,
type ReactNode,
type Provider as ReactProvider,
} from 'react'
/**
* Utility context builder which streamlines the context
* building and exposing API.
@raphtlw
raphtlw / tsm.ts
Last active March 26, 2022 17:44
TypeScript macro preprocessor
#!/usr/bin/env ts-node
import assert from "assert";
import fs from "fs";
import readline from "readline";
const argv = process.argv.slice(2);
const argFileName = argv[0];
function error(msg: string) {
@raphtlw
raphtlw / script.sh
Created August 11, 2021 08:11
Bash script template with argument parsing
#!/usr/bin/env bash
# ----------------------------------------------------------------------------
# script.sh: Bash script template with argument parsing
#
# Author: Raphael Tang <raphpb1912@gmail.com>
# ----------------------------------------------------------------------------
# Dependencies:
# - dependency1
# - dependency2
@raphtlw
raphtlw / log.ts
Created December 23, 2020 10:07
Simple logging boilerplate for Javascript
export class Log {
private static getTime() {
const date = new Date()
return `[${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}]`
}
static d(_: unknown) {
const message = `${this.getTime()} [debug] ${_}`
console.log(message)
}
static i(_: unknown) {