Skip to content
Nullable. Optional. The Maybe monad. Whatever you want to call it, it's for Javascript now.
JavaScript TypeScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
dist
src
test
.gitignore
.travis.yml
LICENSE
README.md
bower.json v0.3.1
gulpfile.js
package.json
tsconfig.json

README.md

Optional.js

Build Status

Nullable. Optional. The Maybe monad. Whatever you want to call it, it's for Javascript now.

Based specifically on java.util.Optional.

How to get it

Grab dist/optional.js or dist/optional.min.js, whichever you prefer. They're UMD modules, so you should be able to use them in the loader/environment of your choice.

If you're a Typescript dev, high-five for typesafety! You'll probably be interested in dist/optional.d.ts, which is specified in the typings property of package.json -- so you should just be able to import Optional from "optional.js"

How to build it

Optional.js uses gulp as its build tool, so you'll want to install that. Otherwise, it's just npm install && npm test.

How to use it

Given the non-typechecked nature of Javascript, Optional.js can come in handy for safely getting-or-defaulting nested properties:

function someFunctionThatTakesAParamObject(params){
  var color = Optional.ofNullable(params)
                      .map(function(params){ return params.color; })
                      .orElse("blue");
}

It's also handy for functions where you might not get a result back:

var submitButton = Optional.ofNullable(document.querySelector('btn#submit'));
submitButton.ifPresent(function() { submitButton.click()});

Really, any use of the Java Optional is applicable here, as Optional.js is a matching port of the JDK8 implementation of this concept.

For more in-depth examples, check the spec file.

Something went wrong with that request. Please try again.