Skip to content

Commit

Permalink
enum factory
Browse files Browse the repository at this point in the history
  • Loading branch information
oofdere committed Aug 13, 2024
1 parent 1f528cc commit 109e57d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
8 changes: 4 additions & 4 deletions .changeset/chilled-coins-eat.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<E>` with `E`. that's right, just the object is all you need. that's right, no more `Enum<Result<Enum<Success>, Enum<Error>>>`, now it's just `Result<Success, Error>`.

> 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.
5 changes: 5 additions & 0 deletions .changeset/healthy-ways-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@oofdere/crabrave": minor
---

add the Enum<E>() factory function as a better DX alternative to pack<E>()
9 changes: 5 additions & 4 deletions examples/colors.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { type Enum, match, pack } from ".."
import { Enum, match, pack } from ".."

type Colors = {
Red: number;
Blue: number;
Green: number;
Rgb: [number, number, number];
};
const Colors = Enum<Colors>()

const red = pack("Red", 128) //=> const red: Enum<Colors>
const green = pack<Colors>("Green", 128) //=> const green: Enum<Colors>
const blue = pack<Colors>("Blue", 128) //=> const blue: Enum<Colors>
const red = Colors("Red", 128) //=> const red: Enum<Colors>
const green = Colors("Green", 128) //=> const green: Enum<Colors>
const blue = Colors("Blue", 128) //=> const blue: Enum<Colors>

function toRGB(color: Colors) { // returns number[]
const a = match(color, {
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -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";
25 changes: 12 additions & 13 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -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: <explanation>
type FetchResult = Enum<Result<Response, unknown>>

async function f(input: string | URL | globalThis.Request, init?: RequestInit): Promise<FetchResult> {
async function f(input: string | URL | globalThis.Request, init?: RequestInit): Promise<Result<Response, FetchErr>> {
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 //=>
}) //=>
})

0 comments on commit 109e57d

Please sign in to comment.