Class: Optional

Optional

new Optional(lens, errorHandler) → {Lens}

Optional Lenses take Lenses and an optional error handler as an argument, and make any Lens accesses safe. For example, Using an unsafe IndexedLens, you may get an error if you try to access an element out of range:

new IndexedLens(100).get([]) // Uh oh!

Using Optional, you can wrap the IndexedLens to return null (or optionally handle exceptions in some other way), e.g:

var lens = new Option(new IndexedLens(100));

lens.get([]); // null
lens.get([], console.log); // prints 'Array index 100 out of range'
Parameters:
Name Type Description
lens Lens

The lens to make safe

errorHandler function | *

A function that is called on any thrown exceptions, or a default value to return on error.

Returns:

A safer lens

Type
Lens