Skip to content

carbonteq/fp

Repository files navigation

FP Utils

Installation

npm i @carbonteq/fp
pnpm i @carbonteq/fp
yarn add @carbonteq/fp

Usage

import { Option, matchOpt } from "@carbonteq/fp";

const safeDiv = (num: number, denom: number): Option<number> => {
  if (denom === 0) return Option.None;

  return Option.Some(num / denom);
};

const getFormatted = (opt: Option<number>): string => {
  return matchOpt(opt, {
    Some: (n) => `Result: ${n}`,
    None: () => "Cannot divide by zero",
  });
};

console.log(getFormatted(safeDiv(10, 2))); // Result: 5
console.log(getFormatted(safeDiv(10, 0))); // Cannot divide by zero