Skip to content

Instantly share code, notes, and snippets.

View sonhanguyen's full-sized avatar

Harry sonhanguyen

  • Melbourne, Australia
View GitHub Profile
@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 / 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 / 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;
}