Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgraded to Object.fromEntries #11

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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