Skip to content

Instantly share code, notes, and snippets.

@gabrielhpugliese
Last active September 19, 2017 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielhpugliese/a7a85942735e980d4f8f1414c7e5272e to your computer and use it in GitHub Desktop.
Save gabrielhpugliese/a7a85942735e980d4f8f1414c7e5272e to your computer and use it in GitHub Desktop.
import {
compose,
findIndex,
cond,
} from 'lodash/fp';
const MY_LIST = [/* items here */];
const findMyItemIndex = findIndex(item => item === 'MY_ITEM');
const changeItemToSomething = () => // returns NEW object (immutable)
const doMagic = (item, list) => compose(
cond([
[(index) => index === -1, () => list],
[(index) => index > -1, (index) => changeItemToSomething(item, list)],
]),
findMyItemIndex(item),
)(list);
doMagic({a: 1}, MY_LIST);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment