Last active
October 2, 2020 06:51
-
-
Save bezenson/982995d53c156f4d39d84d66d05dcc81 to your computer and use it in GitHub Desktop.
Toggle item in array with ramda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { append, contains, curry, ifElse, without } from 'ramda'; | |
const toggleListItem = curry((value, list) => ifElse( | |
contains(value), | |
without([value]), | |
append(value), | |
)(list)); | |
// Example: | |
const data = ['a', 'b', 'c', 'd']; | |
toggleListItem('b', data); | |
// or | |
toggleListItem('b')(data); | |
// will return ['a', 'c', 'd'] |
@rdbmax Sure. Just mistake :) Thank you
Doesn't work for list of numbers
@marcoturi
Try replace without(value)
to without([value]),
. My fault. This function accept array of removing values
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you do not need
pipe
here :Demo in Ramda REPL :
https://ramdajs.com/repl/?v=0.26.1#?const%20toggleListItem%20%3D%20R.curry%28%28value%2C%20list%29%20%3D%3E%20R.ifElse%28%0A%20%20R.contains%28value%29%2C%0A%20%20R.without%28value%29%2C%0A%20%20R.append%28value%29%2C%0A%29%28list%29%29%3B%0A%0A%2F%2F%20Example%3A%0Aconst%20data%20%3D%20%5B%27a%27%2C%20%27b%27%2C%20%27c%27%2C%20%27d%27%5D%3B%0A%0AtoggleListItem%28%27d%27%29%28data%29%3B