Getting
funky
with Deno!
funky
brings the beloved Option<T>
, Result<T, E>
and other functional
utilities into your next Deno masterpiece!
This library is heavily inspired by the awesome @hqoss/monads.
Go to full Option<T>
documentation.
import { Option, some, none } from "https://deno.land/x/funky/mod.ts";
import { isUndefined } from "https://deno.land/x/funky/mod.ts";
const getEnvVar = (key: string): Option<string> => {
const value = Deno.env.get(key);
return isUndefined(value) ? none() : some(value);
};
const getHome = getEnvVar("HOME");
const home = getHome.match({
some: (value: string) => value,
none: () => "Could not retrieve HOME path from env."
});
console.log(home); // "/home/alice"
const getMadeUpVar = getEnvVar("MADE_UP_VAR");
const madeUpVar = getMadeUpVar.match({
some: (value: string) => value,
none: () => "Could not retrieve MADE_UP_VAR"
});
console.log(madeUpVar); // "Could not retrieve MADE_UP_VAR"
Go to full Result<T, E>
documentation
import { Result, ok, err } from "https://deno.land/x/funky/mod.ts";
const fetchUser = async (id: number): Promise<Result<User, string>> => {
const url = `api.com/users/${id}`;
const response = await fetch(url);
if (response.status === 404) {
return err(`User with id ${id} does not exist.`);
}
try {
const user = await response.json();
return Ok(user as User);
} catch (error) {
return err("Parsing the response failed.");
}
};
console.log(await fetchUser(42)); // ResultOk(user: { id: 42 })
console.log(await fetchUser(-1)); // ResultErr("User with id -1 does not exist.")
Any and all contributions are welcome!
If you decide to contribute, first of all thank you! Second, please open an issue first to make sure you are not wasting your time working on something that is already in progress.
When you are ready to deliver your awesome input please open a pull request. No pull request will be ignored!
The author of this project is Ivan Stoyanov.
Contact me on Discord fulstackz#1258.
This project is under the ISC license. Feel free to clone, extend and customize, and possibly add a mention to the original 😇
See the LICENSE file for more information.