platform/dev/tool/src/csv.ts
Andrey Sobolev 149d8cda46
TSK-1414: Fix exceptions in Kanban (#3119) (#3123)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
2023-05-03 20:09:48 +06:00

42 lines
996 B
TypeScript

import { writeFile } from 'fs/promises'
/**
* @public
*/
export class CSVWriter<T extends Record<string, string | number>> {
data: string[] = []
constructor (readonly fields: Record<keyof T, string>) {
this.data.push(
Object.entries(this.fields)
.map(([key, value]) => `"${value}"`)
.join(',')
)
}
toStr (val: string | number): string {
if (typeof val === 'number') {
return `"${Math.round(val * 100) / 100}"`.replace('.', ',')
}
return `"${val}"`
}
add (record: T, print: boolean = true): void {
this.data.push(
Object.entries(this.fields)
.map(([key, value]) => this.toStr(record[key]))
.join(',')
)
if (print) {
console.log(
Object.entries(this.fields)
.map(([key, value]) => `${value}=${this.toStr(record[key])}`)
.join(' ')
)
}
}
async write (filename: string): Promise<void> {
return await writeFile(filename, this.data.join('\n'))
}
}