optic

Requires

  • module:lambda/curry
  • module:operation/map

Methods

(static) createLens(getter, setter) → {function}

The createLens function acts like a factory for new types of lenses

Version:
  • 3.0.0
Source:
Parameters:
Name Type Description
getter function

A function defining how to get a value from the structure

setter function

A function defining how to clone the structure and set a value

Returns:
Type:
function

A factory function which can be used to create lens types

Example
const {createLens, over} = require('futils').optic;

const MapLens = createLens(
    (key, map) => map.get(key),
    (key, val, map) => new Map([...map.entries()]).set(key, val)
);

const LName = MapLens('name');

const data = new Map([['name', 'Nice blue'], ['code', '#3d73cc']]);

const upper = (a) => a.toUpperCase();
over(LName, upper, data); // -> Map([['name', 'NICE BLUE'], ['code', '#3d73cc']])

(static) lenses(…a) → {Object}

The lenses function, which allows to create a collection of lenses from strings. Also creates a "index" lens which can be used with arrays

Version:
  • 3.0.0
Source:
Parameters:
Name Type Attributes Description
a String <repeatable>

Strings representing the properties of an object

Returns:
Type:
Object

Collection of lenses

Example
const {lenses, over} = require('futils').optic;

const L = lenses('name');

const color = {color: '#3d73cc', name: 'Nice blue'};
const upper = (a) => a.toUpperCase();

over(L.name, upper, color); // -> {color: '#3d73cc', name: 'NICE BLUE'}

(static) mapped(f, a) → {LensVal}

The mapped function, which is a special lens for arrays.

Version:
  • 3.0.0
Source:
Parameters:
Name Type Description
f function

Data transformation function

a Array

The nested data structure

Returns:
Type:
LensVal

A LensVal

Example
const {mapped, over} = require('futils').optic;

const even = (n) => n % 2 === 0;

over(mapped, even, [1, 2, 3]); // -> [false, true, false]

(static) over(l, f, a) → {Object|Array}

The over function. Over allows to apply a function over a focused value of a data structure and return the whole structure

Version:
  • 3.0.0
Source:
Parameters:
Name Type Description
l Lens

A lens to focus with

f function

A tranformation function

a Object | Array

The structure to operate on

Returns:
Type:
Object | Array

A manipulated clone of the structure

Example
const {lenses, over} = require('futils').optic;

const L = lenses('name');

const color = {code: '#3d73cc', name: 'Nice blue'};
const upper = (a) => a.toUpperCase();

over(L.name, upper, color); // -> {code: '#3d73cc', name: 'NICE BLUE'}

(static) set(l, v, a) → {Object|Array}

The set function, which allows to set a property to a value on a data structure and return the structure afterwards

Version:
  • 3.0.0
Source:
Parameters:
Name Type Description
l Lens

A lens to focus with

v any

The value to set

a Object | Array

The structure to operate on

Returns:
Type:
Object | Array

A manipulated clone of the structure

Example
const {lenses, set} = require('futils').optic;

const L = lenses('name');

const color = {code: '#3d73cc'};

set(L.name, 'Nice blue', color); // -> {code: '#3d73cc', name: 'Nice blue'}

(static) view(l, a) → {any|null}

The view function. It allows to view a focused property of a data structure and return the value of it. If the property does not exist, null is returned

Version:
  • 3.0.0
Source:
Parameters:
Name Type Description
l Lens

A lens to focus with

a Object | Array

The structure to operate on

Returns:
Type:
any | null

The focused value or null

Example
const {lenses, view} = require('futils').optic;

const L = lenses('name');

const color = {code: '#3d73cc', name: 'Nice blue'};

view(L.name, color); // -> 'Nice blue'