diff --git a/.changeset/chilled-coins-eat.md b/.changeset/chilled-coins-eat.md index a410635..f46740d 100644 --- a/.changeset/chilled-coins-eat.md +++ b/.changeset/chilled-coins-eat.md @@ -2,13 +2,13 @@ "@oofdere/crabrave": major --- -## whar? +#### whar? we are now lying to tsc to make the thing work well, unfortunately this is a breaking change. but ONLY in your types; logic will work the same as it already did. -## whyyyyyy? -because typescript really hated what we were doing before so now we hide that from it so it hates it less +#### whyyyyyy? +because typescript REALLY hated what we were doing before so now we hide that from it so it hates it less -## haEh to upd8 code???? +#### haEh to upd8 code???? to update your code just replace all instances of `Enum` with `E`. that's right, just the object is all you need. that's right, no more `Enum, Enum>>`, now it's just `Result`. > these release notes are highly unserious because no one should have had this deployed anywhere at the time of these changes. but they are accurate. \ No newline at end of file diff --git a/.changeset/healthy-ways-hammer.md b/.changeset/healthy-ways-hammer.md new file mode 100644 index 0000000..1cb1a95 --- /dev/null +++ b/.changeset/healthy-ways-hammer.md @@ -0,0 +1,5 @@ +--- +"@oofdere/crabrave": minor +--- + +add the Enum() factory function as a better DX alternative to pack() diff --git a/examples/colors.ts b/examples/colors.ts index 64d2946..f2237b0 100644 --- a/examples/colors.ts +++ b/examples/colors.ts @@ -1,4 +1,4 @@ -import { type Enum, match, pack } from ".." +import { Enum, match, pack } from ".." type Colors = { Red: number; @@ -6,10 +6,11 @@ type Colors = { Green: number; Rgb: [number, number, number]; }; +const Colors = Enum() -const red = pack("Red", 128) //=> const red: Enum -const green = pack("Green", 128) //=> const green: Enum -const blue = pack("Blue", 128) //=> const blue: Enum +const red = Colors("Red", 128) //=> const red: Enum +const green = Colors("Green", 128) //=> const green: Enum +const blue = Colors("Blue", 128) //=> const blue: Enum function toRGB(color: Colors) { // returns number[] const a = match(color, { diff --git a/index.ts b/index.ts index fb8bff6..0e2130a 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1,3 @@ -export { type Enum, type EnumChecked, pack, match } from "./src/enum"; +export { Enum, type EnumChecked, pack, match } from "./src/enum"; export { type Option, Some, None } from "./src/option"; export { type Result, Ok, Err } from "./src/result"; \ No newline at end of file diff --git a/src/fetch.ts b/src/fetch.ts index 4ab65bf..f7e8c6f 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -1,32 +1,31 @@ import { EnumChecked, match, pack, type Enum } from "./enum" import { Err, Ok, type Result } from "./result" -// biome-ignore lint/suspicious/noRedeclare: makes sense to redeclare for Err -type Err = { +type FetchErr = { AbortError: DOMException, NotAllowedError: DOMException, TypeError: TypeError, OtherError: unknown } -// biome-ignore lint/suspicious/noRedeclare: -type FetchResult = Enum> - -async function f(input: string | URL | globalThis.Request, init?: RequestInit): Promise { +async function f(input: string | URL | globalThis.Request, init?: RequestInit): Promise> { try { return Ok(await fetch(input, init)) } catch (e) { if (e instanceof DOMException) { - return Err(['AbortError', e]) + return Err(pack('AbortError', e)) } - return Err(pack('OtherError', e)) + return Err(pack('OtherError', e)) //=> } - - return Err(pack('OtherError', undefined)) } -match(await f('i'), { - Ok: (x) => { }, - Err: (x) => { } +match(await f('i'), { //=> + Ok: (x) => x, //=> + Err: (x) => match(x, { + AbortError(x) { //=> + console.log("wtaf") + }, + _: x => x //=> + }) //=> }) \ No newline at end of file