@marktiderman/genesis-studio 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -115,77 +115,7 @@ function fieldLabel(field) {
115
115
  }
116
116
 
117
117
  // src/lib/csv.ts
118
- function csvField(value) {
119
- if (value === null || value === void 0) return "";
120
- const text = typeof value === "object" ? JSON.stringify(value) : String(value);
121
- const plainNumber = /^-?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(text);
122
- const safe = !plainNumber && /^[=+\-@\t\r]/.test(text) ? `'${text}` : text;
123
- return /[",\r\n]/.test(safe) ? `"${safe.replace(/"/g, '""')}"` : safe;
124
- }
125
- function toCsv(fields, rows) {
126
- const lines = [fields.map((f) => csvField(f.name)).join(",")];
127
- for (const row of rows) lines.push(fields.map((f) => csvField(row[f.name])).join(","));
128
- return `${lines.join("\r\n")}\r
129
- `;
130
- }
131
- async function exportCsv(options) {
132
- const perPage = 200;
133
- const max = options.maxRows ?? 1e4;
134
- const rows = [];
135
- let truncated = false;
136
- for (let page = 1; ; page++) {
137
- const result = await options.provider.getList(options.resource, {
138
- pagination: { page, perPage },
139
- sort: options.sorts[0],
140
- sorts: options.sorts,
141
- filters: options.filters,
142
- ...options.search ? { search: { query: options.search, fields: [] } } : {}
143
- });
144
- rows.push(...result.data);
145
- options.onProgress?.(rows.length);
146
- if (rows.length >= max) {
147
- truncated = rows.length > max || result.total > max;
148
- rows.length = Math.min(rows.length, max);
149
- break;
150
- }
151
- if (result.data.length < perPage || rows.length >= result.total) break;
152
- }
153
- return { csv: toCsv(options.fields, rows), rows: rows.length, truncated };
154
- }
155
- function parseCsv(text) {
156
- const records = [];
157
- let record = [];
158
- let field = "";
159
- let quoted = false;
160
- const input = text.replace(/^\uFEFF/, "");
161
- for (let i = 0; i < input.length; i++) {
162
- const ch = input[i];
163
- if (quoted) {
164
- if (ch === '"' && input[i + 1] === '"') {
165
- field += '"';
166
- i++;
167
- } else if (ch === '"') quoted = false;
168
- else field += ch;
169
- } else if (ch === '"' && field === "") quoted = true;
170
- else if (ch === ",") {
171
- record.push(field);
172
- field = "";
173
- } else if (ch === "\n" || ch === "\r") {
174
- if (ch === "\r" && input[i + 1] === "\n") i++;
175
- record.push(field);
176
- records.push(record);
177
- record = [];
178
- field = "";
179
- } else field += ch;
180
- }
181
- if (field !== "" || record.length) {
182
- record.push(field);
183
- records.push(record);
184
- }
185
- const nonEmpty = records.filter((r) => r.some((v) => v !== ""));
186
- const [header = [], ...rows] = nonEmpty;
187
- return { header: header.map((h) => h.trim()), rows };
188
- }
118
+ import { csvField, exportCsv, parseCsv, toCsv } from "@marktiderman/genesis-core/csv";
189
119
  function downloadText(filename, text, type = "text/csv;charset=utf-8") {
190
120
  const url = URL.createObjectURL(new Blob([text], { type }));
191
121
  const link = document.createElement("a");