Compare commits

...

2 Commits

Author SHA1 Message Date
361613f507 Added decodeHTML
All checks were successful
Build / Publish Docs (push) Successful in 1m6s
Build / Build NPM Project (push) Successful in 1m10s
Build / Tag Version (push) Successful in 11s
2026-03-29 22:33:21 -04:00
681c89d5af Fixed fromCSV single quotes
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
2026-03-13 01:34:56 -04:00
3 changed files with 56 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.28.14", "version": "0.28.16",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@@ -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
* *

View File

@@ -27,6 +27,31 @@ export function camelCase(str?: string): string {
return pascal.charAt(0).toLowerCase() + pascal.slice(1); return pascal.charAt(0).toLowerCase() + pascal.slice(1);
} }
/**
* Decode HTML escaped characters
* @param html HTML to clean up
* @returns {any}
*/
export function decodeHtml(html: string) {
return html
.replace(/&nbsp;/g, '\u00A0')
.replace(/&quot;/g, '"')
.replace(/&apos;/g, "'")
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&cent;/g, '¢')
.replace(/&pound;/g, '£')
.replace(/&yen;/g, '¥')
.replace(/&euro;/g, '€')
.replace(/&copy;/g, '©')
.replace(/&reg;/g, '®')
.replace(/&trade;/g, '™')
.replace(/&times;/g, '×')
.replace(/&divide;/g, '÷')
.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec))
.replace(/&#x([0-9a-fA-F]+);/g, (match, hex) => String.fromCharCode(parseInt(hex, 16)))
.replace(/&amp;/g, '&'); // Always last!
}
/** /**
* Convert number of bytes into a human-readable size * Convert number of bytes into a human-readable size