Class: PathLens

PathLens

new PathLens(path, unsafe) → {Lens}

A PathLens is a Lens that focuses on some element following a path in a JS object. They are safe by default and will not throw errors when trying to get or set elements that don't exist. In regular javascript, for example:

{}.a.b; // Throws 'cannot read property b of undefined'

But with a PathLens:

new PathLens('a.b').get(); // null

You can also set values that don't exist yet:

new PathLens('a.b').set({}, 100); // {a : { b : 100 } }

Unsafe PathLenses don't catch these errors and will throw the usual error messages. These can be constructed using either the PathLens.Unsafe constructor or by setting the unsafe parameter in the PathLens constructor to true.

Parameters:
Name Type Description
path string | Array

Array or dot-delimited string describing a path to follow in an object

unsafe boolean

If true, construct an unsafe version of

Returns:
Type
Lens

Classes

Unsafe

Methods

<static> deriveLenses(obj)

deriveLenses derives PathLenses for every existent path in the object passed in. For example, if you have the following structure:

var obj = { a: { b : { c : 8, d: 9 } } }

PathLens.deriveLenses(obj) gives back an object with the following keys:

'a', 'a.b', 'a.b.c', and 'a.b.d'.

Each of these keys point to a safe PathLens for that path.

Parameters:
Name Type Description
obj object

The object to derive lenses from