Introducing our lightweight library designed to enhance code reliability using functional programming techniques. Built to empower developers, it introduces features like either, option, tag, and match. Simplify error handling and promote predictability in your code with ease.
npm install clark-fp
import { either } from "clark-fp";
function readFile(path: string) {
try {
const result = // ...read file here
return either.Ok(result);
} catch (err) {
return either.Err(err);
}
}
const result = readFile("file.txt");
if (either.isOk(result)) {
console.log(`Error: ${result.value.message}`);
} else {
console.log(result);
}
import { option } from "clark-fp";
function readFile(path: string) {
try {
const result = // ...read file here
return option.Some(result)
} catch (err) {
return option.None
}
}
const result = readFile("./file.txt");
if (option.isNone(result)) {
console.log(`Error: file not found`);
} else {
console.log(result);
}
This function was inspired by ts-pattern, the difference is that this library is simpler in terms of pattern matching and you can return a value directly instead of having to create a arrow function
import { match } from "clark-fp";
const status = "active" as "active" | "inactive";
const result = match(status)
.when("active", "You are active")
.when("inactive", (status) => `You are not ${status}`)
.exhaustive();
console.log(result);
import { tag, either } from "clark-fp";
function readFile(path: string) {
try {
const result = // ...read file here
return either.Ok(result);
} catch (err) {
return either.Err(err);
}
}
const path = "file.txt";
const result = await readFile(path);
const msg = tag(result, {
Ok: "The file was read successfully",
Err: (err) => err.message,
});
console.log(msg);