Skip to content

amidevtech/optional.js

Repository files navigation

optional.js

Makes TypeScript/JavaScript code undefined/null safe!

Optional.js is a wrapper for undefined and null values in TypeScript/JavaScript which fixes problems with handling this values.

Reasons to use optional.js

  • Eliminate errors correlated with undefined / null handling in code.
  • Makes you aware of possible scenarios when value is undefined / null.
  • Built in methods for handling undefined / null to reduce required checks.
  • Makes code easier to read.

Change this:

const user: User = getUser(4);
if (user !== undefined) {
    console.log(user.name);
}

into this:

getUser(4).ifPresent(user => console.log(user.name))

Or this:

const users: User[] = getUsers();
const user: User = users.find(user => user.name == 'test');
if (user !== undefined) {
    console.log(user.name);
}

into this:

OptionalArray.ofArray(getAllUsers())
    .findOne(user => user.name == 'test')
    .ifPresent(user => console.log(user.name))

Usage

Library consist of two main class/helpers

Features

  • Contains known from Java Optional (Without stream()) and more.
  • Contains useful methods which reduce boilerplate code for array operations.
  • Written in TypeScript with type checking.
  • No external dependencies.
  • Small footprint.
  • 100% code coverage.

Installation

Download from npm or from GitHub

Changelog

Changelog

About

Optional for null and undefined safety

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published