Skip to content

Instantly share code, notes, and snippets.

@gtkatakura-bysoft
Last active February 6, 2019 12:38
Show Gist options
  • Save gtkatakura-bysoft/53b8080f3eef7820bc6d3dc0d47df88c to your computer and use it in GitHub Desktop.
Save gtkatakura-bysoft/53b8080f3eef7820bc6d3dc0d47df88c to your computer and use it in GitHub Desktop.
const R = require('ramda')
const undoable = (shape) => {
const operation = actionSelection => R.evolve(
R.map(R.cond([
[R.is(Function), R.identity],
[R.is(Array), actionSelection],
[R.is(Object), R.pipe(undoable, actionSelection)],
[R.T, R.identity],
]), shape)
)
return [
operation(R.head), // perform
operation(R.last), // undo
]
}
const oldItem = {a: 1}
const itemModified = {b: 2}
const itemId = 1
const [add, remove] = undoable({
collections: {
payload: [R.prepend(oldItem), R.reject(R.eqProps('id', itemModified))],
count: [R.inc, R.dec],
dependencies: R.without([itemId]),
},
})
const [add1, remove1] = undoable({
collections: {
payload: [R.prepend(itemModified), R.reject(R.eqProps('id', oldItem))],
count: [R.inc, R.dec],
dependencies: R.without([itemId]),
},
})
console.log(
add({
collections: {
payload: [itemModified],
count: 0,
dependencies: [2, 3],
},
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment