JSPM

find-and-replace-immutable

0.0.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q28165F
  • License MIT

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

Package Exports

  • find-and-replace-immutable

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (find-and-replace-immutable) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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]);