iamkun / iamkun/dayjs

Reinitialisiation after structuredClone

Open
#2,236 2 comments 3 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

TLDR:
**Describe the bug**
I want to copy an object storing dayJS objects as properties. In order to get a full copy I use structuredClone()

**Expected behavior**
Retrieve an fully functional Object dayJS after re-initialization.

**Information**
- Day.js Version Current
- OS: Browser Firefox Current and NodeJS

**Working Example**
[https://jsfiddle.net/uqm8ts5h/6/](https://jsfiddle.net/uqm8ts5h/6/)

Long Story:
I am a "newbee" to JS and I am fiddeling around with copying an object in order to not do the whole reinitialisation work.
I know I could stringify() and parse(), but then I found the "new" possibility of structuredClone().
So I cloned and reinitialised the object. Sadly dayJS loses it's date. In order to proof it, I made the working example above.
Like in my code all values become NaN after reinitialisation. Please see the Output in the console in the working example

```JS
class anClass {

constructor(json) {
this.someDate=dayjs(json.someDate)
this.someString=json.someString
}
}

testobject=new anClass({someDate:dayjs('2019-01-25'), somestring: "hello"})
console.log("test1", testobject.someDate)
console.log("test1", testobject.someDate.toString())

testobject2=new anClass( structuredClone(testobject) )

console.log("test2",dayjs.isDayjs(testobject2.someDate))
console.log("test2", testobject2.someDate)
console.log("test2", testobject2.someDate.toString())

```

```
"test1", {
$D: 25,
$d: [object Date] { ... },
$g: function(t,e,n){return O.u(t)?this[e]:this.set(n,t)},
$H: 0,
$L: "en",
$locale: function(){return D[this.$L]},
$m: 0,
$M: 0,
$ms: 0,
$s: 0,
[...]
}
"test1", "Thu, 24 Jan 2019 23:00:00 GMT"

"test2", true // <- dayjs.isDayjs()
"test2", {
$D: NaN,
$d: [object Date] { ... },
$g: function(t,e,n){return O.u(t)?this[e]:this.set(n,t)},
$H: NaN,
$L: "en",
$locale: function(){return D[this.$L]},
$m: NaN,
$M: NaN,
$ms: NaN,
$s: NaN,
[...]

```

Checking `JS console.log( structuredClone(testobject) )` seems to return a stripped dayJS-Object. Maybe there is a possibility to tell dayJS to return some Time-String that can than be reinitialised with dayJS() ?

Hope it is just my lack of knowledge.
Best

Contributor guide

Open the contributing guide

Research direction

Start with the linked JSFiddle and the provided structuredClone(testobject) output, then compare the original and cloned Day.js values in both Firefox and Node.js. Determine whether the reported NaN values are reproducible and whether the issue calls for a supported behavior or a documented limitation; done means the behavior and expected handling are clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.