@@ -136,16 +136,16 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
136136 }
137137
138138 function readAttachmentsAsBlobOrBuffer ( res ) {
139- res . rows . forEach ( function ( row ) {
139+ for ( const row of res . rows ) {
140140 const atts = row . doc && row . doc . _attachments ;
141141 if ( ! atts ) {
142- return ;
142+ continue ;
143143 }
144- Object . keys ( atts ) . forEach ( function ( filename ) {
144+ for ( const filename of Object . keys ( atts ) ) {
145145 const att = atts [ filename ] ;
146146 atts [ filename ] . data = b64ToBluffer ( att . data , att . content_type ) ;
147- } ) ;
148- } ) ;
147+ }
148+ }
149149 }
150150
151151 function postprocessAttachments ( opts ) {
@@ -216,12 +216,12 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
216216 '{group: true}' ) ;
217217 }
218218 }
219- [ 'group_level' , 'limit' , 'skip' ] . forEach ( function ( optionName ) {
219+ for ( const optionName of [ 'group_level' , 'limit' , 'skip' ] ) {
220220 const error = checkPositiveInteger ( options [ optionName ] ) ;
221221 if ( error ) {
222222 throw error ;
223223 }
224- } ) ;
224+ }
225225 }
226226
227227 async function httpQuery ( db , fun , opts ) {
@@ -298,12 +298,12 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
298298 }
299299
300300 // fail the entire request if the result contains an error
301- result . rows . forEach ( function ( row ) {
301+ for ( const row of result . rows ) {
302302 /* istanbul ignore if */
303303 if ( row . value && row . value . error && row . value . error === "builtin_reduce_error" ) {
304304 throw new Error ( row . reason ) ;
305305 }
306- } ) ;
306+ }
307307
308308 return new Promise ( function ( resolve ) {
309309 resolve ( result ) ;
@@ -312,13 +312,13 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
312312
313313 // We are using a temporary view, terrible for performance, good for testing
314314 body = body || { } ;
315- Object . keys ( fun ) . forEach ( function ( key ) {
315+ for ( const key of Object . keys ( fun ) ) {
316316 if ( Array . isArray ( fun [ key ] ) ) {
317317 body [ key ] = fun [ key ] ;
318318 } else {
319319 body [ key ] = fun [ key ] . toString ( ) ;
320320 }
321- } ) ;
321+ }
322322
323323 const response = await db . fetch ( '_temp_view' + params , {
324324 headers : new Headers ( { 'Content-Type' : 'application/json' } ) ,
@@ -412,8 +412,7 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
412412 const kvDocs = [ ] ;
413413 const oldKeys = new Set ( ) ;
414414
415- for ( let i = 0 , len = kvDocsRes . rows . length ; i < len ; i ++ ) {
416- const row = kvDocsRes . rows [ i ] ;
415+ for ( const row of kvDocsRes . rows ) {
417416 const doc = row . doc ;
418417 if ( ! doc ) { // deleted
419418 continue ;
@@ -429,7 +428,7 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
429428 }
430429 }
431430 const newKeys = mapToKeysArray ( indexableKeysToKeyValues ) ;
432- newKeys . forEach ( function ( key ) {
431+ for ( const key of newKeys ) {
433432 if ( ! oldKeys . has ( key ) ) {
434433 // new doc
435434 const kvDoc = {
@@ -441,7 +440,7 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
441440 }
442441 kvDocs . push ( kvDoc ) ;
443442 }
444- } ) ;
443+ }
445444 metaDoc . keys = uniq ( newKeys . concat ( metaDoc . keys ) ) ;
446445 kvDocs . push ( metaDoc ) ;
447446
@@ -650,8 +649,7 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
650649
651650 function createDocIdsToChangesAndEmits ( results ) {
652651 const docIdsToChangesAndEmits = new Map ( ) ;
653- for ( let i = 0 , len = results . length ; i < len ; i ++ ) {
654- const change = results [ i ] ;
652+ for ( const change of results ) {
655653 if ( change . doc . _id [ 0 ] !== '_' ) {
656654 mapResults = [ ] ;
657655 doc = change . doc ;
@@ -704,48 +702,48 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
704702 }
705703
706704 const shouldGroup = options . group || options . group_level ;
707-
708705 const reduceFun = reducer ( view . reduceFun ) ;
709-
710706 const groups = [ ] ;
711- const lvl = isNaN ( options . group_level ) ? Number . POSITIVE_INFINITY :
712- options . group_level ;
713- results . forEach ( function ( e ) {
707+ const lvl = isNaN ( options . group_level )
708+ ? Number . POSITIVE_INFINITY
709+ : options . group_level ;
710+
711+ for ( const result of results ) {
714712 const last = groups [ groups . length - 1 ] ;
715- let groupKey = shouldGroup ? e . key : null ;
713+ let groupKey = shouldGroup ? result . key : null ;
716714
717715 // only set group_level for array keys
718716 if ( shouldGroup && Array . isArray ( groupKey ) ) {
719717 groupKey = groupKey . slice ( 0 , lvl ) ;
720718 }
721719
722720 if ( last && collate ( last . groupKey , groupKey ) === 0 ) {
723- last . keys . push ( [ e . key , e . id ] ) ;
724- last . values . push ( e . value ) ;
725- return ;
721+ last . keys . push ( [ result . key , result . id ] ) ;
722+ last . values . push ( result . value ) ;
723+ continue ;
726724 }
727725 groups . push ( {
728- keys : [ [ e . key , e . id ] ] ,
729- values : [ e . value ] ,
726+ keys : [ [ result . key , result . id ] ] ,
727+ values : [ result . value ] ,
730728 groupKey
731729 } ) ;
732- } ) ;
730+ }
731+
733732 results = [ ] ;
734- for ( let i = 0 , len = groups . length ; i < len ; i ++ ) {
735- const e = groups [ i ] ;
736- const reduceTry = tryReduce ( view . sourceDB , reduceFun , e . keys , e . values , false ) ;
733+ for ( const group of groups ) {
734+ const reduceTry = tryReduce ( view . sourceDB , reduceFun , group . keys , group . values , false ) ;
737735 if ( reduceTry . error && reduceTry . error instanceof BuiltInError ) {
738736 // CouchDB returns an error if a built-in errors out
739737 throw reduceTry . error ;
740738 }
741739 results . push ( {
742740 // CouchDB just sets the value to null if a non-built-in errors out
743741 value : reduceTry . error ? null : reduceTry . output ,
744- key : e . groupKey
742+ key : group . groupKey
745743 } ) ;
746744 }
747745 // no total_rows/offset when reducing
748- return { rows : sliceResults ( results , options . limit , options . skip ) } ;
746+ return { rows : sliceResults ( results , options . limit , options . skip ) } ;
749747 }
750748
751749 function queryView ( view , opts ) {
@@ -827,20 +825,18 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
827825 binary : opts . binary
828826 } ) ;
829827 const docIdsToDocs = new Map ( ) ;
830- allDocsRes . rows . forEach ( function ( row ) {
828+ for ( const row of allDocsRes . rows ) {
831829 docIdsToDocs . set ( row . id , row . doc ) ;
832- } ) ;
833- rows . forEach ( function ( row ) {
830+ }
831+ for ( const row of rows ) {
834832 const docId = rowToDocId ( row ) ;
835833 const doc = docIdsToDocs . get ( docId ) ;
836834 if ( doc ) {
837835 row . doc = doc ;
838836 }
839- } ) ;
840- return finalResults ;
841- } else {
842- return finalResults ;
837+ }
843838 }
839+ return finalResults ;
844840 }
845841
846842 if ( typeof opts . keys !== 'undefined' ) {
@@ -931,7 +927,7 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
931927 const metaDoc = await db . get ( '_local/' + localDocName ) ;
932928 const docsToViews = new Map ( ) ;
933929
934- Object . keys ( metaDoc . views ) . forEach ( function ( fullViewName ) {
930+ for ( const fullViewName of Object . keys ( metaDoc . views ) ) {
935931 const parts = parseViewName ( fullViewName ) ;
936932 const designDocName = '_design/' + parts [ 0 ] ;
937933 const viewName = parts [ 1 ] ;
@@ -941,17 +937,17 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
941937 docsToViews . set ( designDocName , views ) ;
942938 }
943939 views . add ( viewName ) ;
944- } ) ;
940+ }
945941 const opts = {
946942 keys : mapToKeysArray ( docsToViews ) ,
947943 include_docs : true
948944 } ;
949945
950946 const res = await db . allDocs ( opts ) ;
951947 const viewsToStatus = { } ;
952- res . rows . forEach ( function ( row ) {
948+ for ( const row of res . rows ) {
953949 const ddocName = row . key . substring ( 8 ) ; // cuts off '_design/'
954- docsToViews . get ( row . key ) . forEach ( function ( viewName ) {
950+ for ( const viewName of docsToViews . get ( row . key ) ) {
955951 let fullViewName = ddocName + '/' + viewName ;
956952 /* istanbul ignore if */
957953 if ( ! metaDoc . views [ fullViewName ] ) {
@@ -963,12 +959,11 @@ function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) {
963959 // design doc deleted, or view function nonexistent
964960 const statusIsGood = row . doc && row . doc . views &&
965961 row . doc . views [ viewName ] ;
966- viewDBNames . forEach ( function ( viewDBName ) {
967- viewsToStatus [ viewDBName ] =
968- viewsToStatus [ viewDBName ] || statusIsGood ;
969- } ) ;
970- } ) ;
971- } ) ;
962+ for ( const viewDBName of viewDBNames ) {
963+ viewsToStatus [ viewDBName ] = viewsToStatus [ viewDBName ] || statusIsGood ;
964+ }
965+ }
966+ }
972967
973968 const dbsToDelete = Object . keys ( viewsToStatus )
974969 . filter ( function ( viewDBName ) { return ! viewsToStatus [ viewDBName ] ; } ) ;
0 commit comments