Skip to content

Instantly share code, notes, and snippets.

View sonhanguyen's full-sized avatar

Harry sonhanguyen

  • Melbourne, Australia
View GitHub Profile
@sonhanguyen
sonhanguyen / cache.js
Last active January 30, 2024 03:59
File system-based memoization
/**
* @template [T=any]
* @property {{ (_: T): Promise<void>} & { pending?: Promise<T> }} put
*/
class CacheIOError extends Error {
constructor(cause, context) {
Object.assign(this, context, { cause })
}
}
@sonhanguyen
sonhanguyen / index.html
Last active March 17, 2023 00:08
resume
<head>
<style>
@media print { body { height: 297mm } }
body {
width: 210mm;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
margin: 0;
display: flex;
}
@sonhanguyen
sonhanguyen / mixin_delegate_decorator.ts
Last active October 31, 2022 13:44
kotlin's style delegate in typescript
function delegateTo(prop: string) {
const { __proto__ } = this
Object.setPrototypeOf(this,
new Proxy({}, {
get: (target, name) => {
target = this[prop]
if (name === prop) return target
if (name in __proto__) return __proto__[name]
return target ? target[name] : undefined
@sonhanguyen
sonhanguyen / mobX.md
Last active October 6, 2022 01:50
use revealjs to view

MobX


  • What it is
  • Why do we need it
  • How do we use it
  • How it works


@sonhanguyen
sonhanguyen / userChrome.css
Last active December 6, 2021 05:03
side panel style for using with Tree Tabs
#!/usr/bin/env bash {} /*
cd ~/.mozilla/firefox || cd ~/Library/Application\ Support/Firefox*
source <(grep Default= profiles.ini)
mkdir $Default/chrome
tee $Default/chrome/userChrome.css <<EOF
/*#region Firefox */
#browser,
@sonhanguyen
sonhanguyen / parser.ts
Last active December 24, 2020 07:58
Composable validation
// I could not find any validation library (zod, io-ts etc) that allows custom error types
// so quickly wipped this up, note that the errors here are string literals but they
// might as well be, say, custom classes for pattern matching later
// think about error messages that you need to translate to many languages in the front-end
const formValidator = <S extends Record<string, Validator<any, any>>>(schema: S): Validator<
{ [K in keyof S]: Infer<S[K]>['Input'] },
{ [K in keyof S]:
{ error: Infer<S[K]>['Error']
value: Infer<S[K]>['Input']
@sonhanguyen
sonhanguyen / lens.ts
Last active November 30, 2020 05:27
typescript optics
type Lens<State, Value> = {
(_: State): Value
set: (_: Value) => (_: State) => State
map<T>(_: Lens<Value, T>): Lens<State, T>
}
type Prop<K extends string, V = any> = Lens<Record<K, V>, V>
@sonhanguyen
sonhanguyen / cache.py
Last active October 17, 2020 03:03
A filesystem-based memoization decorator
import os
import collections
import pickle
import json
import urllib.parse
import re
from functools import wraps
from typing import Callable, Any
from dataclasses import dataclass, fields
@sonhanguyen
sonhanguyen / overload.ts
Last active September 20, 2019 01:31
OverloadBuilder
type Func = (..._) => any
const overload = <Default extends Func>(defaultFunc: Default): OverloadBuilder<Default> => {
// @ts-ignore
const match = cases => Object.assign(
(...args) => {
const matcher = cases.find(([ guard ]) => guard(args))
const func = matcher
? matcher[1]
: defaultFunc
@sonhanguyen
sonhanguyen / Default.bttpreset
Created July 20, 2019 02:35
Better touch tool setup for window management
{
"BTTPresetName" : "Default",
"BTTGeneralSettings" : {
"disableScrollingIf3" : true,
"BTTPasteWhenTriggeringClipboardAgain" : true,
"BTTForceNormalClickPressure5F" : 200,
"disableScrollingIf2" : true,
"BTTDidRegisterForUpdateStats" : "3.140",
"BTTShowControlStrip" : true,
"BTTShowControlStripItem" : true,