All files / libs/kernel/file-generation/csv/src/lib csv-file-generator.ts

100% Statements 20/20
100% Branches 1/1
100% Functions 1/1
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x  
import { unparse, type UnparseConfig, type UnparseObject } from 'papaparse';
 
import { type FileGenerator } from '@amalia/kernel/file-generation/meta';
 
export const DEFAULT_CSV_BUILD_CONFIG: UnparseConfig = {
  header: true,
  skipEmptyLines: 'greedy',
  newline: '\r\n',
};
 
export type CsvFileGeneratorInput<T> = T[] | UnparseObject<T>;
 
// Note: this is the only one used only in frontend.
// Maybe move the other to the backend side and this one to the frontend scope?
// No need to make them compliant on both sides
export class CsvFileGenerator<T> implements FileGenerator<CsvFileGeneratorInput<T>, UnparseConfig, Promise<string>> {
  public generate(data: CsvFileGeneratorInput<T>, config?: UnparseConfig): Promise<string> {
    return Promise.resolve(unparse<T>(data, { ...DEFAULT_CSV_BUILD_CONFIG, ...config }));
  }
}