@amidevtech/optional.js

Provide Optional functionality for null/undefined safety!

Usage no npm install needed!

<script type="module">
  import amidevtechOptionalJs from 'https://cdn.skypack.dev/@amidevtech/optional.js';
</script>

README

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))

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

Usage

Library consist of two main class/helpers