src/Infrastructure/Tables/TableCsvFactory.ts   A
last analyzed

Complexity

Total Complexity 1
Complexity/F 1

Size

Lines of Code 27
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 21
mnd 0
bc 0
fnc 1
dl 0
loc 27
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A TableCsvFactory.toCsv 0 14 1
1
import { Inject, Injectable } from '@nestjs/common';
2
import { ITranslator } from '../Translations/ITranslator';
3
import { Table } from './';
4
5
@Injectable()
6
export class TableCsvFactory {
7
  constructor(
8
    @Inject('ITranslator')
9
    private translator: ITranslator
10
  ) {}
11
12
  public toCsv(table: Table): string {
13
    const header = table.columns.map(column =>
14
      this.translator.translate(column.toString())
15
    );
16
    const rows = table.rows.map(row =>
17
      row
18
        .map(cell => {
19
          const text = cell.renderText();
20
          return `"${text}"`; // Use quotes to allow newlines
21
        })
22
        .join(';')
23
    );
24
    return [header.join(';'), ...rows].join('\n');
25
  }
26
}
27