radash icon indicating copy to clipboard operation
radash copied to clipboard

why not provide deep copy method,be like deep copy of array or object?

Open yyqxjwxy opened this issue 2 years ago • 8 comments

yyqxjwxy avatar Mar 13 '24 02:03 yyqxjwxy

Deep cloning objects and arrays can indeed get quite complex, especially when you consider all the potential edge cases like nested structures and special types (like Date, RegExp). The aim of Radash is to keep things simple and efficient, focusing on the most common use cases.

Adding a deep copy feature would significantly increase the complexity and size of the library. That’s why it’s left out, allowing users to integrate or implement their own solutions for specific needs. There are dedicated libraries out there that handle deep cloning well, which might be worth looking into for more complex requirements.

I hope this makes sense and aligns with your expectations for using Radash.

shtse8 avatar Mar 14 '24 12:03 shtse8

Now the packaging tools all have tree shaking functions, if the developer does not reference the deep copy function, it will not be packaged into the final bundle, so there is no need to consider the package size.

qingzhoufeihu avatar Mar 19 '24 02:03 qingzhoufeihu

Maybe structuredClone ?

troy351 avatar Mar 19 '24 05:03 troy351

We could use immer instead

qingzhoufeihu avatar Mar 19 '24 09:03 qingzhoufeihu

Now the packaging tools all have tree shaking functions, if the developer does not reference the deep copy function, it will not be packaged into the final bundle, so there is no need to consider the package size.

日常需求感觉JSON.parse JSON.stringify就够了

Gemini-0529 avatar Apr 08 '24 05:04 Gemini-0529

Now the packaging tools all have tree shaking functions, if the developer does not reference the deep copy function, it will not be packaged into the final bundle, so there is no need to consider the package size.

日常需求感觉JSON.parse JSON.stringify就够了

https://dev.to/builderio/deep-cloning-objects-in-javascript-the-modern-way-17kf

json parse has some problems as well...

Extracted from the article:

Take this as an example:

const calendarEvent = {
  title: "Builder.io Conf",
  date: new Date(123),
  attendees: ["Steve"]
}
// 🚩 JSON.stringify converted the `date` to a string
const problematicCopy = JSON.parse(JSON.stringify(calendarEvent))

If we log problematicCopy, we would get:

{
  title: "Builder.io Conf",
  date: "1970-01-01T00:00:00.123Z"
  attendees: ["Steve"]
}

That’s not what we wanted! date is supposed to be a Date object, not a string.

This happened because JSON.stringify can only handle basic objects, arrays, and primitives. Any other type can be handled in hard to predict ways. For instance, Dates are converted to a string. But a Set is simply converted to {}.

JSON.stringify even completely ignores certain things, like undefined or functions.

For instance, if we copied our kitchenSink example with this method:

const kitchenSink = {
  set: new Set([1, 3, 3]),
  map: new Map([[1, 2]]),
  regex: /foo/,
  deep: { array: [ new File(someBlobData, 'file.txt') ] },
  error: new Error('Hello!')
}
const veryProblematicCopy = JSON.parse(JSON.stringify(kitchenSink))

We would get:

{
  "set": {},
  "map": {},
  "regex": {},
  "deep": {
    "array": [
      {}
    ]
  },
  "error": {},
}

Ew!

Oh yeah, and we had to remove the circular reference we originally had for this, as JSON.stringify simply throws errors if it encounters one of those.

CavalcanteLeo avatar Jun 23 '24 05:06 CavalcanteLeo

Hello @yyqxjwxy @qingzhoufeihu @CavalcanteLeo and others, we've added a cloneDeep function over at the Radashi fork. Read the docs here. If you have any feedback, comment on this PR.

You can use it today by installing radashi@beta. An official release is pending until I get the docs website running. You can check out the CHANGELOG.md to see everything else we've added/fixed.

aleclarson avatar Jul 12 '24 18:07 aleclarson

what a pity, if there's no deep clone function.

skylingfly avatar Aug 20 '24 06:08 skylingfly