lenses

Methods

(inner) createSelector(path) → {function}

Create a memoized function that finds and returns specified property of an object. Uses ramda lensPath, and view internally

Pass a single string propName for top level key, or an array of propNames for deep nested keys

Parameters:
Name Type Description
path String | Array.<String>

an array for deep prop, or string for top level

Source:
See:
  • tests

Returns:

function that returns the value of a property at the specified path

Type
function
Example
const getA = createSelector('a')
const simpleObj = { a: 'rumble in the bronx', b: 'I like turtles' }
getA(simpleObj) //=> 'rumble in the bronx'

const getC = createSelector(['a', 'b', 'c'])
const nestedObj = { a: { b: { c: 'rumble in the bronx' } } }
getC(nestedObj) //=> 'rumble in the bronx'

(inner) createSetter(path) → {function}

Create a memoized function that returns a shallow copy of a given object, plus an altered value according the property it is focused to. Uses ramda lensPath, and set internally

Pass a single string propName for top level key, or an array of propNames for deep nested keys

Parameters:
Name Type Description
path String | Array.<String>

an array of strings for a deep property, or string for top level property

Source:
See:
  • tests

Returns:

function that returns a clone of an object with a new value set to the property at the specified path

Type
function
Example
const newVal = 'it is party time'

const setA = createSetter('a')
const obj = { a: 'I am bored', b: 'I like turtles' }
setA(newVal, obj) //=> { a: 'it is party time', b: 'I like turtles' }

const setC = createSetter(['a', 'b', 'c'])
const obj = { a: { b: { c: 'I am bored' } } }
setC(newVal, obj) //=> { a: { b: { c: 'it is party time' } } }

(inner) getLens(path) → {function}

Wraps ramda's lensProp and lensPath to return a lens that focuses on a top level property if passed a string, and a deep property if passed an array of strings for propPath

Parameters:
Name Type Description
path String | Array.<String>

an array for deep prop, or string for top level

Source:
See:
  • tests

Returns:
Type
function
Example
import { view, set } from 'ramda'

const aLens = getLens('a')
const simpleObj = { a: 'rumble in the bronx', b: 'I like turtles' }
view(aLens, simpleObj) //=> 'rumble in the bronx'

const cLens = getLens(['a', 'b', 'c'])
const nestedObj = { a: { b: { c: 'I am bored' } } }
set(cLens, 'it is party time', nestedObj)
//=> { a: { b: { c: 'it is party time' } } }