0.5.11 • Published 8 months ago

clark-fp v0.5.11

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

clark-fp

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.

clark-fp

Install

npm install clark-fp

Examples

Either

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);
}

Option

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);
}

Match

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);

Tag

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);
0.5.10

8 months ago

0.5.11

8 months ago

0.5.8

8 months ago

0.5.9

8 months ago

0.5.7

8 months ago

0.5.4

8 months ago

0.4.5

8 months ago

0.5.3

8 months ago

0.4.4

8 months ago

0.5.6

8 months ago

0.5.5

8 months ago

0.5.0

8 months ago

0.4.1

8 months ago

0.4.0

8 months ago

0.5.2

8 months ago

0.4.3

8 months ago

0.5.1

8 months ago

0.4.2

8 months ago

0.3.0

9 months ago

0.3.1

9 months ago

0.2.0

9 months ago

0.1.0

9 months ago