Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/robertherber/find-and-replace-immutable

Exposes immutable findAndReplace and replaceAt functions on top of lodash/fp
https://github.com/robertherber/find-and-replace-immutable

immutable javascript node utility

Last synced: 2 months ago
JSON representation

Exposes immutable findAndReplace and replaceAt functions on top of lodash/fp

Awesome Lists containing this project

README

        

# find-and-replace-immutable

Exposes two simple but essential helper immutable methods:
* replaceAt (index, objectOrMapFunction, array)
* findAndReplace (findPredicateFunction, objectOrMapFunction, array)

Uses [lodash/fp](https://github.com/lodash/lodash/wiki/FP-Guide) under the hood, which is great for it's immutability.

I always end up using this in my React projects - so I thought I'd share it! ;)

## Examples

Using *replaceAt*:
~~~~
import { replaceAt } from 'find-and-replace-immutable';

const initialArray = ["original"];

const newArray = replaceAt(0, "new value", initialArray);

// expect(newArray).toEqual(["new value"]);
// expect(initialArray).toEqual(["original"]);
~~~~

Using *findAndReplace* with mapping function:
~~~~
import { findAndReplace } from 'find-and-replace-immutable';

const initialArray = [1, 2, 3, 3, 4, 5];

const newArray = findAndReplace(
e => e === 3,
oldValue => 33 * oldValue,
initialArray
);

// expect(newArray).toEqual([1, 2, 99, 3, 4, 5]);
~~~~