immutable-find-replace
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

View on NPM Build Status Dependencies Status

immutable-find-replace

This library was inspired by and based on find-replace, does not mutate an initial array and built on top of TypeScript

Usage

If the replacements value is a plain value, it will be used found values will be replaced by it:

import { immutableFindReplace } from 'immutable-find-replace';

const initialArray = [1, 2, 3, 4, 5];
const result = immutableFindReplace(initialArray, x => x === 5, 10);

console.log(result);
// [1, 2, 3, 4, 10]

If the replacements value is a function, it will be invoked with the found item and its result used as the replace value:

import { immutableFindReplace } from 'immutable-find-replace';

const initialArray = [1, 2, 3, 4, 5];
const result = immutableFindReplace(initialArray, x => x === 5, x => x * 20);

console.log(result);
// [1, 2, 3, 4, 100]

If the replacements isn't provided then found values will be deleted from the initial array:

import { immutableFindReplace } from 'immutable-find-replace';

const initialArray = [1, 2, 3, 4, 5];
const result = immutableFindReplace(initialArray, x => x > 1 && x < 5);

console.log(result);
// [1, 5]

If the initialArray does not contain required value, nothing will be changed:

import { immutableFindReplace } from 'immutable-find-replace';

const initialArray = [1, 2, 3, 4, 5];
const result = immutableFindReplace(initialArray, x => x === 6, 10);

console.log(result);
// [1, 2, 3, 4, 5]

Package Sidebar

Install

npm i immutable-find-replace

Weekly Downloads

8

Version

1.0.3

License

MIT

Unpacked Size

10.5 kB

Total Files

14

Last publish

Collaborators

  • zamaletto