From 9a451a3669b8ec1711fd19588fae6143aebbe6cb Mon Sep 17 00:00:00 2001 From: Nick Soggin Date: Wed, 9 Sep 2020 20:34:18 +0000 Subject: [PATCH] upgraded to Object.fromEntries Ref: nodejs/node#25852 Fixes: Update to `Object.fromEntries` when v8 7.3 is integrated into node #2 --- .github/workflows/gh-pages.yml | 2 +- index.js | 20 +++++++++++--------- test/index.test.js | 12 ++++++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index 02e816c..2ba1ff1 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -1,4 +1,4 @@ -name: Generate GitHub Pages Example +name: Generate GitHub Pages on: push: diff --git a/index.js b/index.js index 9b2c804..e1801ee 100644 --- a/index.js +++ b/index.js @@ -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() diff --git a/test/index.test.js b/test/index.test.js index f084c11..ce65830 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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 );