Skip to content

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

Notifications You must be signed in to change notification settings

robertherber/find-and-replace-immutable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

find-and-replace-immutable

Exposes two simple but essential helper immutable methods:

  • replaceAt (index, objectOrMapFunction, array)
  • findAndReplace (findPredicateFunction, objectOrMapFunction, array)

Uses lodash/fp 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]);

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published