Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtmilam/05f63ce660290fee2f07081635d7d4c8 to your computer and use it in GitHub Desktop.
Save kurtmilam/05f63ce660290fee2f07081635d7d4c8 to your computer and use it in GitHub Desktop.
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