@hansogj/maybe
TypeScript icon, indicating that this package has built-in type declarations

2.2.23 • Public • Published

example workflow

maybe

A lightweight stream-like maybe/orElse lib for optional values. 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

Versioning and publishing

We strive to use a semantic version regime on this lib. From a clean git status do

> npm version [ major | minor | patch | premajor | preminor | prepatch | prerelease ]
> git push && git push --follow-tags

and then publish a new version

npm publish

Package Sidebar

Install

npm i @hansogj/maybe

Weekly Downloads

0

Version

2.2.23

License

ISC

Unpacked Size

16.3 kB

Total Files

6

Last publish

Collaborators

  • hansogj