Shulk
Introduction Get startedBasics
Tagged unions Pattern matchingMonads
Result Maybe LoadingAsync
Procedure ConcurrentlyOthers
WrappersWrappers
Shulk helps you to make your code safer by providing useful tools and structures, but you’ll probably have to use unsafe third-party libraries or legacy code at some point.
To help you keep your code safe, Shulk provides wrappers that enable you to transform unsafe functions outputs into safe monads.
resultify
The resultify
wrapper takes an unsafe function and return its output in a Result
.
You have probably used the JSON.stringify()
method in your code before, but did you know that it will throw a TypeError
if it encounters a BigInt
? Probably not, and you won’t learn it from its signature.
It is one of the rare functions of the JS standard library that actually throws an error instead of returning a null
or an incorrect value. It is an improvement, but for us who want to build stable and reliable applications, it is not enough.
Let’s make JSON.stringify()
way safer by using Shulk’s resultify
wrapper:
maybify
The maybify
wrapper takes a function that can return undefined
, null
, or NaN
, and returns its output in a Maybe
monad, with the None
state representing undefined
, null
, or NaN
.
Let’s reuse our JSON.stringify
from before, but this time we will want a Maybe
instead of a Result
.