Last active
March 23, 2018 00:44
-
-
Save busypeoples/b216616b41996cc20cd702a84d8c73a2 to your computer and use it in GitHub Desktop.
Useful Redux / Ramda functionalities
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 { 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 }); |
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 { 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