2024-11-09 13:49:04 -05:00
|
|
|
import {ASet} from './aset.ts';
|
2024-11-09 16:30:05 -05:00
|
|
|
import {dotNotation, flattenObj, JSONSanitize} from './objects.ts';
|
|
|
|
|
|
|
|
export function fromCsv<T = any>(csv: string, hasHeaders=true): T[] {
|
|
|
|
const row = csv.split('\n');
|
|
|
|
let headers: any = hasHeaders ? row.splice(0, 1)[0] : null;
|
|
|
|
if(headers) headers = headers.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g);
|
|
|
|
return <T[]>row.map(r => {
|
|
|
|
const props: string[] = <any>r.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g);
|
|
|
|
const h = headers || (Array(props.length).fill(null).map((r, i) => {
|
|
|
|
let letter = '';
|
|
|
|
const first = i / 26;
|
|
|
|
if(first > 1) letter +=
|
|
|
|
i % 26
|
|
|
|
i
|
|
|
|
}));
|
|
|
|
return h.reduce((acc: any, h: any, i: number) => ({...acc, [h]: props[i]}), {})
|
|
|
|
})
|
|
|
|
}
|
2024-09-22 03:35:12 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an object to a CSV string
|
|
|
|
*
|
|
|
|
* @param {any[]} target Array of objects to create CSV from
|
|
|
|
* @param {boolean} flatten Should nested object be flattened or treated as values
|
|
|
|
* @return {string} CSV string
|
|
|
|
*/
|
2024-11-09 13:49:04 -05:00
|
|
|
export function toCsv(target: any[], flatten=true) {
|
|
|
|
const headers = new ASet(target.reduce((acc, row) => [...acc, ...Object.keys(flatten ? flattenObj(row) : row)], []));
|
2024-09-22 03:35:12 -04:00
|
|
|
return [
|
|
|
|
headers.join(','),
|
|
|
|
...target.map(row => headers.map((h: string) => {
|
|
|
|
const value = dotNotation<any>(row, h);
|
2024-11-09 13:49:04 -05:00
|
|
|
return (typeof value == 'object' && value != null) ? '"' + JSONSanitize(value).replaceAll('"', '""') + '"' : value;
|
2024-09-22 03:35:12 -04:00
|
|
|
}).join(','))
|
|
|
|
].join('\n');
|
|
|
|
}
|