@contractspec/lib.exporter 1.57.0 → 1.59.0

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.
@@ -0,0 +1,61 @@
1
+ // 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 = [
16
+ ...metaCols.map((k) => escapeCsv(String(payload.meta[k] ?? ""))),
17
+ ...itemCols.map((k) => escapeCsv(String(it[k] ?? "")))
18
+ ];
19
+ lines.push(row.join(","));
20
+ }
21
+ return lines.join(`
22
+ `);
23
+ }
24
+ function toXmlGeneric(payload, options) {
25
+ const root = options?.root ?? "export";
26
+ const itemTag = options?.itemTag ?? "item";
27
+ const metaEntries = Object.entries(payload.meta).map(([k, v]) => ` <${k}>${xml(String(v ?? ""))}</${k}>`).join(`
28
+ `);
29
+ const items = payload.items.map((it) => {
30
+ const attrs = it.id ? ` id="${xml(String(it.id))}"` : "";
31
+ const fields = Object.entries(it).filter(([k]) => k !== "id").map(([k, v]) => ` <${k}>${xml(String(v ?? ""))}</${k}>`).join(`
32
+ `);
33
+ return ` <${itemTag}${attrs}>
34
+ ${fields}
35
+ </${itemTag}>`;
36
+ }).join(`
37
+ `);
38
+ return `<?xml version="1.0" encoding="UTF-8"?>
39
+ <${root}>
40
+ <meta>
41
+ ${metaEntries}
42
+ </meta>
43
+ <items>
44
+ ${items}
45
+ </items>
46
+ </${root}>`;
47
+ }
48
+ function escapeCsv(val) {
49
+ if (val.includes(",") || val.includes('"') || val.includes(`
50
+ `)) {
51
+ return '"' + val.replace(/"/g, '""') + '"';
52
+ }
53
+ return val;
54
+ }
55
+ function xml(val) {
56
+ return val.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
57
+ }
58
+ export {
59
+ toXmlGeneric,
60
+ toCsvGeneric
61
+ };
package/dist/index.d.ts CHANGED
@@ -1,25 +1,22 @@
1
- //#region src/index.d.ts
2
- interface CsvXmlExportItem {
3
- id?: string;
4
- name: string;
5
- qty: number;
6
- unitPrice?: number;
7
- currency?: string;
8
- [key: string]: unknown;
1
+ export interface CsvXmlExportItem {
2
+ id?: string;
3
+ name: string;
4
+ qty: number;
5
+ unitPrice?: number;
6
+ currency?: string;
7
+ [key: string]: unknown;
9
8
  }
10
- type CsvXmlExportMeta = Record<string, string | number | boolean | null | undefined>;
11
- interface CsvXmlExportPayload<TMeta extends CsvXmlExportMeta = CsvXmlExportMeta> {
12
- meta: TMeta;
13
- items: CsvXmlExportItem[];
9
+ export type CsvXmlExportMeta = Record<string, string | number | boolean | null | undefined>;
10
+ export interface CsvXmlExportPayload<TMeta extends CsvXmlExportMeta = CsvXmlExportMeta> {
11
+ meta: TMeta;
12
+ items: CsvXmlExportItem[];
14
13
  }
15
- declare function toCsvGeneric<TMeta extends CsvXmlExportMeta>(payload: CsvXmlExportPayload<TMeta>, options?: {
16
- metaColumns?: string[];
17
- itemColumns?: string[];
14
+ export declare function toCsvGeneric<TMeta extends CsvXmlExportMeta>(payload: CsvXmlExportPayload<TMeta>, options?: {
15
+ metaColumns?: string[];
16
+ itemColumns?: string[];
18
17
  }): string;
19
- declare function toXmlGeneric<TMeta extends CsvXmlExportMeta>(payload: CsvXmlExportPayload<TMeta>, options?: {
20
- root?: string;
21
- itemTag?: string;
18
+ export declare function toXmlGeneric<TMeta extends CsvXmlExportMeta>(payload: CsvXmlExportPayload<TMeta>, options?: {
19
+ root?: string;
20
+ itemTag?: string;
22
21
  }): string;
23
- //#endregion
24
- export { CsvXmlExportItem, CsvXmlExportMeta, CsvXmlExportPayload, toCsvGeneric, toXmlGeneric };
25
22
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";UAAiB,gBAAA;EACf,EAAA;EACA,IAAA;EACA,GAAA;EACA,SAAA;EACA,QAAA;EAAA,CACC,GAAA;AAAA;AAAA,KAGS,gBAAA,GAAmB,MAAA;AAAA,UAKd,mBAAA,eACD,gBAAA,GAAmB,gBAAA;EAEjC,IAAA,EAAM,KAAA;EACN,KAAA,EAAO,gBAAA;AAAA;AAAA,iBAGO,YAAA,eAA2B,gBAAA,CAAA,CACzC,OAAA,EAAS,mBAAA,CAAoB,KAAA,GAC7B,OAAA;EAAY,WAAA;EAAwB,WAAA;AAAA;AAAA,iBAuBtB,YAAA,eAA2B,gBAAA,CAAA,CACzC,OAAA,EAAS,mBAAA,CAAoB,KAAA,GAC7B,OAAA;EAAY,IAAA;EAAe,OAAA;AAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC/B,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,MAAM,gBAAgB,GAAG,MAAM,CACnC,MAAM,EACN,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAC7C,CAAC;AAEF,MAAM,WAAW,mBAAmB,CAClC,KAAK,SAAS,gBAAgB,GAAG,gBAAgB;IAEjD,IAAI,EAAE,KAAK,CAAC;IACZ,KAAK,EAAE,gBAAgB,EAAE,CAAC;CAC3B;AAED,wBAAgB,YAAY,CAAC,KAAK,SAAS,gBAAgB,EACzD,OAAO,EAAE,mBAAmB,CAAC,KAAK,CAAC,EACnC,OAAO,CAAC,EAAE;IAAE,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,GAC3D,MAAM,CAoBR;AAED,wBAAgB,YAAY,CAAC,KAAK,SAAS,gBAAgB,EACzD,OAAO,EAAE,mBAAmB,CAAC,KAAK,CAAC,EACnC,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAC5C,MAAM,CAiBR"}
package/dist/index.js CHANGED
@@ -1,37 +1,62 @@
1
- //#region src/index.ts
1
+ // @bun
2
+ // src/index.ts
2
3
  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");
4
+ const metaCols = options?.metaColumns ?? Object.keys(payload.meta);
5
+ const itemCols = options?.itemColumns ?? [
6
+ "id",
7
+ "name",
8
+ "qty",
9
+ "unitPrice",
10
+ "currency"
11
+ ];
12
+ const header = [...metaCols, ...itemCols];
13
+ const lines = [];
14
+ lines.push(header.join(","));
15
+ for (const it of payload.items) {
16
+ const row = [
17
+ ...metaCols.map((k) => escapeCsv(String(payload.meta[k] ?? ""))),
18
+ ...itemCols.map((k) => escapeCsv(String(it[k] ?? "")))
19
+ ];
20
+ lines.push(row.join(","));
21
+ }
22
+ return lines.join(`
23
+ `);
19
24
  }
20
25
  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
+ const root = options?.root ?? "export";
27
+ const itemTag = options?.itemTag ?? "item";
28
+ const metaEntries = Object.entries(payload.meta).map(([k, v]) => ` <${k}>${xml(String(v ?? ""))}</${k}>`).join(`
29
+ `);
30
+ const items = payload.items.map((it) => {
31
+ const attrs = it.id ? ` id="${xml(String(it.id))}"` : "";
32
+ const fields = Object.entries(it).filter(([k]) => k !== "id").map(([k, v]) => ` <${k}>${xml(String(v ?? ""))}</${k}>`).join(`
33
+ `);
34
+ return ` <${itemTag}${attrs}>
35
+ ${fields}
36
+ </${itemTag}>`;
37
+ }).join(`
38
+ `);
39
+ return `<?xml version="1.0" encoding="UTF-8"?>
40
+ <${root}>
41
+ <meta>
42
+ ${metaEntries}
43
+ </meta>
44
+ <items>
45
+ ${items}
46
+ </items>
47
+ </${root}>`;
26
48
  }
27
49
  function escapeCsv(val) {
28
- if (val.includes(",") || val.includes("\"") || val.includes("\n")) return "\"" + val.replace(/"/g, "\"\"") + "\"";
29
- return val;
50
+ if (val.includes(",") || val.includes('"') || val.includes(`
51
+ `)) {
52
+ return '"' + val.replace(/"/g, '""') + '"';
53
+ }
54
+ return val;
30
55
  }
31
56
  function xml(val) {
32
- return val.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
57
+ return val.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
33
58
  }
34
-
35
- //#endregion
36
- export { toCsvGeneric, toXmlGeneric };
37
- //# sourceMappingURL=index.js.map
59
+ export {
60
+ toXmlGeneric,
61
+ toCsvGeneric
62
+ };
@@ -0,0 +1,61 @@
1
+ // 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 = [
16
+ ...metaCols.map((k) => escapeCsv(String(payload.meta[k] ?? ""))),
17
+ ...itemCols.map((k) => escapeCsv(String(it[k] ?? "")))
18
+ ];
19
+ lines.push(row.join(","));
20
+ }
21
+ return lines.join(`
22
+ `);
23
+ }
24
+ function toXmlGeneric(payload, options) {
25
+ const root = options?.root ?? "export";
26
+ const itemTag = options?.itemTag ?? "item";
27
+ const metaEntries = Object.entries(payload.meta).map(([k, v]) => ` <${k}>${xml(String(v ?? ""))}</${k}>`).join(`
28
+ `);
29
+ const items = payload.items.map((it) => {
30
+ const attrs = it.id ? ` id="${xml(String(it.id))}"` : "";
31
+ const fields = Object.entries(it).filter(([k]) => k !== "id").map(([k, v]) => ` <${k}>${xml(String(v ?? ""))}</${k}>`).join(`
32
+ `);
33
+ return ` <${itemTag}${attrs}>
34
+ ${fields}
35
+ </${itemTag}>`;
36
+ }).join(`
37
+ `);
38
+ return `<?xml version="1.0" encoding="UTF-8"?>
39
+ <${root}>
40
+ <meta>
41
+ ${metaEntries}
42
+ </meta>
43
+ <items>
44
+ ${items}
45
+ </items>
46
+ </${root}>`;
47
+ }
48
+ function escapeCsv(val) {
49
+ if (val.includes(",") || val.includes('"') || val.includes(`
50
+ `)) {
51
+ return '"' + val.replace(/"/g, '""') + '"';
52
+ }
53
+ return val;
54
+ }
55
+ function xml(val) {
56
+ return val.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
57
+ }
58
+ export {
59
+ toXmlGeneric,
60
+ toCsvGeneric
61
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contractspec/lib.exporter",
3
- "version": "1.57.0",
3
+ "version": "1.59.0",
4
4
  "description": "Generic CSV and XML exporters usable across web and mobile.",
5
5
  "keywords": [
6
6
  "contractspec",
@@ -14,20 +14,21 @@
14
14
  "scripts": {
15
15
  "publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
16
16
  "publish:pkg:canary": "bun publish:pkg --tag canary",
17
- "build": "bun build:types && bun build:bundle",
18
- "build:bundle": "tsdown",
19
- "build:types": "tsc --noEmit",
20
- "dev": "bun build:bundle --watch",
17
+ "build": "bun run prebuild && bun run build:bundle && bun run build:types",
18
+ "build:bundle": "contractspec-bun-build transpile",
19
+ "build:types": "contractspec-bun-build types",
20
+ "dev": "contractspec-bun-build dev",
21
21
  "clean": "rimraf dist .turbo",
22
22
  "lint": "bun lint:fix",
23
23
  "lint:fix": "eslint src --fix",
24
- "lint:check": "eslint src"
24
+ "lint:check": "eslint src",
25
+ "prebuild": "contractspec-bun-build prebuild",
26
+ "typecheck": "tsc --noEmit"
25
27
  },
26
28
  "devDependencies": {
27
- "@contractspec/tool.tsdown": "1.57.0",
28
- "@contractspec/tool.typescript": "1.57.0",
29
- "tsdown": "^0.20.3",
30
- "typescript": "^5.9.3"
29
+ "@contractspec/tool.typescript": "1.59.0",
30
+ "typescript": "^5.9.3",
31
+ "@contractspec/tool.bun": "1.58.0"
31
32
  },
32
33
  "types": "./dist/index.d.ts",
33
34
  "files": [
@@ -35,14 +36,18 @@
35
36
  "README.md"
36
37
  ],
37
38
  "exports": {
38
- ".": "./dist/index.js",
39
- "./*": "./*"
39
+ ".": "./src/index.ts"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public",
43
43
  "exports": {
44
- ".": "./dist/index.js",
45
- "./*": "./*"
44
+ ".": {
45
+ "types": "./dist/index.d.ts",
46
+ "bun": "./dist/index.js",
47
+ "node": "./dist/node/index.mjs",
48
+ "browser": "./dist/browser/index.js",
49
+ "default": "./dist/index.js"
50
+ }
46
51
  },
47
52
  "registry": "https://registry.npmjs.org/"
48
53
  },
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["export interface CsvXmlExportItem {\n id?: string;\n name: string;\n qty: number;\n unitPrice?: number;\n currency?: string;\n [key: string]: unknown;\n}\n\nexport type CsvXmlExportMeta = Record<\n string,\n string | number | boolean | null | undefined\n>;\n\nexport interface CsvXmlExportPayload<\n TMeta extends CsvXmlExportMeta = CsvXmlExportMeta,\n> {\n meta: TMeta;\n items: CsvXmlExportItem[];\n}\n\nexport function toCsvGeneric<TMeta extends CsvXmlExportMeta>(\n payload: CsvXmlExportPayload<TMeta>,\n options?: { metaColumns?: string[]; itemColumns?: string[] }\n): string {\n const metaCols = options?.metaColumns ?? Object.keys(payload.meta);\n const itemCols = options?.itemColumns ?? [\n 'id',\n 'name',\n 'qty',\n 'unitPrice',\n 'currency',\n ];\n const header = [...metaCols, ...itemCols];\n const lines: string[] = [];\n lines.push(header.join(','));\n for (const it of payload.items) {\n const row = [\n ...metaCols.map((k) => escapeCsv(String(payload.meta[k] ?? ''))),\n ...itemCols.map((k) => escapeCsv(String(it[k] ?? ''))),\n ];\n lines.push(row.join(','));\n }\n return lines.join('\\n');\n}\n\nexport function toXmlGeneric<TMeta extends CsvXmlExportMeta>(\n payload: CsvXmlExportPayload<TMeta>,\n options?: { root?: string; itemTag?: string }\n): string {\n const root = options?.root ?? 'export';\n const itemTag = options?.itemTag ?? 'item';\n const metaEntries = Object.entries(payload.meta)\n .map(([k, v]) => ` <${k}>${xml(String(v ?? ''))}</${k}>`)\n .join('\\n');\n const items = payload.items\n .map((it) => {\n const attrs = it.id ? ` id=\"${xml(String(it.id))}\"` : '';\n const fields = Object.entries(it)\n .filter(([k]) => k !== 'id')\n .map(([k, v]) => ` <${k}>${xml(String(v ?? ''))}</${k}>`)\n .join('\\n');\n return ` <${itemTag}${attrs}>\\n${fields}\\n </${itemTag}>`;\n })\n .join('\\n');\n return `<?xml version=\"1.0\" encoding=\"UTF-8\"?>\\n<${root}>\\n <meta>\\n${metaEntries}\\n </meta>\\n <items>\\n${items}\\n </items>\\n</${root}>`;\n}\n\nfunction escapeCsv(val: string): string {\n if (val.includes(',') || val.includes('\"') || val.includes('\\n')) {\n return '\"' + val.replace(/\"/g, '\"\"') + '\"';\n }\n return val;\n}\n\nfunction xml(val: string): string {\n return val\n .replace(/&/g, '&amp;')\n .replace(/</g, '&lt;')\n .replace(/>/g, '&gt;')\n .replace(/\"/g, '&quot;')\n .replace(/'/g, '&apos;');\n}\n"],"mappings":";AAqBA,SAAgB,aACd,SACA,SACQ;CACR,MAAM,WAAW,SAAS,eAAe,OAAO,KAAK,QAAQ,KAAK;CAClE,MAAM,WAAW,SAAS,eAAe;EACvC;EACA;EACA;EACA;EACA;EACD;CACD,MAAM,SAAS,CAAC,GAAG,UAAU,GAAG,SAAS;CACzC,MAAM,QAAkB,EAAE;AAC1B,OAAM,KAAK,OAAO,KAAK,IAAI,CAAC;AAC5B,MAAK,MAAM,MAAM,QAAQ,OAAO;EAC9B,MAAM,MAAM,CACV,GAAG,SAAS,KAAK,MAAM,UAAU,OAAO,QAAQ,KAAK,MAAM,GAAG,CAAC,CAAC,EAChE,GAAG,SAAS,KAAK,MAAM,UAAU,OAAO,GAAG,MAAM,GAAG,CAAC,CAAC,CACvD;AACD,QAAM,KAAK,IAAI,KAAK,IAAI,CAAC;;AAE3B,QAAO,MAAM,KAAK,KAAK;;AAGzB,SAAgB,aACd,SACA,SACQ;CACR,MAAM,OAAO,SAAS,QAAQ;CAC9B,MAAM,UAAU,SAAS,WAAW;AAcpC,QAAO,4CAA4C,KAAK,eAbpC,OAAO,QAAQ,QAAQ,KAAK,CAC7C,KAAK,CAAC,GAAG,OAAO,QAAQ,EAAE,GAAG,IAAI,OAAO,KAAK,GAAG,CAAC,CAAC,IAAI,EAAE,GAAG,CAC3D,KAAK,KAAK,CAWsE,0BAVrE,QAAQ,MACnB,KAAK,OAAO;AAMX,SAAO,MAAM,UALC,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,GAAG,CAAC,CAAC,KAAK,GAKzB,KAJd,OAAO,QAAQ,GAAG,CAC9B,QAAQ,CAAC,OAAO,MAAM,KAAK,CAC3B,KAAK,CAAC,GAAG,OAAO,QAAQ,EAAE,GAAG,IAAI,OAAO,KAAK,GAAG,CAAC,CAAC,IAAI,EAAE,GAAG,CAC3D,KAAK,KAAK,CAC4B,QAAQ,QAAQ;GACzD,CACD,KAAK,KAAK,CACsG,kBAAkB,KAAK;;AAG5I,SAAS,UAAU,KAAqB;AACtC,KAAI,IAAI,SAAS,IAAI,IAAI,IAAI,SAAS,KAAI,IAAI,IAAI,SAAS,KAAK,CAC9D,QAAO,OAAM,IAAI,QAAQ,MAAM,OAAK,GAAG;AAEzC,QAAO;;AAGT,SAAS,IAAI,KAAqB;AAChC,QAAO,IACJ,QAAQ,MAAM,QAAQ,CACtB,QAAQ,MAAM,OAAO,CACrB,QAAQ,MAAM,OAAO,CACrB,QAAQ,MAAM,SAAS,CACvB,QAAQ,MAAM,SAAS"}