Package Exports
- optional.js
- optional.js/dist/commonjs/optional.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (optional.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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.