Skip to content
/ kekka Public

Result Monad adaptation for easy usage in JavaScript

License

Notifications You must be signed in to change notification settings

Apemb/kekka

Repository files navigation

Kekka

A set of monad objects to simplify and clarify your business logic:

  • Result
  • Optional

The first object is the Result Object inspired by Rust Result Monad. The second is the java inspired Optional.

GOAL

Managing unsuccessful but not erroneous return value

Never felt that something was missing in Javascript way of managing errors ? A middle ground between returning undefined for business reasons and throwing Errors for business reasons.

getUser(userId)  // returns undefined if no user found
  .then((user) => {
    if(!user) {
      // do stuff knowing user was not found
    } 
  })

Or throwing errors for business reasons ?

getUser(userId)  // returns undefined if no user found
  .catch((error) => {
    if(error instanceof NoUserFoundError) {
      // do stuff knowing user was not found
    } 
  }) 

Ever heard about Railway Programming ? or the Rust Result Monad ?

Wanting something like that in Javascript ? I did ! Enter the Result class.

Defeating defensive programming

Tired of writing endless guard clauses like

if(!userName) {
  return
}

or

if(user && user.name) {
  // do something
}

Maybe want more elegant and explicit way to signal optional values in your business logic ? Look out for the Optional class

Changelog

  • v4.0: Add Optional Object
  • v3.0: Fix broken import from different modules in mono-repo
  • v2.0: Transcription to Typescript
    • Typescript Types
    • Suppression of enableResultPromiseHelpers function
    • Removal of support for non-native promises

Documentation

Result Object Documentation

Optional Object Documentation

There is a chai plugin available to make sweeter assertions: chai-kekka.

About

Result Monad adaptation for easy usage in JavaScript

Resources

License

Stars

Watchers

Forks

Packages

No packages published