You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

try-typescript

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

try-typescript

This repository implements a Try class inspired by the Vavr library in Java. The Try class is a functional programming construct for handling computations that may succeed or fail. It encapsulates exceptions and streamlines error handling, reducing boiler

0.2.2
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Try-Typescript

Overview

This repository contains an implementation of the Try class, inspired by the Vavr library in Java. The Try class is a functional programming construct designed to handle computations that may result in success or failure. It allows developers to encapsulate exceptions and manage error handling in a more functional and expressive manner, reducing boilerplate code and improving code readability.

Features

  • Success and Failure Handling: The implementation provides two subclasses, Success and Failure, to represent the result of computations. This makes it easy to handle both successful outcomes and exceptions gracefully.

  • Functional Methods: Includes a variety of methods that allow for functional transformations and error recovery:

    • Mapping: Transform the result of a successful computation using map and flatMap.
    • Error Recovery: Recover from failures using recover and recoverWith methods.
    • Conditional Execution: Filter results with filter and execute side effects with onSuccess and onFailure.
  • Option and Either Conversion: Convert the Try results into Option or Either for seamless integration with other functional programming constructs.

  • Custom Exception Handling: Provides mechanisms to specify custom behavior for both success and failure cases, enabling tailored error handling strategies.

Usage (Will be updated as soon as more functionality is implemented)

To use the Try class, instantiate it with a computation that may fail, and utilize the available methods to handle the result. Below is an example of how to use the Try class in TypeScript:

// Example of using the Try class
const result = new Try<number>(() => {
    return 10 / 2; // Successful computation
});

result
    .map(value => value * 2)
    .onSuccess(value => console.log(`Success: ${value}`)) // Output: Success: 10
    .onFailure(error => console.error(`Failed with error: ${error.message}`));

const failureResult = new Try<number>(() => {
    throw new Error("Division by zero");
}).recover(() => 0);

console.log(failureResult.get()); // Output: 0

Available functions

  • get
  • getOrElse
  • getOrElseGet
  • getOrElseThrow
  • isSuccess
  • isFailure
  • map
  • flatMap
  • recover
  • recoverWith
  • andThen
  • filter
  • onFailure
  • onSuccess
  • getCause
  • filterNot
  • peek

FAQs

Package last updated on 16 Oct 2024

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.