@lssm/lib.exporter 0.0.0-canary-20251217063201 → 0.0.0-canary-20251217072406

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.
Files changed (2) hide show
  1. package/dist/index.js +36 -6
  2. package/package.json +3 -2
package/dist/index.js CHANGED
@@ -1,6 +1,36 @@
1
- function e(e,t){let r=t?.metaColumns??Object.keys(e.meta),i=t?.itemColumns??[`id`,`name`,`qty`,`unitPrice`,`currency`],a=[...r,...i],o=[];o.push(a.join(`,`));for(let t of e.items){let a=[...r.map(t=>n(String(e.meta[t]??``))),...i.map(e=>n(String(t[e]??``)))];o.push(a.join(`,`))}return o.join(`
2
- `)}function t(e,t){let n=t?.root??`export`,i=t?.itemTag??`item`;return`<?xml version="1.0" encoding="UTF-8"?>\n<${n}>\n <meta>\n${Object.entries(e.meta).map(([e,t])=>` <${e}>${r(String(t??``))}</${e}>`).join(`
3
- `)}\n </meta>\n <items>\n${e.items.map(e=>` <${i}${e.id?` id="${r(String(e.id))}"`:``}>\n${Object.entries(e).filter(([e])=>e!==`id`).map(([e,t])=>` <${e}>${r(String(t??``))}</${e}>`).join(`
4
- `)}\n </${i}>`).join(`
5
- `)}\n </items>\n</${n}>`}function n(e){return e.includes(`,`)||e.includes(`"`)||e.includes(`
6
- `)?`"`+e.replace(/"/g,`""`)+`"`:e}function r(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`).replace(/"/g,`&quot;`).replace(/'/g,`&apos;`)}export{e as toCsvGeneric,t as toXmlGeneric};
1
+ //#region src/index.ts
2
+ function toCsvGeneric(payload, options) {
3
+ const metaCols = options?.metaColumns ?? Object.keys(payload.meta);
4
+ const itemCols = options?.itemColumns ?? [
5
+ "id",
6
+ "name",
7
+ "qty",
8
+ "unitPrice",
9
+ "currency"
10
+ ];
11
+ const header = [...metaCols, ...itemCols];
12
+ const lines = [];
13
+ lines.push(header.join(","));
14
+ for (const it of payload.items) {
15
+ const row = [...metaCols.map((k) => escapeCsv(String(payload.meta[k] ?? ""))), ...itemCols.map((k) => escapeCsv(String(it[k] ?? "")))];
16
+ lines.push(row.join(","));
17
+ }
18
+ return lines.join("\n");
19
+ }
20
+ function toXmlGeneric(payload, options) {
21
+ const root = options?.root ?? "export";
22
+ const itemTag = options?.itemTag ?? "item";
23
+ return `<?xml version="1.0" encoding="UTF-8"?>\n<${root}>\n <meta>\n${Object.entries(payload.meta).map(([k, v]) => ` <${k}>${xml(String(v ?? ""))}</${k}>`).join("\n")}\n </meta>\n <items>\n${payload.items.map((it) => {
24
+ return ` <${itemTag}${it.id ? ` id="${xml(String(it.id))}"` : ""}>\n${Object.entries(it).filter(([k]) => k !== "id").map(([k, v]) => ` <${k}>${xml(String(v ?? ""))}</${k}>`).join("\n")}\n </${itemTag}>`;
25
+ }).join("\n")}\n </items>\n</${root}>`;
26
+ }
27
+ function escapeCsv(val) {
28
+ if (val.includes(",") || val.includes("\"") || val.includes("\n")) return "\"" + val.replace(/"/g, "\"\"") + "\"";
29
+ return val;
30
+ }
31
+ function xml(val) {
32
+ return val.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
33
+ }
34
+
35
+ //#endregion
36
+ export { toCsvGeneric, toXmlGeneric };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lssm/lib.exporter",
3
- "version": "0.0.0-canary-20251217063201",
3
+ "version": "0.0.0-canary-20251217072406",
4
4
  "description": "Generic CSV and XML exporters usable across web and mobile.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -16,7 +16,8 @@
16
16
  "lint:check": "eslint src"
17
17
  },
18
18
  "devDependencies": {
19
- "@lssm/tool.typescript": "0.0.0-canary-20251217063201",
19
+ "@lssm/tool.tsdown": "0.0.0-canary-20251217072406",
20
+ "@lssm/tool.typescript": "0.0.0-canary-20251217072406",
20
21
  "tsdown": "^0.17.4",
21
22
  "typescript": "^5.9.3"
22
23
  },