Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@quanterall/lich

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@quanterall/lich

Lich is a library for chaining computations in TypeScript. It's heavily inspired by the monadic structures in Haskell. In particular Maybe and Either.

  • 1.0.13
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14
Maintainers
2
Weekly downloads
 
Created
Source

Lich - Chaining computations in TypeScript

Lich is a library for chaining computations in TypeScript. It's heavily inspired by the monadic structures in Haskell. In particular Maybe and Either. The goal of lich is to improve flow, safety and reliability of your code.

Installing

Via npm:

$ npm install @quanterall/lich

Via yarn:

$ yarn add @quanterall/lich

Via html:

<script src="https://unpkg.com/@quanterall/lich@1.0.8/dist/index.web.js"></script>

and then use via Lich like so:

<script>
  const maybe = Lich.Just(10);
  const either = Lich.Right("hello world");
</script>

How does it work?

Let's say you want to apply several functions to a string, but you only want to work with it, if it's not empty at the end of each one of them? This is how you may go about it, using lich:

import { Maybe, Just, Nothing } from "lich";

export function parseString(s: string): Maybe<string> {
  return safeTrim(s)
    .map((j) => j.toUpperCase())
    .bind(removePs) // Maybe remove the 'p's in the text.
    .bind(clearSpaces); // Maybe clear up the spaces.
}

function safeTrim(s: string): Maybe<string> {
  const trimmed = s.trim();
  if (trimmed.length === 0) return Nothing();

  return Just(trimmed);
}

function removePs(s: string): Maybe<string> {
  const res = s.replace(/p/g, "");
  if (res.length === 0) return Nothing();

  return Just(res);
}

function clearSpaces(s: string): Maybe<string> {
  const res = s.replace(/ +/g, " ").trim(); // We want only one space between words
  if (res.length === 0) return Nothing();

  return Just(res);
}

If you care about in which step things went wrong you can use Either to track your errors:

import { Either, Right, Left } from "lich";

export function parseString(s: string): Either<string, string> {
  return safeTrim(s)
    .map((j) => j.toUpperCase())
    .bind(removePs) // Either remove the 'p's in the text or return a `Left` with a reason.
    .bind(clearSpaces) // Either clear up the spaces or return `Left` with a reason.
    .onLeft((l) => console.error(l)); // Log the error to the console
}

function safeTrim(s: string): Either<string, string> {
  const trimmed = s.trim();
  if (trimmed.length === 0) return Left("Error in 'safeTrim': String is empty");

  return Right(trimmed);
}

function removePs(s: string): Either<string, string> {
  const res = s.replace(/p/g, "");
  if (res.length === 0) return Left("Error in 'removePs': String is empty");

  return Right(res);
}

function clearSpaces(s: string): Either<string, string> {
  const res = s.replace(/ +/g, " ").trim(); // We want only one space between words.
  if (res.length === 0) return Left("Error in 'clearSpaces': String is empty");

  return Right(res);
}

A more deep comparison against other error handling practices could be found here.

Documentation

Keywords

FAQs

Package last updated on 23 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc