This repository was archived by the owner on Sep 15, 2023. It is now read-only.
File tree 4 files changed +31
-4
lines changed
4 files changed +31
-4
lines changed Original file line number Diff line number Diff line change
1
+ const arrayFrom = obj => {
2
+ try {
3
+ return Array . from ( obj )
4
+ } catch ( e ) {
5
+ return null
6
+ }
7
+ }
1
8
class Format {
2
9
constructor ( obj , options = { } ) {
3
10
this . options = options
@@ -51,9 +58,12 @@ class Format {
51
58
52
59
get objectAsArray ( ) {
53
60
const value = Array . isArray ( this . object ) ? this . object
54
- : this . isArray ( ) ? Array . from ( this . object )
61
+ : this . isArray ( ) ? arrayFrom ( this . object )
55
62
: null
56
63
64
+ if ( value === null )
65
+ this . isArray = ( ) => false
66
+
57
67
Object . defineProperty ( this , 'objectAsArray' , { value } )
58
68
return value
59
69
}
@@ -207,7 +217,7 @@ class Format {
207
217
: this . isSet ( ) ? this . set ( )
208
218
: this . isMap ( ) ? this . map ( )
209
219
: this . isBuffer ( ) ? this . buffer ( )
210
- : this . isArray ( ) ? this . array ( )
220
+ : this . isArray ( ) && this . objectAsArray ? this . array ( )
211
221
// TODO streams, JSX
212
222
: this . pojo ( )
213
223
@@ -441,7 +451,7 @@ class Format {
441
451
}
442
452
}
443
453
}
444
- return Array . from ( own )
454
+ return arrayFrom ( own )
445
455
} else
446
456
return Object . keys ( obj || this . object )
447
457
}
Original file line number Diff line number Diff line change @@ -3374,6 +3374,10 @@ Null Object {
3374
3374
}
3375
3375
`
3376
3376
3377
+ exports [ `test/format.js TAP invalid iterator > must match snapshot 1` ] = `
3378
+ Object {}
3379
+ `
3380
+
3377
3381
exports [ `test/format.js TAP locale sorting > must match snapshot 1` ] = `
3378
3382
Object {
3379
3383
"cat": "meow",
Original file line number Diff line number Diff line change @@ -256,3 +256,16 @@ t.test('locale sorting', t => {
256
256
t . matchSnapshot ( format ( obj , { sort : true } ) )
257
257
t . end ( )
258
258
} )
259
+
260
+ t . test ( 'invalid iterator' , t => {
261
+ const obj = { [ Symbol . iterator ] ( ) { return { } } }
262
+ t . matchSnapshot ( format ( obj ) )
263
+ const f = new Format ( obj )
264
+ // looks like an array
265
+ t . equal ( f . isArray ( ) , true )
266
+ // until you try to format it
267
+ t . equal ( f . print ( ) , 'Object {}' )
268
+ // then it realizes it's actually not
269
+ t . equal ( f . isArray ( ) , false )
270
+ t . end ( )
271
+ } )
Original file line number Diff line number Diff line change @@ -79,7 +79,7 @@ t.test('array-likes', t => {
79
79
a . push ( 1 , 2 , 3 )
80
80
const b = [ 1 , 2 , 3 ]
81
81
t . ok ( same ( t , a , b ) )
82
- t . notEqual ( a . constructor , b . constructor )
82
+ t . not ( a . constructor , b . constructor )
83
83
84
84
const args = ( function ( ) { return arguments } ) ( 1 , 2 , 3 )
85
85
const o = { [ Symbol . iterator ] : function * ( ) { for ( let i of a ) { yield i } } }
You can’t perform that action at this time.
0 commit comments