Skip to content

fix(pinia-orm): Improve Date serialization in Model class#1121

Merged
CodeDredd merged 1 commit intoCodeDredd:masterfrom
sergerdn:feat/model-date-serialization-improvements
Mar 27, 2023
Merged

fix(pinia-orm): Improve Date serialization in Model class#1121
CodeDredd merged 1 commit intoCodeDredd:masterfrom
sergerdn:feat/model-date-serialization-improvements

Conversation

@sergerdn
Copy link
Copy Markdown
Contributor

@sergerdn sergerdn commented Mar 25, 2023

The new check ensures that the value is an instance of Date in the serializeValue function of Model.ts.

Adding a condition like if ('toISOString' in value') to check for the existence of a method on an object can lead to issues in JavaScript because it does not accurately check whether the method actually exists and can be called. This is because objects in JavaScript can have inherited properties, and the in operator also checks for properties in the object's prototype chain. As a result, using the in operator to check for the existence of a method on an object can sometimes result in unexpected behaviour.

In my personal experience, I had a big headache with the in operator in Python, and since then, I have never used in in any language with similar behaviour. Because it affected performance much and resulted in unexpected outcomes.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in#Caveats :

The in operator returns true if the specified property is in the specified object or its prototype chain.

A few examples:

const myObj = {
  toISOString: function () {
    return 'some value';
  }
};

// This will return true, even though myObj is not a Date object
console.log('toISOString' in myObj);

// This will return false because myObj is not a valid Date object
console.log(myObj instanceof Date && !isNaN(myObj.getTime()) && typeof myObj.toISOString === 'function');
class MyObject {
  toISOString() {
    return 'some value';
  }
}

class MyChildObject extends MyObject {
  
}

const myObj = new MyChildObject();

// This will return true, because myObj's prototype chain includes the toISOString method
console.log('toISOString' in myObj);

// This will return false because myObj is not a valid Date object
console.log(myObj instanceof Date && !isNaN(myObj.getTime()) && typeof myObj.toISOString === 'function');

🔗 Linked issue

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • [ x] 🐞 Bug fix (a non-breaking change that fixes an issue)
  • [ x] 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

The new check ensures that the value is an instance of Date, that its getTime() method returns a valid number, and that its toISOString() method returns a valid ISO 8601 string.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

The new check ensures that the value is an instance of Date
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (99fc915) 99.64% compared to head (b8a90e4) 99.64%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1121   +/-   ##
=======================================
  Coverage   99.64%   99.64%           
=======================================
  Files          88       88           
  Lines        5875     5878    +3     
  Branches      536      537    +1     
=======================================
+ Hits         5854     5857    +3     
  Misses         19       19           
  Partials        2        2           
Impacted Files Coverage Δ
packages/pinia-orm/src/model/Model.ts 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@CodeDredd
Copy link
Copy Markdown
Owner

Thanks for this PR @sergerdn. 🥇 . That's a good notice.

@CodeDredd CodeDredd added the enhancement New feature or request label Mar 25, 2023
@CodeDredd CodeDredd changed the title feat(Model): improve Date serialization in Model class refactor(Model): improve Date serialization in Model class Mar 27, 2023
@CodeDredd CodeDredd changed the title refactor(Model): improve Date serialization in Model class fix(Model): improve Date serialization in Model class Mar 27, 2023
@CodeDredd CodeDredd changed the title fix(Model): improve Date serialization in Model class fix(pinia-orm): Improve Date serialization in Model class Mar 27, 2023
@CodeDredd CodeDredd merged commit 84aeb72 into CodeDredd:master Mar 27, 2023
@sergerdn sergerdn deleted the feat/model-date-serialization-improvements branch March 28, 2023 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants