Skip to content

Instantly share code, notes, and snippets.

@busypeoples
Last active March 23, 2018 00:44
Show Gist options
  • Save busypeoples/b216616b41996cc20cd702a84d8c73a2 to your computer and use it in GitHub Desktop.
Save busypeoples/b216616b41996cc20cd702a84d8c73a2 to your computer and use it in GitHub Desktop.
Useful Redux / Ramda functionalities
import { insert, remove, reject } from 'ramda';
// function _insertItem(array, action) {
// return [
// ...array.slice(0, action.index),
// action.item,
// ...array.slice(action.index)
// ];
// }
const insertItem = (array, action) => insert(action.index, action.item, array);
// function _removeItem(array, action) {
// return [...array.slice(0, action.index), ...array.slice(action.index + 1)];
// }
const removeItem = (array, action) => remove(action.index, 1, array);
const items = [
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 },
{ id: 5 },
{ id: 6 }
];
insertItem(items, { index: 3, item: { id: 7 } });
removeItem(items, { index: 3 });
import { adjust, findIndex, reject, eqProps, propEq } from 'ramda';
const items = [
{ id: 1, text: 'Clojure' },
{ id: 2, text: 'Haskell' },
{ id: 3, text: 'Elm' },
{ id: 4, text: 'JS' },
{ id: 5, text: 'ReasonML' },
{ id: 6, text: 'Java' }
];
const findIndexById = (id, state) => findIndex(propEq('id', id), state);
const updateItemById = (state, action) =>
adjust(
item => ({ ...item, text: action.text }),
findIndexById(action.id, state),
state
);
const removeItemById = (state, action) => reject(eqProps('id', action), state);
// removes the item { id: 5, text: 'ReasonML' } to {id: 5, text: "OCaml"}
updateItemById(items, { id: 5, text: 'OCaml' });
// removes the item { id: 5, text: 'ReasonML' }
removeItemById(items, { id: 5 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment