Files
utils/src/csv.ts
ztimson 681c89d5af
All checks were successful
Build / Publish Docs (push) Successful in 58s
Build / Build NPM Project (push) Successful in 55s
Build / Tag Version (push) Successful in 9s
Fixed fromCSV single quotes
2026-03-13 01:34:56 -04:00

103 lines
3.2 KiB
TypeScript

import {makeArray} from './array.ts';
import {ASet} from './aset.ts';
import {JSONSanitize} from './json.ts';
import {dotNotation, flattenObj} from './objects.ts';
import {LETTER_LIST} from './string.ts';
/**
* Parse a CSV string into an array of objects
*
* @param csv String with CSV
* @param hasHeaders First line of CSV contains headers
* @return {T[]} Array of parsed objects
*/
export function fromCsv<T = any>(csv: string, hasHeaders = true): T[] {
function parseLine(line: string): (string | null)[] {
const columns: string[] = [];
let current = '', inQuotes = false, quoteChar: string | null = null;
for (let i = 0; i < line.length; i++) {
const char = line[i];
const nextChar = line[i + 1];
if ((char === '"' || char === "'") && !inQuotes) {
inQuotes = true;
quoteChar = char;
} else if (char === quoteChar && inQuotes) {
if (nextChar === quoteChar) {
current += quoteChar; // Handle escaped quotes
i++;
} else {
inQuotes = false;
quoteChar = null;
}
} else if (char === ',' && !inQuotes) {
columns.push(current.trim());
current = '';
} else current += char;
}
columns.push(current.trim());
return columns.map(col => {
// Remove surrounding quotes (both " and ')
col = col.replace(/^["']|["']$/g, '');
// Unescape doubled quotes
return col.replace(/""/g, '"').replace(/''/g, "'");
});
}
const rows = [];
let currentRow = '', inQuotes = false, quoteChar: string | null = null;
for (const char of csv.replace(/\r\n/g, '\n')) {
if ((char === '"' || char === "'") && !inQuotes) {
inQuotes = true;
quoteChar = char;
} else if (char === quoteChar && inQuotes) {
inQuotes = false;
quoteChar = null;
}
if (char === '\n' && !inQuotes) {
rows.push(currentRow.trim());
currentRow = '';
} else currentRow += char;
}
if (currentRow) rows.push(currentRow.trim());
let headers: any = hasHeaders ? rows.splice(0, 1)[0] : null;
if (headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g)?.map((h: any) => h.trim());
return <T[]>rows.map(r => {
const props = parseLine(r);
const h = headers || (Array(props.length).fill(null).map((_, i) => {
let letter = '';
const first = i / 26;
if (first > 1) letter += LETTER_LIST[Math.floor(first - 1)];
letter += LETTER_LIST[i % 26];
return letter;
}));
return h.reduce((acc: any, h: any, i: number) => {
dotNotation(acc, h, props[i]);
return acc;
}, {});
});
}
/**
* Convert an array of objects 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
*/
export function toCsv(target: any, flatten=true) {
const t = makeArray(target);
const headers = new ASet(t.reduce((acc, row) => [...acc, ...Object.keys(flatten ? flattenObj(row) : row)], []));
return [
headers.join(','),
...t.map(row => headers.map((h: string) => {
const value = dotNotation<any>(row, h);
if(value == null) return '';
if(typeof value == 'object') return `"${JSONSanitize(value).replaceAll('"', '""')}"`;
if(typeof value == 'string' && /[\n",]/g.test(value)) return `"${value.replaceAll('"', '""')}"`;
return value;
}).join(','))
].join('\n');
}