feat(isDate, isRegExp): support cross-realm#1568
feat(isDate, isRegExp): support cross-realm#1568haejunejung wants to merge 4 commits intotoss:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR adds cross-realm support to isDate and isRegExp predicates by replacing instanceof checks with Object.prototype.toString.call(), which works reliably across different JavaScript realms (e.g., iframes, VM contexts).
Key Changes:
- Replaced
instanceofchecks withObject.prototype.toString.call()for realm-independent type checking - Added
isObjectguard for performance optimization before the toString check - Added cross-realm tests using
node:vmmodule
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/predicate/isRegExp.ts |
Updated implementation to use Object.prototype.toString.call() instead of instanceof RegExp |
src/predicate/isRegExp.spec.ts |
Added cross-realm test using runInNewContext |
src/predicate/isDate.ts |
Updated implementation to use Object.prototype.toString.call() instead of instanceof Date |
src/predicate/isDate.spec.ts |
Added cross-realm test using runInNewContext |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1568 +/- ##
=======================================
Coverage 99.97% 99.97%
=======================================
Files 495 495
Lines 4648 4648
Branches 1340 1342 +2
=======================================
Hits 4647 4647
Misses 1 1 🚀 New features to boost your workflow:
|
|
Hey, thanks for the PR! We're not sure cross-realm support is a common enough use case for us to prioritize. es-toolkit aims to keep implementations simple and cover ~85% of real-world scenarios—edge cases aren't really our focus (more on that in our contributing guide). For checking RegExp or Date values, we think Appreciate the contribution though—hope to see more from you! |
Support cross-realm
Problem
instanceoffails when checking objects created in different JavaScript realms (e.g., iframes,vmcontexts). Each realm has its own set of built-in constructors, sovalue instanceof RegExpreturnsfalsefor a RegExp created in another realm.Solution
Replace
instanceofwithObject.prototype.toString.call(), which checks the object's internal[[Class]]slot and works reliably across realms.Changes
Object.prototype.toString.call()instead ofinstanceofnode:vmReference