From 9e28ea8e6c657749555b88c476e9530c4f3b3e8c Mon Sep 17 00:00:00 2001 From: Keith Bauson Date: Mon, 12 Apr 2021 18:31:46 -0400 Subject: [PATCH] basic optic --- .envrc | 1 + .gitignore | 2 +- src/app.ts | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..0e1c5c2 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +eval "$(nle direnv)" diff --git a/.gitignore b/.gitignore index ae8f1bf..f2260d7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -node_modules/ +node_modules dist/ cache/ .cache/ diff --git a/src/app.ts b/src/app.ts index a5b9709..6762a65 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1 +1,88 @@ -console.log('foo') +export declare class Optic { + static identity: Optic + + get: Fn<[S], A> + put: Fn<[S, A], S> + + to(get: Fn<[A], B>, put: Fn<[A, B], A>): Optic + to(optical: Optical): Optic + + of(get: Fn<[T], S>, put: Fn<[T, S], T>): Optic + of(optical: Optical): Optic + + pick(keys: KS): Optic> + omit(keys: KS): Optic> + + map(optical: Optical, B>): Optic + + at>(key: K): Optic[K]>> + + choices: { [K in keyof Choice]: Optic[K], A> } +} + +export type Optical = + | Optic + | Exclude> + | ((attrs: { [K in keyof A]: Optic }) => Optical) + | { [K in keyof A]: Optical } + +export declare function optic(): Optic + +export type Record = keyof T extends never + ? never + : Exclude, Function> +export type Choice = NotUnknown : never>> +type Single = ChoiceType extends never + ? Intersect extends never + ? unknown + : T + : { [K in ChoiceType]: undefined } +type ChoiceType = string extends T + ? never + : number extends T + ? never + : symbol extends T + ? never + : T extends keyof any + ? T + : T extends true + ? 'true' + : T extends false + ? 'false' + : never + +export type Fn = (...args: Args) => Result +export type Maybe = 'nothing' | { just: T } +export type Intersect = (T extends any ? Fn<[T], void> : never) extends Fn< + [infer I], + void +> + ? I + : never +export type ValueOf = T[keyof T] +export type ValueType = [T] extends [(infer U)[]] + ? U + : [T] extends [{ [_ in any]: infer U }] + ? U + : never +type NotUnknown = [unknown] extends [T] ? never : T +export type Diff = IfEq> +export type Common = Pick> +type IfEq = [T] extends [U] ? ([U] extends [T] ? A : B) : B + +export type VNode = + | { Div: VNode[] } + | { Text: string } + | { Input: string } + | { Button: { label: string; clicked: boolean } } + | 'Break' + | 'Divider' + +export const { + Div, + Text, + Input, + Button, + Break, + Divider, +} = optic().choices