All files / src/app/services/data-service format.service.ts

100% Statements 32/32
100% Branches 2/2
100% Functions 6/6
100% Lines 30/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50      1x   1x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x             5x 5x 5x 4x 4x 4x 4x 28x 4x 4x   28x   4x       1x 4x     1x 4x   1x  
 
import { IFileInfo } from '../../model/fileInfo.model';
 
export abstract class FormatService {
 
  public processContent(content: string, fileInfo: IFileInfo ): IFileInfo {
    const id = 'dataContainer';
    const searchFor = /Command[\s]+Timeout[\s]+only[\s]+supported[\s]+by[\s]+SQL[\s]+Client[\s]+\(so[\s]+far\)<br\/>/;
    const regex = new RegExp(searchFor, 'g');
    const data = content.replace(regex, '');
 
    const tempDiv = document.createElement('div');
    tempDiv.innerHTML = data;
    this.prefixStylesWithId(tempDiv, id);
    fileInfo.content = tempDiv.innerHTML;
    fileInfo.metric = this.getMetric(tempDiv);
    fileInfo.badgeColor = this.getBadgeColor(fileInfo.metric);
    return fileInfo;
  }
 
  public abstract getMetric(tempDiv: HTMLElement): number | string ;
 
  public abstract getBadgeColor(errorCount): string;
 
  private prefixStylesWithId(tempDiv: HTMLElement, id: string) {
    const styleSections = tempDiv.getElementsByTagName('style');
    for (let i = 0; i < styleSections.length; ++i) {
      const styles = styleSections[i];
      const styleLines = styles.innerHTML.split(/\r?\n/);
      const newStyles = new Array<String>();
      styleLines.forEach((line: string) => {
        if (line.indexOf('{') !== -1) {
          line = '#' + id + ' ' + line;
          line = this.replaceAll(line, ',', ', #' + id);
        }
        newStyles.push(line);
      });
      styles.innerHTML = newStyles.join('\n');
    }
  }
 
  private replaceAll(theString: string, search: string, replace: string ): string {
    return theString.replace(new RegExp(this.escapeRegExp(search), 'g'), replace);
  }
 
  private escapeRegExp(str) {
    return str.replace(/[.*+?^${}()|[\]\\]/g, '\$&'); // $& means the whole matched string
  }
}