/** * Open example · Cihan Üstündağ Atelier * CSV parser (single-file local utility). No remote network access. * Supports quoted fields, escaped double quotes, commas and newlines. * No external dependency. MIT license: see public/assets/code/LICENSE.txt */ export function parseCsv(source, {maxRows = 5000} = {}) { if (typeof source !== 'string') throw new TypeError('CSV metni gerekli'); if (source.length > 2_500_000) throw new Error('2.5 MB sınırı aşıldı'); let cells = [], rows = [], field = '', quoted = false; for (let i=0; ix!=='')) rows.push(cells); cells=[]; if (ch==='\r' && source[i+1]==='\n') i++; if (rows.length > maxRows+1) throw new Error(`En fazla ${maxRows} veri satırı desteklenir`); } else field += ch; } if (quoted) throw new Error('Kapanmamış çift tırnak bulundu'); cells.push(field.trim()); if (cells.some(x=>x!=='')) rows.push(cells); if (!rows.length) return {columns:[], records:[]}; const columns=rows.shift().map((x,i)=> x || `Sutun_${i+1}`); if (new Set(columns).size !== columns.length) throw new Error('Sütun adları benzersiz olmalı'); const records=rows.map((r,i)=> { if (r.length > columns.length) throw new Error(`${i+2}. satırda fazla sütun var`); return Object.fromEntries(columns.map((name,j)=>[name,r[j]??''])); }); return {columns,records}; } export function numericSummary(records,column) { const values=records.map(x=> String(x[column]??'').trim()).filter(x=>x!=='').map(x=>Number(x.replace(',','.'))).filter(Number.isFinite); if (!values.length) return {count:0,total:0,mean:0,min:null,max:null}; return {count:values.length,total:values.reduce((a,b)=>a+b,0),mean:values.reduce((a,b)=>a+b,0)/values.length,min:Math.min(...values),max:Math.max(...values)}; } export function countBy(records,column) { const counts=new Map(); for (const row of records) { const key=String(row[column]??'(boş)').trim()||'(boş)'; counts.set(key,(counts.get(key)||0)+1); } return Array.from(counts.entries()).sort((a,b)=>b[1]-a[1]); }