# immutable-replace

> Returns a result with the matching objects replaced recursively

Latest version **1.1.17** (published 2020-12-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install immutable-replace
pnpm add immutable-replace
yarn add immutable-replace
bun add immutable-replace
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.17 |
| Published | 2020-12-24 |
| First published | 2018-04-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Stewart Anderson |
| Maintainers | stewie1570 |
| Keywords | immutable, merge, replace |

## Links

- npm: https://www.npmjs.com/package/immutable-replace
- Repository: https://github.com/stewie1570/immutable-replace
- Homepage: https://github.com/stewie1570/immutable-replace#readme
- Issues: https://github.com/stewie1570/immutable-replace/issues
- npm.io page: https://npm.io/package/immutable-replace

## Alternatives

- [@lexical/table](https://npm.io/package/@lexical/table.md) — 3.0M weekly downloads
- [mantine-datatable](https://npm.io/package/mantine-datatable.md) — 98.2K weekly downloads
- [react-native-collapsible-tab-view](https://npm.io/package/react-native-collapsible-tab-view.md) — 70.6K weekly downloads
- [@handsontable/vue3](https://npm.io/package/@handsontable/vue3.md) — 16.1K weekly downloads
- [vuewordcloud](https://npm.io/package/vuewordcloud.md) — 7.2K weekly downloads

## Recent versions

- 1.1.17 (latest) — 2020-12-24
- 1.1.17-0 — 2020-12-24
- 1.1.16 — 2020-11-24
- 1.1.15 — 2020-11-11
- 1.1.14 — 2020-11-11
- 1.1.9 — 2020-11-11
- 1.1.6 — 2020-07-24
- 1.1.5 — 2020-07-01
- 1.1.4 — 2020-06-19
- 1.1.3 — 2020-01-10
- 1.1.2 — 2019-10-19
- 1.1.1 — 2019-10-11
- 1.1.0 — 2019-10-01
- 1.0.2 — 2019-01-13
- 1.0.1 — 2018-04-20
- … 1 more at https://npm.io/package/immutable-replace/versions

## README

# immutable-replace

[![Build](https://github.com/stewie1570/immutable-replace/workflows/Build/badge.svg)](https://github.com/stewie1570/immutable-replace/actions)
[![Coverage Status](https://coveralls.io/repos/github/stewie1570/immutable-replace/badge.svg?branch=master)](https://coveralls.io/github/stewie1570/immutable-replace?branch=master)
[![npm version](https://badge.fury.io/js/immutable-replace.svg)](https://badge.fury.io/js/immutable-replace)
[![Package Size](https://badgen.net/bundlephobia/minzip/immutable-replace)](https://bundlephobia.com/result?p=immutable-replace)

If you don't want to nest or recurse usage of the spread operator to get an updated object then you may like this package.

**General Usage**
```javascript
import { replace } from 'immutable-replace'

const result = replace(old).with(updated).in(haystack);
```

**Examples from unit tests:**
```javascript
it("should return the new object when the haystack is the old object", () => {
    const old = { value: "old" };
    const updated = { value: "updated" };

    expect(replace(old).with(updated).in(old)).toBe(updated);
});

it("should find and replace the object in a complex object", () => {
    //Arrange
    const old = { value: "old" };
    const updated = { value: "updated" };
    const haystack = {
        untouched: { wrapper: "something" },
        targetWrapper: {
            target: old
        }
    };

    //Act
    const result = replace(old).with(updated).in(haystack);

    //Assert
    expect(result).toEqual({
        untouched: { wrapper: "something" },
        targetWrapper: {
            target: updated
        }
    });
    expect(result.targetWrapper.target).toBe(updated);
    expect(result.untouched).toBe(haystack.untouched);
});

it("should find and replace the object in an array in a complex object", () => {
    //Arrange
    const old = { value: "old" };
    const updated = { value: "updated" };
    const haystack = {
        untouched: [{ untouched: "something" }],
        target: [old]
    };

    //Act
    const result = replace(old).with(updated).in(haystack);

    //Assert
    expect(result).toEqual({
        untouched: [{ untouched: "something" }],
        target: [updated]
    });
    expect(result.untouched).toBe(haystack.untouched);
    expect(result.target[0]).toBe(updated);
});

it("should find and replace the object in a complex object in an array in another complex object", () => {
    //Arrange
    const old = { value: "old" };
    const updated = { value: "updated" };
    const haystack = {
        untouched: [{ untouched: "something" }],
        target: [{ targetWrapper: old }]
    };

    //Act
    const result = replace(old).with(updated).in(haystack);

    //Assert
    expect(result).toEqual({
        untouched: [{ untouched: "something" }],
        target: [{ targetWrapper: updated }]
    });
    expect(result.untouched[0]).toBe(haystack.untouched[0]);
    expect(result.target[0].targetWrapper).toBe(updated);
});

it("should find and replace multiple targets in a complex structure", () => {
    //Arrange
    const old = { value: "old" };
    const updated = { value: "updated" };
    const haystack = {
        untouched: [{ untouched: "something" }],
        target: [{ targetWrapper: old }],
        secondTarget: old
    };

    //Act
    const result = replace(old).with(updated).in(haystack);

    //Assert
    expect(result).toEqual({
        untouched: [{ untouched: "something" }],
        target: [{ targetWrapper: updated }],
        secondTarget: updated
    });
    expect(result.untouched[0]).toBe(haystack.untouched[0]);
    expect(result.target[0].targetWrapper).toBe(updated);
    expect(result.secondTarget).toBe(updated);
});
```

---
_Source: https://npm.io/package/immutable-replace · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
