optional-type

1.0.5 • Public • Published

Optional-type 🤷‍♂️

Maintainability Test Coverage Known Vulnerabilities CircleCI Greenkeeper badge

A Typescript class trying to be Java's Optional.

Resources

Why

  • Your application might need to add special meaning to null values.
  • Avoid repeating if(expr === null)
  • Avoid cannot read property prop of null

Examples

Some very good examples can be found here. The examples are written in Java, but the concept should get across regardless.

Sample code:

Optional.ofNullable(response)
        .map(response => response.data)
        .filter(data => !!data.length)
        .ifPresent(data => serialize(data))
        .orElse([]);

Now has support for pivoting on both undefined & null if you don't want to differentiate in JS, just expect it to work.

Optional.ofUndefinable(response);

Contributing

To get the project running locally:

  1. git clone
  2. npm install
  3. npm run test to run jest

Before committing, please recompile the docs using npm run docs.

Package Sidebar

Install

npm i optional-type

Weekly Downloads

1

Version

1.0.5

License

MIT

Unpacked Size

419 kB

Total Files

24

Last publish

Collaborators

  • kreatemore