4.0.1 • Published 3 months ago

kekka v4.0.1

Weekly downloads
6
License
ISC
Repository
github
Last release
3 months ago

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.

4.0.1

3 months ago

4.0.0

3 months ago

3.0.1

10 months ago

3.0.0

10 months ago

2.0.3

1 year ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.0

3 years ago

0.0.1

3 years ago