pvorb / clone

deeply clone arbitrary objects in javascript
https://www.npmjs.com/package/clone
MIT License
781 stars 129 forks source link

Use URL when available natively #110

Open iambalaam opened 4 years ago

iambalaam commented 4 years ago

This is my first attempt to solve Cannot clone a URL in Node >= 10 https://github.com/pvorb/clone/issues/109 .


I have had to clone the property descriptors of Symbols, and I assume I should do the same for property names.

When running the following code I found that the value wasn't a primitive. This made the URL object clone to a new object, but the URLSearchParams inside it point to the same instance. (I have not assessed the performance implications of this.)

> const url = new URL('https://example.com/?a=1');
> const querySymbol = Object.getOwnPropertySymbols(url)[1] // Symbol(query)
> Object.getOwnPropertyDescriptor(url, querySymbol)

{ value: URLSearchParams { 'a' => '1' },
  writable: true,
  enumerable: true,
  configurable: true }
iambalaam commented 4 years ago

Since raising this PR, I have questioned if both parts of the solution are needed (nativeURL and cloned property descriptor/names). I have now tested and know that cloning the property descriptor/names is enough on its own.

I am now unsure if there are any merits of adding the nativeURL, or if I should remove that part?