1.1.17 • Published 3 years ago

immutable-replace v1.1.17

Weekly downloads
17
License
MIT
Repository
github
Last release
3 years ago

immutable-replace

Build Coverage Status npm version Package Size

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

import { replace } from 'immutable-replace'

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

Examples from unit tests:

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);
});
1.1.17

3 years ago

1.1.17-0

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.9

3 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago