optional.js
TypeScript icon, indicating that this package has built-in type declarations

0.4.3 • Public • Published

Optional.js

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

In the browser, grab dist/umd/optional.js or dist/umd/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.

Elsewhere, use dist/commonjs/optional.js. It's commonjs, and has typings specified, so if you're a Typescript dev, high-five for typesafety! You'll probably be interested in dist/commonjs/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 relies on some npm scripts. So just run npm install && npm test, and you're good!

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.

Readme

Keywords

Package Sidebar

Install

npm i optional.js

Weekly Downloads

0

Version

0.4.3

License

MIT

Unpacked Size

46.3 kB

Total Files

23

Last publish

Collaborators

  • spencerwi