mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-20 06:55:06 -04:00
35 lines
895 B
TypeScript
35 lines
895 B
TypeScript
export { getHeaders, convertArrayToCsv };
|
|
|
|
function getHeaders({ array }: { array: Record<string, unknown>[] }): string[] {
|
|
const headers = new Set<string>();
|
|
|
|
array.forEach(item => Object.keys(item).forEach(key => headers.add(key)));
|
|
|
|
return Array.from(headers);
|
|
}
|
|
|
|
function serializeValue(value: unknown): string {
|
|
if (value === null) {
|
|
return 'null';
|
|
}
|
|
|
|
if (value === undefined) {
|
|
return '';
|
|
}
|
|
|
|
const valueAsString = String(value).replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/"/g, '\\"');
|
|
|
|
if (valueAsString.includes(',')) {
|
|
return `"${valueAsString}"`;
|
|
}
|
|
|
|
return valueAsString;
|
|
}
|
|
|
|
function convertArrayToCsv({ array }: { array: Record<string, unknown>[] }): string {
|
|
const headers = getHeaders({ array });
|
|
|
|
const rows = array.map(item => headers.map(header => serializeValue(item[header])));
|
|
|
|
return [headers.join(','), ...rows].join('\n');
|
|
}
|