| 1 |
import{lpStatsState}from"./state.js";const sanitizeSegment=e=>String(e??"").toLowerCase().replace(/[^a-z0-9-]+/g,"-").replace(/^-+|-+$/g,"")||"data";export const buildCsvFilename=(e,t)=>{const{filtertype:n}=lpStatsState.get();return`learnpress-${sanitizeSegment(e)}-${sanitizeSegment(t)}-${sanitizeSegment(n)}.csv`};const escapeCell=e=>{let t=null==e?"":String(e);return/^[=+\-@]/.test(t)&&(t=`'${t}`),/[",\n\r]/.test(t)&&(t=`"${t.replace(/"/g,'""')}"`),t};export const exportCsv=(e,t=[],n=[])=>{if(!t.length)return;const s=[t.map(e=>escapeCell(e.label)).join(",")];n.forEach(e=>{s.push(t.map(t=>{const n="function"==typeof t.csv?t.csv(e):e[t.key];return escapeCell(n)}).join(","))});const a=new Blob(["\ufeff"+s.join("\r\n")],{type:"text/csv;charset=utf-8;"}),o=URL.createObjectURL(a),r=document.createElement("a");r.href=o,r.download=e,document.body.appendChild(r),r.click(),document.body.removeChild(r),URL.revokeObjectURL(o)}; |