montainer
v0.0.20
Published
A Monadic container for TypeScript where everything is an Array
Downloads
3
Readme
Montainer
Montainer types Listop
and Dictop
provide convenient methods for working with arrays and dicts (JS objects).
const tmp = Dictop.of(myState.foo).do().some().operations();
myState.foo = tmp.unwrap();
myState.error = tmp.getError();
Dictop.of(myState.foo).do().some().operations().done(
(v) => {
myState.foo = v;
},
(err) => {
myState.foo = undefined;
myState.error = err;
}
);
Error handling
Errors detected by Montainer types are not thrown but rather set as property of
a resulting instance. You can either test the result manually using .hasError()
/
.getError()
methods or you can use method done(ok:(v)=>void, err:(e)=>void)
to perform operations based on the result state.
Examples
Listop.of([{name: 'John Doe', reads: [10, 17, 19]}, {name: 'Jane Doe', reads: [3, 5, 21]}])
.flatMap(user => Listop.of(user.reads)) // now we have [10, 17, 19, 3, 5, 21]
.find(v => v === 17)
.unwrapFirst();
Dictop.of({'19e2f7': {time: 10}, '2518af': {time: 13}, '5abfdd': {time: 27}})
.map((item, _) => ({time: item.time + 1})
.unwrap();
// produces: {'19e2f7': {time: 11}, '2518af': {time: 14}, '5abfdd': {time: 28}}