emmercm/igir

View on GitHub
src/modules/dats/datCombiner.ts

Summary

Maintainability
A
1 hr
Test Coverage
import moment from 'moment';

import ProgressBar from '../../console/progressBar.js';
import Package from '../../globals/package.js';
import ArrayPoly from '../../polyfill/arrayPoly.js';
import DAT from '../../types/dats/dat.js';
import Header from '../../types/dats/logiqx/header.js';
import LogiqxDAT from '../../types/dats/logiqx/logiqxDat.js';
import Module from '../module.js';

/**
 * Combine every {@link DAT} into a single {@link DAT}.
 */
export default class DATCombiner extends Module {
  constructor(progressBar: ProgressBar) {
    super(progressBar, DATCombiner.name);
  }

  /**
   * Combine the DATs.
   */
  combine(dats: DAT[]): DAT {
    this.progressBar.logTrace(`combining ${dats.length} DAT${dats.length !== 1 ? 's' : ''}`);

    const newDat = new LogiqxDAT(
      DATCombiner.generateHeader(dats),
      dats
        .flatMap((dat) => dat.getGames())
        .filter(ArrayPoly.filterUniqueMapped((game) => game.hashCode())),
    );

    this.progressBar.logTrace(`done combining ${dats.length} DAT${dats.length !== 1 ? 's' : ''}`);
    return newDat;
  }

  private static generateHeader(dats: DAT[]): Header {
    const datName = 'igir combined';
    const date = moment().format('YYYYMMDD-HHmmss');
    return new Header({
      name: datName,
      description: datName,
      version: date,
      date,
      author: Package.NAME,
      url: Package.HOMEPAGE,
      comment: [
        `Combined DAT generated by ${Package.NAME} v${Package.VERSION}`,
        ...dats.map((dat) => dat.getName()),
      ].join('\n'),
    });
  }
}