Skip to content

Instantly share code, notes, and snippets.

@dra1n
Created March 16, 2020 11:08
Show Gist options
  • Select an option

  • Save dra1n/c73b300636f74ae883e1d50c687e3ec8 to your computer and use it in GitHub Desktop.

Select an option

Save dra1n/c73b300636f74ae883e1d50c687e3ec8 to your computer and use it in GitHub Desktop.
// https://jsbin.com/lagobuzoxu/1/edit?html,js,output
let state = []
const handleToggle = value => {
state = state.includes(value)
? R.without([value], state)
: [...state, value]
}
const getToggleSet = R.symmetricDifference
const reverseStateTo = oldState => () => {
getToggleSet(state, oldState).forEach(value => (
handleToggle(value)
))
}
QUnit.module('reverseStateTo')
QUnit.test('it reverts to initial when more items were added', assert => {
state = ['a', 'b', 'c']
reverseStateTo(['a', 'b'])()
assert.deepEqual(state, ['a', 'b'])
})
QUnit.test('it reverts to initial when items were remvoved', assert => {
state = ['a', 'b']
reverseStateTo(['a', 'b', 'c'])()
assert.deepEqual(state, ['a', 'b', 'c'])
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment