@@ -284,10 +284,17 @@ function formatValue(ctx, value, recurseTimes) {
284
284
formatted = formatPrimitiveNoColor ( ctx , raw ) ;
285
285
return ctx . stylize ( '[Boolean: ' + formatted + ']' , 'boolean' ) ;
286
286
}
287
+ // Fast path for ArrayBuffer. Can't do the same for DataView because it
288
+ // has a non-primitive .buffer property that we need to recurse for.
289
+ if ( value instanceof ArrayBuffer ) {
290
+ return `${ getConstructorOf ( value ) . name } ` +
291
+ ` { byteLength: ${ formatNumber ( ctx , value . byteLength ) } }` ;
292
+ }
287
293
}
288
294
289
295
var constructor = getConstructorOf ( value ) ;
290
- var base = '' , empty = false , braces , formatter ;
296
+ var base = '' , empty = false , braces ;
297
+ var formatter = formatObject ;
291
298
292
299
if ( Array . isArray ( value ) ) {
293
300
// We can't use `constructor === Array` because this could
@@ -314,6 +321,28 @@ function formatValue(ctx, value, recurseTimes) {
314
321
keys . unshift ( 'size' ) ;
315
322
empty = value . size === 0 ;
316
323
formatter = formatMap ;
324
+ } else if ( value instanceof ArrayBuffer ) {
325
+ braces = [ '{' , '}' ] ;
326
+ keys . unshift ( 'byteLength' ) ;
327
+ visibleKeys . byteLength = true ;
328
+ } else if ( value instanceof DataView ) {
329
+ braces = [ '{' , '}' ] ;
330
+ // .buffer goes last, it's not a primitive like the others.
331
+ keys . unshift ( 'byteLength' , 'byteOffset' , 'buffer' ) ;
332
+ visibleKeys . byteLength = true ;
333
+ visibleKeys . byteOffset = true ;
334
+ visibleKeys . buffer = true ;
335
+ } else if ( isTypedArray ( value ) ) {
336
+ braces = [ '[' , ']' ] ;
337
+ formatter = formatTypedArray ;
338
+ if ( ctx . showHidden ) {
339
+ // .buffer goes last, it's not a primitive like the others.
340
+ keys . unshift ( 'BYTES_PER_ELEMENT' ,
341
+ 'length' ,
342
+ 'byteLength' ,
343
+ 'byteOffset' ,
344
+ 'buffer' ) ;
345
+ }
317
346
} else {
318
347
// Only create a mirror if the object superficially looks like a Promise.
319
348
var promiseInternals = value instanceof Promise && inspectPromise ( value ) ;
@@ -336,7 +365,6 @@ function formatValue(ctx, value, recurseTimes) {
336
365
constructor = null ;
337
366
braces = [ '{' , '}' ] ;
338
367
empty = true ; // No other data than keys.
339
- formatter = formatObject ;
340
368
}
341
369
}
342
370
}
@@ -408,6 +436,15 @@ function formatValue(ctx, value, recurseTimes) {
408
436
}
409
437
410
438
439
+ function formatNumber ( ctx , value ) {
440
+ // Format -0 as '-0'. Strict equality won't distinguish 0 from -0,
441
+ // so instead we use the fact that 1 / -0 < 0 whereas 1 / 0 > 0 .
442
+ if ( value === 0 && 1 / value < 0 )
443
+ return ctx . stylize ( '-0' , 'number' ) ;
444
+ return ctx . stylize ( '' + value , 'number' ) ;
445
+ }
446
+
447
+
411
448
function formatPrimitive ( ctx , value ) {
412
449
if ( value === undefined )
413
450
return ctx . stylize ( 'undefined' , 'undefined' ) ;
@@ -424,13 +461,8 @@ function formatPrimitive(ctx, value) {
424
461
. replace ( / \\ " / g, '"' ) + '\'' ;
425
462
return ctx . stylize ( simple , 'string' ) ;
426
463
}
427
- if ( type === 'number' ) {
428
- // Format -0 as '-0'. Strict equality won't distinguish 0 from -0,
429
- // so instead we use the fact that 1 / -0 < 0 whereas 1 / 0 > 0 .
430
- if ( value === 0 && 1 / value < 0 )
431
- return ctx . stylize ( '-0' , 'number' ) ;
432
- return ctx . stylize ( '' + value , 'number' ) ;
433
- }
464
+ if ( type === 'number' )
465
+ return formatNumber ( ctx , value ) ;
434
466
if ( type === 'boolean' )
435
467
return ctx . stylize ( '' + value , 'boolean' ) ;
436
468
// es6 symbol primitive
@@ -480,6 +512,20 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
480
512
}
481
513
482
514
515
+ function formatTypedArray ( ctx , value , recurseTimes , visibleKeys , keys ) {
516
+ var output = new Array ( value . length ) ;
517
+ for ( var i = 0 , l = value . length ; i < l ; ++ i )
518
+ output [ i ] = formatNumber ( ctx , value [ i ] ) ;
519
+ for ( const key of keys ) {
520
+ if ( typeof key === 'symbol' || ! key . match ( / ^ \d + $ / ) ) {
521
+ output . push (
522
+ formatProperty ( ctx , value , recurseTimes , visibleKeys , key , true ) ) ;
523
+ }
524
+ }
525
+ return output ;
526
+ }
527
+
528
+
483
529
function formatSet ( ctx , value , recurseTimes , visibleKeys , keys ) {
484
530
var output = [ ] ;
485
531
value . forEach ( function ( v ) {
@@ -626,6 +672,19 @@ function reduceToSingleString(output, base, braces) {
626
672
}
627
673
628
674
675
+ function isTypedArray ( value ) {
676
+ return value instanceof Float32Array ||
677
+ value instanceof Float64Array ||
678
+ value instanceof Int16Array ||
679
+ value instanceof Int32Array ||
680
+ value instanceof Int8Array ||
681
+ value instanceof Uint16Array ||
682
+ value instanceof Uint32Array ||
683
+ value instanceof Uint8Array ||
684
+ value instanceof Uint8ClampedArray ;
685
+ }
686
+
687
+
629
688
// NOTE: These type checking functions intentionally don't use `instanceof`
630
689
// because it is fragile and can be easily faked with `Object.create()`.
631
690
exports . isArray = Array . isArray ;
0 commit comments