Last active
March 23, 2023 01:48
-
-
Save turbotobias/8fb8c324a63beb4bcfc66e30e42892a4 to your computer and use it in GitHub Desktop.
Revisions
-
turbotobias revised this gist
Mar 23, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ import { Ok, Err } from "./@result" const truthy = (arg?: any) => (!!arg ? Ok(arg) : Err(`falsy`)) -
turbotobias revised this gist
Mar 23, 2023 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,3 @@ export type Result<T, E = undefined> = { ok: true; value: T } | { ok: false; error: E | undefined } export const Ok = <T>(data: T): Result<T, never> => ({ ok: true, value: data }) export const Err = <E>(error?: E): Result<never, E> => ({ ok: false, error }) -
turbotobias revised this gist
Mar 23, 2023 . 3 changed files with 25 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,7 @@ import { type Result, Ok, Err } from "./@result" const truthy = (arg?: any) => (!!arg ? Ok(arg) : Err(`falsy`)) const truth = truthy("something") truth.ok ? console.log(`truthy`) : truth.error ? console.trace(`falsy ${truth.error}`) : console.trace(`unknown error`) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,12 @@ import { type Result, Ok, Err } from "./@result" const numberIsLessThan10 = (num: number): Result<number, string> => { if (num < 10) return Ok(num) return Err("number is not less than 10") } const number = numberIsLessThan10(5) if (number.ok) useNumber(number.value) // valid else if (number.error) console.trace(number.error) // invalid with known error else console.trace("unknown error") // invalid with unknown error This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,17 @@ import { type Result, Ok, Err } from "./@result" const fetchGeoJson = async (): Promise<Result<GeoJson, string>> => { const res = await fetch(`/api`) const json = await res.json() if (!json) return Err("could not get json from api") if (!validate(json.geo)) return Err("did not get valid geo json from api") return Ok(json.geo) } const geoJson = await fetchGeoJson() if (geoJson.ok) useGeoJson(geoJson.value) // valid else if (geoJson.error) console.trace(geoJson.error) // invalid with known error else console.trace("unknown error") // invalid with unknown error -
turbotobias revised this gist
Mar 23, 2023 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ const truthy = (arg?: any) => (!!arg ? Ok(arg) : Err(`falsy`)) const truth = truthy("something") truth.ok ? console.log(`truthy`) : truth.error ? console.trace(`falsy ${truth.error}`) : console.trace(`unknown error`) -
turbotobias revised this gist
Mar 23, 2023 . 2 changed files with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ import { type Result, Ok, Err } from "./@result" const numberIsLessThan10 = (num: number): Result<number, string> => { if (num < 10) return Ok(num) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ import { type Result, Ok, Err } from "./@result" const fetchGeoJson = async (): Promise<Result<GeoJson, string>> => { const res = await fetch(`/api`) const json = await res.json() -
turbotobias revised this gist
Mar 23, 2023 . 2 changed files with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,6 @@ const numberIsLessThan10 = (num: number): Result<number, string> => { const number = numberIsLessThan10(5) if (number.ok) useNumber(number.value) // valid else if (number.error) console.trace(number.error) // invalid with known error else console.trace("unknown error") // invalid with unknown error This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,6 +10,6 @@ const fetchGeoJson = async (): Promise<Result<GeoJson, string>> => { const geoJson = await fetchGeoJson() if (geoJson.ok) useGeoJson(geoJson.value) // valid else if (geoJson.error) console.trace(geoJson.error) // invalid with known error else console.trace("unknown error") // invalid with unknown error -
turbotobias revised this gist
Mar 23, 2023 . 3 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes. -
turbotobias revised this gist
Mar 23, 2023 . 3 changed files with 29 additions and 35 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ const fetchGeoJson = async (): Promise<Result<GeoJson, string>> => { const res = await fetch(`/api`) const json = await res.json() if (!json) return Err("could not get json from api") if (!validate(json.geo)) return Err("did not get valid geo json from api") return Ok(json.geo) } const geoJson = await fetchGeoJson() if (geoJson.ok) useGeoJson(geoJson.value) // ok else if (geoJson.error) console.trace(geoJson.error) // known error else console.trace("unknown error") // unknown error This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,38 +1,5 @@ /** made by Dan Imhoff, the code below is copy-pasted from https://www.huy.rocks/everyday/02-14-2022-typescript-implement-rust-style-result */ export type Result<T, E = undefined> = { ok: true; value: T } | { ok: false; error: E | undefined } export const Ok = <T>(data: T): Result<T, never> => ({ ok: true, value: data }) export const Err = <E>(error?: E): Result<never, E> => ({ ok: false, error }) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ import { type Result, Ok, Err } from "./result" const numberIsLessThan10 = (num: number): Result<number, string> => { if (num < 10) return Ok(num) return Err("number is not less than 10") } const number = numberIsLessThan10(5) if (number.ok) useNumber(number.value) // ok else if (number.error) console.trace(number.error) // known error else console.trace("unknown error") // unknown error -
turbotobias revised this gist
Mar 23, 2023 . 1 changed file with 8 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,8 @@ /** made by Dan Imhoff, the code below is copy-pasted from https://www.huy.rocks/everyday/02-14-2022-typescript-implement-rust-style-result */ export type Result<T, E = undefined> = { ok: true; value: T } | { ok: false; error: E | undefined } export const Ok = <T>(data: T): Result<T, never> => { ok: true, value: data } export const Err = <E>(error?: E): Result<never, E> => { ok: false, error } /** * simple example @@ -20,8 +14,9 @@ const numberIsLessThan10 = (num: number): Result<number, string> => { const number = numberIsLessThan10(5) if (number.ok) useNumber(number.value) // ok else if (number.error) console.trace(number.error) // known error else console.trace("unknown error") // unknown error /** * async example @@ -38,5 +33,6 @@ const fetchGeoJson = async (): Promise<Result<GeoJson, string>> => { const geoJson = await fetchGeoJson() if (geoJson.ok) useGeoJson(geoJson.value) // ok else if (geoJson.error) console.trace(geoJson.error) // known error else console.trace("unknown error") // unknown error -
turbotobias revised this gist
Mar 23, 2023 . 1 changed file with 8 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,13 +13,15 @@ export const Err = <E>(error?: E): Result<never, E> => { /** * simple example **/ const numberIsLessThan10 = (num: number): Result<number, string> => { if (num < 10) return Ok(num) return Err("number is not less than 10") } const number = numberIsLessThan10(5) if (number.ok) useNumber(number.value) if (!number.ok) console.trace(number.error) /** * async example @@ -34,7 +36,7 @@ const fetchGeoJson = async (): Promise<Result<GeoJson, string>> => { return Ok(json.geo) } const geoJson = await fetchGeoJson() if (geoJson.ok) useGeoJson(geoJson.value) if (!geoJson.ok) console.trace(geoJson.error) -
turbotobias revised this gist
Mar 23, 2023 . 1 changed file with 29 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,3 +9,32 @@ export const Ok = <T>(data: T): Result<T, never> => { export const Err = <E>(error?: E): Result<never, E> => { return { ok: false, error } } /** * simple example **/ const numberIsLessThan10 = (num: number): Result<boolean, string> => { if (num < 10) return Ok(num) return Err("number is not less than 10") } if (numberIsLessThan10.ok) useNumber(result.value) if (!numberIsLessThan10.ok) console.trace(result.error) /** * async example **/ const fetchGeoJson = async (): Promise<Result<GeoJson, string>> => { const res = await fetch(`/api`) const json = await res.json() if (!json) return Err("could not get json from api") if (!validate(json.geo)) return Err("did not get valid geo json from api") return Ok(json.geo) } const result = await fetchGeoJson() if (result.ok) useGeoJson(result.value) if (!result.ok) console.trace(result.error) -
turbotobias revised this gist
Mar 23, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ /** made by Dan Imhoff, the code below is copy-pasted from https://www.huy.rocks/everyday/02-14-2022-typescript-implement-rust-style-result */ export type Result<T, E = undefined> = { ok: true; value: T } | { ok: false; error: E | undefined } -
turbotobias revised this gist
Mar 23, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ /** made by Dan Imhoff, the code below is copy-pasted from https://www.huy.rocks/everyday/02-14-2022-typescript-implement-rust-style-result?ref=morioh.com */ export type Result<T, E = undefined> = { ok: true; value: T } | { ok: false; error: E | undefined } -
turbotobias created this gist
Mar 23, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ /** made by Dan Imhoff, the code below is copy-pasted from https://imhoff.blog/posts/using-results-in-typescript */ export type Result<T, E = undefined> = { ok: true; value: T } | { ok: false; error: E | undefined } export const Ok = <T>(data: T): Result<T, never> => { return { ok: true, value: data } } export const Err = <E>(error?: E): Result<never, E> => { return { ok: false, error } }