@@ -195,6 +195,8 @@ const {
195195 kValidateObjectAllowArray,
196196} = require ( 'internal/validators' ) ;
197197
198+ const temporal = require ( 'internal/util/temporal' ) ;
199+
198200let hexSlice ;
199201let internalUrl ;
200202
@@ -208,40 +210,6 @@ function isURL(value) {
208210 return typeof value . href === 'string' && value instanceof internalUrl . URL ;
209211}
210212
211- const temporalConstructors = [
212- { name : 'Duration' , fullName : 'Temporal.Duration' } ,
213- { name : 'Instant' , fullName : 'Temporal.Instant' } ,
214- { name : 'PlainDate' , fullName : 'Temporal.PlainDate' } ,
215- { name : 'PlainDateTime' , fullName : 'Temporal.PlainDateTime' } ,
216- { name : 'PlainMonthDay' , fullName : 'Temporal.PlainMonthDay' } ,
217- { name : 'PlainTime' , fullName : 'Temporal.PlainTime' } ,
218- { name : 'PlainYearMonth' , fullName : 'Temporal.PlainYearMonth' } ,
219- { name : 'ZonedDateTime' , fullName : 'Temporal.ZonedDateTime' } ,
220- ] ;
221-
222- // Best-effort, using instanceof. Polyfills will likely pass these checks.
223- // TODO: Hopefully the V8 API will expose typechecks at some point.
224- function getTemporalInfo ( value ) {
225- try {
226- const { Temporal } = globalThis ;
227- if ( Temporal != null ) {
228- for ( let i = 0 ; i < temporalConstructors . length ; i ++ ) {
229- const entry = temporalConstructors [ i ] ;
230- const constructor = Temporal [ entry . name ] ;
231- if ( typeof constructor !== 'function' || ! FunctionPrototypeSymbolHasInstance ( constructor , value ) ) {
232- continue ;
233- }
234- const { prototype } = constructor ;
235- if ( ! ObjectPrototypeHasOwnProperty ( prototype , 'toString' ) ) {
236- return ;
237- }
238- const result = FunctionPrototypeCall ( prototype . toString , value ) ;
239- return { ...entry , result } ;
240- }
241- }
242- } catch { /* void */ }
243- }
244-
245213const builtInObjects = new SafeSet (
246214 ArrayPrototypeFilter (
247215 ObjectGetOwnPropertyNames ( globalThis ) ,
@@ -1355,7 +1323,6 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
13551323 if ( noIterator ) {
13561324 keys = getKeys ( value , ctx . showHidden ) ;
13571325 braces = [ '{' , '}' ] ;
1358- let temporalInfo ;
13591326 if ( typeof value === 'function' ) {
13601327 base = getFunctionBase ( ctx , value , constructor , tag ) ;
13611328 if ( keys . length === 0 && protoProps === undefined )
@@ -1439,11 +1406,67 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
14391406 if ( keys . length === 0 && protoProps === undefined ) {
14401407 return base ;
14411408 }
1442- } else if ( ( temporalInfo = getTemporalInfo ( value ) ) ) {
1443- const prefix = constructor === temporalInfo . name && tag === temporalInfo . fullName ?
1444- ( temporalInfo . fullName + ' ' ) :
1445- getPrefix ( constructor , tag , temporalInfo . fullName ) ;
1446- base = `${ prefix } ${ temporalInfo . result } ` ;
1409+ } else if ( temporal . isTemporalDuration ( value ) ) {
1410+ const prefix = constructor === 'Duration' && tag === 'Temporal.Duration' ?
1411+ ( 'Temporal.Duration ' ) :
1412+ getPrefix ( constructor , tag , 'Temporal.Duration' ) ;
1413+ base = `${ prefix } ${ temporal . TemporalDurationPrototypeToString ( value ) } ` ;
1414+ if ( keys . length === 0 && protoProps === undefined ) {
1415+ return ctx . stylize ( base , 'date' ) ;
1416+ }
1417+ } else if ( temporal . isTemporalInstant ( value ) ) {
1418+ const prefix = constructor === 'Instant' && tag === 'Temporal.Instant' ?
1419+ ( 'Temporal.Instant ' ) :
1420+ getPrefix ( constructor , tag , 'Temporal.Instant' ) ;
1421+ base = `${ prefix } ${ temporal . TemporalInstantPrototypeToString ( value ) } ` ;
1422+ if ( keys . length === 0 && protoProps === undefined ) {
1423+ return ctx . stylize ( base , 'date' ) ;
1424+ }
1425+ } else if ( temporal . isTemporalPlainDate ( value ) ) {
1426+ const prefix = constructor === 'PlainDate' && tag === 'Temporal.PlainDate' ?
1427+ ( 'Temporal.PlainDate ' ) :
1428+ getPrefix ( constructor , tag , 'Temporal.PlainDate' ) ;
1429+ base = `${ prefix } ${ temporal . TemporalPlainDatePrototypeToString ( value ) } ` ;
1430+ if ( keys . length === 0 && protoProps === undefined ) {
1431+ return ctx . stylize ( base , 'date' ) ;
1432+ }
1433+ } else if ( temporal . isTemporalPlainDateTime ( value ) ) {
1434+ const prefix = constructor === 'PlainDateTime' && tag === 'Temporal.PlainDateTime' ?
1435+ ( 'Temporal.PlainDateTime ' ) :
1436+ getPrefix ( constructor , tag , 'Temporal.PlainDateTime' ) ;
1437+ base = `${ prefix } ${ temporal . TemporalPlainDateTimePrototypeToString ( value ) } ` ;
1438+ if ( keys . length === 0 && protoProps === undefined ) {
1439+ return ctx . stylize ( base , 'date' ) ;
1440+ }
1441+ } else if ( temporal . isTemporalPlainMonthDay ( value ) ) {
1442+ const prefix = constructor === 'PlainMonthDay' && tag === 'Temporal.PlainMonthDay' ?
1443+ ( 'Temporal.PlainMonthDay ' ) :
1444+ getPrefix ( constructor , tag , 'Temporal.PlainMonthDay' ) ;
1445+ base = `${ prefix } ${ temporal . TemporalPlainMonthDayPrototypeToString ( value ) } ` ;
1446+ if ( keys . length === 0 && protoProps === undefined ) {
1447+ return ctx . stylize ( base , 'date' ) ;
1448+ }
1449+ } else if ( temporal . isTemporalPlainTime ( value ) ) {
1450+ const prefix = constructor === 'PlainTime' && tag === 'Temporal.PlainTime' ?
1451+ ( 'Temporal.PlainTime ' ) :
1452+ getPrefix ( constructor , tag , 'Temporal.PlainTime' ) ;
1453+ base = `${ prefix } ${ temporal . TemporalPlainTimePrototypeToString ( value ) } ` ;
1454+ if ( keys . length === 0 && protoProps === undefined ) {
1455+ return ctx . stylize ( base , 'date' ) ;
1456+ }
1457+ } else if ( temporal . isTemporalPlainYearMonth ( value ) ) {
1458+ const prefix = constructor === 'PlainYearMonth' && tag === 'Temporal.PlainYearMonth' ?
1459+ ( 'Temporal.PlainYearMonth ' ) :
1460+ getPrefix ( constructor , tag , 'Temporal.PlainYearMonth' ) ;
1461+ base = `${ prefix } ${ temporal . TemporalPlainYearMonthPrototypeToString ( value ) } ` ;
1462+ if ( keys . length === 0 && protoProps === undefined ) {
1463+ return ctx . stylize ( base , 'date' ) ;
1464+ }
1465+ } else if ( temporal . isTemporalZonedDateTime ( value ) ) {
1466+ const prefix = constructor === 'ZonedDateTime' && tag === 'Temporal.ZonedDateTime' ?
1467+ ( 'Temporal.ZonedDateTime ' ) :
1468+ getPrefix ( constructor , tag , 'Temporal.ZonedDateTime' ) ;
1469+ base = `${ prefix } ${ temporal . TemporalZonedDateTimePrototypeToString ( value ) } ` ;
14471470 if ( keys . length === 0 && protoProps === undefined ) {
14481471 return ctx . stylize ( base , 'date' ) ;
14491472 }
0 commit comments