1.4.3 • Published 2 years ago

maybe-for-sure v1.4.3

Weekly downloads
3
License
ISC
Repository
github
Last release
2 years ago

maybe-for-sure

A lightweight stream-like maybe/or else monad for typescript. maybe(val).valueOr(other) grant you no unexpected undefined and hence allows you to write you TS-code in a more fluid way, skipping some of those if-else nestings.

Maybe also provides some useful filters and chaining into object structures ie

Some examples

  const myObject = {
        prop: 1,
        complex: {
            sub: 'ABC',
            list: [1, 2, 3],
            subComplex: { type: 'noType', other: 21, },
        },
    };
     
maybe(myObject).valueOr((undefined as unknown) as typeof myObject); // => myObject
maybe(undefined as any).valueOr(myObject); // => myObject
maybe(myObject).mapTo('prop').valueOr(0); // => 1
maybe(myObject)
    .mapTo('complex')
    .map((it) => ((it.sub = 'DEF'), it))
    .mapTo('sub')
    .valueOr('null'); // => 'DEF'
maybe(myObject)
    .mapTo('complex')
    .mapTo('list')
    .map((it) => it.reduce((cur: number, next: number) => (cur += next), 0))
    .valueOr(0); // => 6
maybe(myObject)
    .mapTo('complex')
    .mapTo('subComplex')
    .mapTo('other')
    .nothingIf((it) => it === 21)
    .valueOr(0); // => 0

Look into test to see more examples and filters.

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago