const _ = require('lodash');
const {getNumber, getCollection} = require('./file');

class Merge {
  constructor(filename) {
    this.filename = filename;
  }

  async* merge() {
    const findMin = (values) => {
      let minIdx = undefined;
      for (const idx of _.range(number)) {
        const v = values[idx];
        if (_.isUndefined(v)) {
          continue;
        }
        minIdx = minIdx === undefined ? idx : minIdx;
        if (v < values[minIdx]) {
          minIdx = idx;
        }
      }
      return minIdx;
    };

    const number = await getNumber(this.filename);
    const collections = _.map(_.range(number), (i) => {
      return getCollection(this.filename, i);
    });
    const next = (i) => collections[i].next().then((x) => x.value);

    const initialValues = function* () {
      for (const i of _.range(number)) {
        yield next(i);
      }
    };
    const values = await Promise.all(initialValues());

    while (true) {
      if (_.every(values, (v) => _.isUndefined(v))) {
        break;
      }

      const minIdx = findMin(values);
      yield values[minIdx];
      values[minIdx] = await next(minIdx);
    }
  }
}

exports.Merge = Merge;

@@ -1,15 +1,16 @@
 const 
-_
+fp
  = requi
@@ -19,16 +19,19 @@
 ('lodash
+/fp
 ');%0Acons
@@ -259,32 +259,36 @@
 onst idx of 
-_
+fp
 .range(
+0, 
 number)) %7B%0A 
@@ -329,17 +329,18 @@
     if (
-_
+fp
 .isUndef
@@ -618,52 +618,21 @@
 s = 
-_
+fp
 .map(
-_.range(number), (i) =%3E %7B%0A      return
+(i) =%3E
  get
@@ -659,23 +659,37 @@
 name, i)
-;%0A    %7D
+)(fp.range(0, number)
 );%0A    c
@@ -819,16 +819,19 @@
  of 
-_
+fp
 .range(
+0,
 numb
@@ -964,32 +964,19 @@
 if (
-_
+fp
 .every(
-values, (v) =%3E _
+fp
 .isU
@@ -983,19 +983,24 @@
 ndefined
+)
 (v
-)
+alues
 )) %7B%0A   

use lodash fp