Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
partial.lenses - get/set object in list by uuid
// REPL: https://calmm-js.github.io/partial.lenses/playground.html
// setup
const list = [ { uuid: 1, v: 1 }, { uuid: 2, v: 2 } ]
// lenses
const byPropNameL = R.curry( propName => R.compose( L.find, R.propEq( propName ) ) )
const byUuidL = byPropNameL( 'uuid' )
// getters & setters
const getByUuid = R.curry( ( uuid, o ) => L.get( byUuidL( uuid ), o ) )
const setByUuid = R.curry( ( uuid, val, o ) => L.set( byUuidL( uuid ), val, o ) )
const setPropByUuid = R.curry( ( prop, uuid, val, o ) => L.set( [ byUuidL( uuid ), prop ], val, o ) )
const setVByUuid = setPropByUuid( 'v' )
// usage
getByUuid( 2, list )
setByUuid( 2, { uuid: 4, v: 4 }, list )
setPropByUuid( 'v', 2, 4, list )
setVByUuid( 2, 4, list )
// partial.lenses: https://calmm-js.github.io/partial.lenses
// calmm-js: https://github.com/calmm-js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.