Comments (5)
I think that for the sake of loupe, the right step forward is to return Invalid Date
.
The specs unfortunately states that the behavior is both "may fall back to any implementation-specific heuristics or implementation-specific date formats" and "Strings that are unrecognizable shall cause Date.parse to return NaN".
This makes me think that we should return NaN
since the String is "unrecognizable" but my tests shows that both V8 and SpiderMonkey agrees on "implementation-specific heuristics" for a String that is clearly not a date. It gets mucky when I use a String that has a year component in it. E.g. foo-bar 2022
.
Below are my notes about the subject. I will add a simple PR which will return "Invalid Date"
with a link to this issue.
From the ECMAScript® 2022 Language Specification 21.4.2.1 Date ( ...values )
When the Date function is called, the following steps are taken:
- If NewTarget is undefined, then
- ...
- Let numberOfArgs be the number of elements in values.
- If numberOfArgs = 0, then
- ...
- Else if numberOfArgs = 1, then
- Let value be values[0].
- If Type(value) is Object and value has a [[DateValue]] internal slot, then
- ...
- Else,
- Let v be ? ToPrimitive(value).
- If Type(v) is String, then
1.Assert: The next step never returns an abrupt completion because Type(v) is String.- Let tv be the result of parsing v as a date, in exactly the same manner as for the parse method (21.4.3.2).
21.4.3.2:
If the String does not conform to that format (Date Time String Format) the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Strings that are unrecognizable or contain out-of-bounds format element values shall cause Date.parse
to return NaN
.
21.4.4.37 Date.prototype.toJSON ( key ):
Note 2
The
toJSON
function is intentionally generic; it does not require that its this value be a Date. Therefore, it can be transferred to other kinds of objects for use as a method. However, it does require that any such object have atoISOString
method.
JS engine behavior
const d = new Date('not a date');
Method | node 18 V8 | Firefox |
---|---|---|
valueOf | NaN |
NaN |
toString | Invalid Date | Invalid Date |
toUTCString | Invalid Date | Invalid Date |
toISOString | RangeError: invalid date |
RangeError: invalid date |
toJSON | null |
null |
const d = new Date('foo-bar 2022');
Method | node 18 V8 | Firefox |
---|---|---|
valueOf | 1640991600000 |
NaN |
toString | Sat Jan 01 2022 00:00:00 GMT+0100 (Central European Standard Time) | Invalid Date |
toUTCString | Fri, 31 Dec 2021 23:00:00 GMT | Invalid Date |
toISOString | 2021-12-31T23:00:00.000Z | Invalid Date |
toJSON | 2021-12-31T23:00:00.000Z | null |
Test code:
const test = f => {
let r;
try {
r = f.call(d);
} catch (err) {
r = err;
}
return r;
}
[
Date.prototype.valueOf,
Date.prototype.toString,
Date.prototype.toUTCString,
Date.prototype.toISOString,
Date.prototype.toJSON,
].map(test)
.forEach(m => { console.debug(m); });
from loupe.
I would be happy to provide a patch if I know what the return value should be for invalid dates.
I am not a direct consumer of Loupe but rather of Vitest. So for me it would make sense to return NaN
, which is a global variable but that is inconsistent with the description of Loupe.
Loupe turns the object you give it into a string.
A better place to fix this might be in chai?
from loupe.
Ping @keithamus
from loupe.
Thanks for the issue @dotnetCarpenter. I trust your judgement and would be happy to merge a PR. FWIW nodejs, Chrome devtools, and Firefox devtools all render this as Invalid Date
.
from loupe.
Fixed in v2.3.5 🎉
from loupe.
Related Issues (20)
- TODO for v2 Release HOT 4
- Time representation difference in Chai.js HOT 6
- Loupe doesn't work correctly for buffers HOT 2
- Return value for number 0 HOT 6
- `inspect( 1n )` => no joy HOT 2
- broken dependency `get-function-name` HOT 2
- Handle circular references HOT 1
- Quote complex keys in objects HOT 1
- The automated release is failing 🚨 HOT 1
- IE9+ not supported since 4.3.5 because of loupe HOT 4
- inspect of Object with null prototype causes primitive value error
- add an option to render functions as their body HOT 5
- Package `loupe` seems to be an ES Module but shipped in a CommonJS package HOT 1
- `depth` option HOT 1
- vulnerability in loupe HOT 1
- New release for Dependabot Fix? HOT 1
- prevent `playwright` installation HOT 2
- Is the custom `inspect()` still needed?
- Handle circular references better HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loupe.