Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 681c89d5af |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.28.14",
|
"version": "0.28.15",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
47
src/csv.ts
47
src/csv.ts
@@ -14,41 +14,55 @@ import {LETTER_LIST} from './string.ts';
|
|||||||
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, quoteChar: string | null = null;
|
||||||
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 === '"' || char === "'") && !inQuotes) {
|
||||||
if (inQuotes && nextChar === '"') {
|
inQuotes = true;
|
||||||
current += '"'; // Handle escaped quotes
|
quoteChar = char;
|
||||||
|
} else if (char === quoteChar && inQuotes) {
|
||||||
|
if (nextChar === quoteChar) {
|
||||||
|
current += quoteChar; // Handle escaped quotes
|
||||||
i++;
|
i++;
|
||||||
} else inQuotes = !inQuotes;
|
} else {
|
||||||
|
inQuotes = false;
|
||||||
|
quoteChar = null;
|
||||||
|
}
|
||||||
} else if (char === ',' && !inQuotes) {
|
} else if (char === ',' && !inQuotes) {
|
||||||
columns.push(current.trim()); // Trim column values
|
columns.push(current.trim());
|
||||||
current = '';
|
current = '';
|
||||||
} else current += char;
|
} else current += char;
|
||||||
}
|
}
|
||||||
columns.push(current.trim()); // Trim last column value
|
columns.push(current.trim());
|
||||||
return columns.map(col => col.replace(/^"|"$/g, '').replace(/""/g, '"'));
|
return columns.map(col => {
|
||||||
|
// Remove surrounding quotes (both " and ')
|
||||||
|
col = col.replace(/^["']|["']$/g, '');
|
||||||
|
// Unescape doubled quotes
|
||||||
|
return col.replace(/""/g, '"').replace(/''/g, "'");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalize line endings and split rows
|
|
||||||
const rows = [];
|
const rows = [];
|
||||||
let currentRow = '', inQuotes = false;
|
let currentRow = '', inQuotes = false, quoteChar: string | null = null;
|
||||||
for (const char of csv.replace(/\r\n/g, '\n')) { // Normalize \r\n to \n
|
for (const char of csv.replace(/\r\n/g, '\n')) {
|
||||||
if (char === '"') inQuotes = !inQuotes;
|
if ((char === '"' || char === "'") && !inQuotes) {
|
||||||
|
inQuotes = true;
|
||||||
|
quoteChar = char;
|
||||||
|
} else if (char === quoteChar && inQuotes) {
|
||||||
|
inQuotes = false;
|
||||||
|
quoteChar = null;
|
||||||
|
}
|
||||||
if (char === '\n' && !inQuotes) {
|
if (char === '\n' && !inQuotes) {
|
||||||
rows.push(currentRow.trim()); // Trim row
|
rows.push(currentRow.trim());
|
||||||
currentRow = '';
|
currentRow = '';
|
||||||
} else currentRow += char;
|
} else currentRow += char;
|
||||||
}
|
}
|
||||||
if (currentRow) rows.push(currentRow.trim()); // Trim last row
|
if (currentRow) rows.push(currentRow.trim());
|
||||||
|
|
||||||
// Extract headers
|
|
||||||
let headers: any = hasHeaders ? rows.splice(0, 1)[0] : null;
|
let headers: any = hasHeaders ? rows.splice(0, 1)[0] : null;
|
||||||
if (headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g)?.map((h: any) => h.trim());
|
if (headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g)?.map((h: any) => h.trim());
|
||||||
|
|
||||||
// Parse rows
|
|
||||||
return <T[]>rows.map(r => {
|
return <T[]>rows.map(r => {
|
||||||
const props = parseLine(r);
|
const props = parseLine(r);
|
||||||
const h = headers || (Array(props.length).fill(null).map((_, i) => {
|
const h = headers || (Array(props.length).fill(null).map((_, i) => {
|
||||||
@@ -65,7 +79,6 @@ 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
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user