Skip to content

Commit

Permalink
upgraded to Object.fromEntries
Browse files Browse the repository at this point in the history
Ref: nodejs/node#25852
Fixes: Update to `Object.fromEntries` when v8 7.3 is integrated into node #2
  • Loading branch information
iSkore committed Sep 9, 2020
1 parent 50b02fa commit 9a451a3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Generate GitHub Pages Example
name: Generate GitHub Pages

on:
push:
Expand Down
20 changes: 11 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,18 @@ class LightMap extends Map
// TODO: check for circular references
toObject()
{
return this.reduce(
( r, [ k, v ] ) => {
if ( v instanceof LightMap ) {
v = v.toObject();
}
const
obj = Object.fromEntries( this ),
keys = Object.keys( obj );

for ( let i = 0; i < keys.length; i++ ) {
const key = keys[ i ];
if ( obj[ key ] instanceof Map ) {
obj[ key ] = obj[ key ].toObject();
}
}

r[ k ] = v;
return r;
}, {}
);
return obj;
}

toJSON()
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,18 @@ describe( 'LightMap', () => {
() => expect( Object.prototype.toString.call( new LightMap() ) ).to.eq( '[object LightMap]' )
);

it( 'Symbol.species should equal Map',
() => {
const
a = new LightMap( [ [ 'a', 1 ], [ 'a', 2 ] ] ),
b = new Map( [ [ 'a', 1 ], [ 'a', 2 ] ] );

expect( LightMap[ Symbol.species ] ).to.eq( Map );
expect( a instanceof LightMap[ Symbol.species ] ).to.eq( true );
expect( b instanceof LightMap[ Symbol.species ] ).to.eq( true );
}
);

describe( 'Symbol.hasInstance', () => {
it( 'should be false if instance of Map', () => {
expect( new Map() instanceof LightMap ).to.eq( false );
Expand Down

0 comments on commit 9a451a3

Please sign in to comment.