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/dca3d87d2c88ff41949e2136cd783683 to your computer and use it in GitHub Desktop.
Save kurtmilam/dca3d87d2c88ff41949e2136cd783683 to your computer and use it in GitHub Desktop.
partial.lenses: query array by sub array object property
// REPL: https://calmm-js.github.io/partial.lenses/playground.html
// discussion: https://gitter.im/calmm-js/chat?at=58ecf9c6bdf4acc1124c504f
const list =
{
tasks: [ { id: 2
, state: 'todo'
, users: [ { id: 1 } ]
}
, { id: 2
, state: 'todo'
, users: [ { id: 1 }
, { id: 5 }
]
}
, { id: 4
, state: 'todo'
, users: [ { id: 2 } ]
}
]
}
const hasUserWithID = id =>
L.when( L.any( R.equals( id )
, [ 'users'
, L.elems
, 'id'
]
)
)
const tasksForUser = id =>
[ 'tasks'
, L.elems
, hasUserWithID( id )
]
// return an array of tasks having user.id === 1
L.collect( tasksForUser( 1 ), list )
// set task.state to 'audit' on all tasks having user.id === 1
L.set( [ tasksForUser( 1 ), 'state' ], 'audit', list )
// partial.lenses: // REPL: 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