mirror of
https://github.com/hcengineering/platform.git
synced 2025-02-07 11:30:16 +00:00
42 lines
996 B
TypeScript
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'))
|
|
}
|
|
}
|