Skip to content

Commit 9a451a3

Browse files
committed
upgraded to Object.fromEntries
Ref: nodejs/node#25852 Fixes: Update to `Object.fromEntries` when v8 7.3 is integrated into node #2
1 parent 50b02fa commit 9a451a3

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

.github/workflows/gh-pages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Generate GitHub Pages Example
1+
name: Generate GitHub Pages
22

33
on:
44
push:

index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,18 @@ class LightMap extends Map
355355
// TODO: check for circular references
356356
toObject()
357357
{
358-
return this.reduce(
359-
( r, [ k, v ] ) => {
360-
if ( v instanceof LightMap ) {
361-
v = v.toObject();
362-
}
358+
const
359+
obj = Object.fromEntries( this ),
360+
keys = Object.keys( obj );
361+
362+
for ( let i = 0; i < keys.length; i++ ) {
363+
const key = keys[ i ];
364+
if ( obj[ key ] instanceof Map ) {
365+
obj[ key ] = obj[ key ].toObject();
366+
}
367+
}
363368

364-
r[ k ] = v;
365-
return r;
366-
}, {}
367-
);
369+
return obj;
368370
}
369371

370372
toJSON()

test/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,18 @@ describe( 'LightMap', () => {
451451
() => expect( Object.prototype.toString.call( new LightMap() ) ).to.eq( '[object LightMap]' )
452452
);
453453

454+
it( 'Symbol.species should equal Map',
455+
() => {
456+
const
457+
a = new LightMap( [ [ 'a', 1 ], [ 'a', 2 ] ] ),
458+
b = new Map( [ [ 'a', 1 ], [ 'a', 2 ] ] );
459+
460+
expect( LightMap[ Symbol.species ] ).to.eq( Map );
461+
expect( a instanceof LightMap[ Symbol.species ] ).to.eq( true );
462+
expect( b instanceof LightMap[ Symbol.species ] ).to.eq( true );
463+
}
464+
);
465+
454466
describe( 'Symbol.hasInstance', () => {
455467
it( 'should be false if instance of Map', () => {
456468
expect( new Map() instanceof LightMap ).to.eq( false );

0 commit comments

Comments
 (0)