Tagged Union implementation and some helper functions in JavaScript.
https://github.com/tariqqubti/js-tagged-union
A package to basically be able to do this:
import {Maybe, Run, Check, Http, Loading, Ok, Err} from '...';
// Each arm could be abstracted into its own function
const state = await Maybe(localStorage.getItem('userInfo')).match({ // null, undefined check
Some: info => Run(() => JSON.parse(info)).match({ // parse json
Ok: info => Check(info.id, x => /valid/.test(x)).match({ // run validation
Pass: async id => (await Http(fetch(`/user/${id}`))).match({ // fetch
Success: res => Ok(res.data), // res.status === 200
NotFound: res => Err(Error(res.data)), // res.status === 404
Err: err => Err(err), // timeout, disconnection, etc.
}),
Fail: () => Err(Error('Invalid stored user info')),
})
Err: () => Err(Error('Could not parse user info')),
})
None: () => Err(Error('Stored user info was not found'))
});