Skip to content

Instantly share code, notes, and snippets.

@bezenson
Last active October 2, 2020 06:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bezenson/982995d53c156f4d39d84d66d05dcc81 to your computer and use it in GitHub Desktop.
Save bezenson/982995d53c156f4d39d84d66d05dcc81 to your computer and use it in GitHub Desktop.
Toggle item in array with ramda
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']
@bezenson
Copy link
Author

bezenson commented Aug 8, 2019

@rdbmax Sure. Just mistake :) Thank you

@marcoturi
Copy link

Doesn't work for list of numbers

@bezenson
Copy link
Author

@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