Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
a03567eba3 | |||
f9fc4fb7ff | |||
ff16f3bf9b |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.23.16",
|
"version": "0.23.19",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
41
src/csv.ts
41
src/csv.ts
@ -10,36 +10,50 @@ import {LETTER_LIST} from './string.ts';
|
|||||||
* @param hasHeaders First line of CSV contains headers
|
* @param hasHeaders First line of CSV contains headers
|
||||||
* @return {T[]} Array of parsed objects
|
* @return {T[]} Array of parsed objects
|
||||||
*/
|
*/
|
||||||
export function fromCsv<T = any>(csv: string, hasHeaders=true): T[] {
|
export function fromCsv<T = any>(csv: string, hasHeaders = true): T[] {
|
||||||
function parseLine(line: string): (string | null)[] {
|
function parseLine(line: string): (string | null)[] {
|
||||||
const columns: string[] = [];
|
const columns: string[] = [];
|
||||||
let current = '', inQuotes = false;
|
let current = '', inQuotes = false;
|
||||||
for(let i = 0; i < line.length; i++) {
|
for (let i = 0; i < line.length; i++) {
|
||||||
const char = line[i];
|
const char = line[i];
|
||||||
const nextChar = line[i + 1];
|
const nextChar = line[i + 1];
|
||||||
if (char === '"') {
|
if (char === '"') {
|
||||||
if (inQuotes && nextChar === '"') {
|
if (inQuotes && nextChar === '"') {
|
||||||
current += '"';
|
current += '"'; // Handle escaped quotes
|
||||||
i++;
|
i++;
|
||||||
} else inQuotes = !inQuotes;
|
} else inQuotes = !inQuotes;
|
||||||
} else if (char === ',' && !inQuotes) {
|
} else if (char === ',' && !inQuotes) {
|
||||||
columns.push(current);
|
columns.push(current.trim()); // Trim column values
|
||||||
current = '';
|
current = '';
|
||||||
} else current += char;
|
} else current += char;
|
||||||
}
|
}
|
||||||
columns.push(current);
|
columns.push(current.trim()); // Trim last column value
|
||||||
return columns.map(col => col.replace(/^"|"$/g, '').replace(/""/g, '"'));
|
return columns.map(col => col.replace(/^"|"$/g, '').replace(/""/g, '"'));
|
||||||
}
|
}
|
||||||
|
|
||||||
const row = csv.split(/\r?\n/);
|
// Normalize line endings and split rows
|
||||||
let headers: any = hasHeaders ? row.splice(0, 1)[0] : null;
|
const rows = [];
|
||||||
if(headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g);
|
let currentRow = '', inQuotes = false;
|
||||||
return <T[]>row.map(r => {
|
for (const char of csv.replace(/\r\n/g, '\n')) { // Normalize \r\n to \n
|
||||||
|
if (char === '"') inQuotes = !inQuotes;
|
||||||
|
if (char === '\n' && !inQuotes) {
|
||||||
|
rows.push(currentRow.trim()); // Trim row
|
||||||
|
currentRow = '';
|
||||||
|
} else currentRow += char;
|
||||||
|
}
|
||||||
|
if (currentRow) rows.push(currentRow.trim()); // Trim last row
|
||||||
|
|
||||||
|
// Extract headers
|
||||||
|
let headers: any = hasHeaders ? rows.splice(0, 1)[0] : null;
|
||||||
|
if (headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g)?.map((h: any) => h.trim());
|
||||||
|
|
||||||
|
// Parse rows
|
||||||
|
return <T[]>rows.map(r => {
|
||||||
const props = parseLine(r);
|
const props = parseLine(r);
|
||||||
const h = headers || (Array(props.length).fill(null).map((r, i) => {
|
const h = headers || (Array(props.length).fill(null).map((_, i) => {
|
||||||
let letter = '';
|
let letter = '';
|
||||||
const first = i / 26;
|
const first = i / 26;
|
||||||
if(first > 1) letter += LETTER_LIST[Math.floor(first - 1)];
|
if (first > 1) letter += LETTER_LIST[Math.floor(first - 1)];
|
||||||
letter += LETTER_LIST[i % 26];
|
letter += LETTER_LIST[i % 26];
|
||||||
return letter;
|
return letter;
|
||||||
}));
|
}));
|
||||||
@ -50,6 +64,7 @@ export function fromCsv<T = any>(csv: string, hasHeaders=true): T[] {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert an array of objects to a CSV string
|
* Convert an array of objects to a CSV string
|
||||||
*
|
*
|
||||||
@ -65,8 +80,8 @@ export function toCsv(target: any, flatten=true) {
|
|||||||
...t.map(row => headers.map((h: string) => {
|
...t.map(row => headers.map((h: string) => {
|
||||||
const value = dotNotation<any>(row, h);
|
const value = dotNotation<any>(row, h);
|
||||||
if(value == null) return '';
|
if(value == null) return '';
|
||||||
if(typeof value == 'object') return `"${JSONSanitize(value).replaceAll('`', '""')}"`;
|
if(typeof value == 'object') return `"${JSONSanitize(value).replaceAll('"', '""')}"`;
|
||||||
if(typeof value == 'string' && /[\n"]/g.test(value)) return `"${value.replaceAll('"', '""')}"`;
|
if(typeof value == 'string' && /[\n",]/g.test(value)) return `"${value.replaceAll('"', '""')}"`;
|
||||||
return value;
|
return value;
|
||||||
}).join(','))
|
}).join(','))
|
||||||
].join('\n');
|
].join('\n');
|
||||||
|
Reference in New Issue
Block a user