Skip to content

Implement shim/polyfil for structuredClone() #558

Open
@VenelinBakalov

Description

@VenelinBakalov

Description

It is a common task that we need to deep clone an object and change it's values without affecting the original. It might be useful to implement a shim/polyfil for structuredClone() so that we have that function out of the box. This would lead to more consistency and simplicity across the different approaches of implementing similar solution (most of which actually do not deep clone the object and lead to issues and bugs later down the line).

  • the options.trasfer array implementation is nice to have but not a must

Alternatives

Additional Context

https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone

It can be something as simple as

        if (source === undefined || source === null) {
            throw new Error("Source value is mandatory.");
        }
        return JSON.parse(JSON.stringify(source));

Note that this has some restrictions, e.g. works mainly for simple objects

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions