@cfworker/csv 1.12.3 → 1.12.4

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/encode.d.ts CHANGED
@@ -1,4 +1,9 @@
1
- export interface EncodeOptions {
2
- columns?: Record<string, string>;
1
+ export interface EncodeOptions<T> {
2
+ columns?: Column<T, keyof T>[];
3
3
  }
4
- export declare function encode(rows: any[], { columns }?: EncodeOptions): ReadableStream;
4
+ export interface Column<T, K extends keyof T> {
5
+ key: K;
6
+ label: string;
7
+ format: (value: T[K], row: T) => unknown;
8
+ }
9
+ export declare function encode<T>(rows: T[], { columns }?: EncodeOptions<T>): ReadableStream;
package/dist/encode.js CHANGED
@@ -9,10 +9,11 @@ export function encode(rows, { columns } = {}) {
9
9
  const writer = writable.getWriter();
10
10
  if (rows.length > 0) {
11
11
  if (!columns) {
12
- columns = Object.keys(rows[0]).reduce((p, c) => {
13
- p[c] = c;
14
- return p;
15
- }, {});
12
+ columns = Object.keys(rows[0]).map(key => ({
13
+ key: key,
14
+ label: key,
15
+ format: (x) => x
16
+ }));
16
17
  }
17
18
  await writeRows(writer, rows, columns);
18
19
  }
@@ -24,14 +25,13 @@ async function writeRows(writer, rows, columns) {
24
25
  const text = new TextEncoder();
25
26
  const utf8 = text.encode.bind(text);
26
27
  const write = writer.write.bind(writer);
27
- const keys = Object.keys(columns);
28
- await write(utf8(keys.map(k => encodeValue(columns[k])).join(delimiter)));
28
+ await write(utf8(columns.map(c => encodeValue(c.label)).join(delimiter)));
29
29
  for (const row of rows) {
30
30
  await write(utf8(rowDelimiter));
31
31
  let s = '';
32
32
  let first = true;
33
- for (const key of keys) {
34
- s += (first ? '' : ',') + encodeValue(row[key]);
33
+ for (const { key, format } of columns) {
34
+ s += (first ? '' : ',') + encodeValue(format(row[key], row));
35
35
  first = false;
36
36
  }
37
37
  await write(utf8(s));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cfworker/csv",
3
- "version": "1.12.3",
3
+ "version": "1.12.4",
4
4
  "description": "Streaming CSV encoding for Cloudflare Workers and service workers",
5
5
  "sideEffects": false,
6
6
  "main": "dist/index.js",
package/src/encode.ts CHANGED
@@ -4,11 +4,17 @@ const delimiter = ',';
4
4
  const rowDelimiter = '\r\n';
5
5
  const requiresQuoteRegex = /["\r\n,]/;
6
6
 
7
- export interface EncodeOptions {
7
+ export interface EncodeOptions<T> {
8
8
  /**
9
- * Properties to include. Values will be used as column name.
9
+ * Properties to include
10
10
  */
11
- columns?: Record<string, string>;
11
+ columns?: Column<T, keyof T>[];
12
+ }
13
+
14
+ export interface Column<T, K extends keyof T> {
15
+ key: K;
16
+ label: string;
17
+ format: (value: T[K], row: T) => unknown;
12
18
  }
13
19
 
14
20
  /**
@@ -17,9 +23,9 @@ export interface EncodeOptions {
17
23
  * @param options
18
24
  * @returns
19
25
  */
20
- export function encode(
21
- rows: any[],
22
- { columns }: EncodeOptions = {}
26
+ export function encode<T>(
27
+ rows: T[],
28
+ { columns }: EncodeOptions<T> = {}
23
29
  ): ReadableStream {
24
30
  // Cloudflare Workers cannot construct a ReadableStream
25
31
  const { readable, writable } = new TransformStream();
@@ -27,13 +33,11 @@ export function encode(
27
33
  const writer = writable.getWriter();
28
34
  if (rows.length > 0) {
29
35
  if (!columns) {
30
- columns = Object.keys(rows[0]).reduce<Record<string, string>>(
31
- (p, c) => {
32
- p[c] = c;
33
- return p;
34
- },
35
- {}
36
- );
36
+ columns = Object.keys(rows[0]).map(key => ({
37
+ key: key as keyof T,
38
+ label: key,
39
+ format: (x: any) => x
40
+ }));
37
41
  }
38
42
  await writeRows(writer, rows, columns);
39
43
  }
@@ -43,17 +47,16 @@ export function encode(
43
47
  return readable;
44
48
  }
45
49
 
46
- async function writeRows(
50
+ async function writeRows<T>(
47
51
  writer: WritableStreamDefaultWriter,
48
- rows: any[],
49
- columns: Record<string, string>
52
+ rows: T[],
53
+ columns: Column<T, keyof T>[]
50
54
  ) {
51
55
  const text = new TextEncoder();
52
56
  const utf8 = text.encode.bind(text);
53
57
  const write = writer.write.bind(writer);
54
- const keys = Object.keys(columns);
55
58
  // write header row
56
- await write(utf8(keys.map(k => encodeValue(columns[k])).join(delimiter)));
59
+ await write(utf8(columns.map(c => encodeValue(c.label)).join(delimiter)));
57
60
 
58
61
  for (const row of rows) {
59
62
  // write row delimiter
@@ -62,8 +65,8 @@ async function writeRows(
62
65
  // write data row
63
66
  let s = '';
64
67
  let first = true;
65
- for (const key of keys) {
66
- s += (first ? '' : ',') + encodeValue(row[key]);
68
+ for (const { key, format } of columns) {
69
+ s += (first ? '' : ',') + encodeValue(format(row[key], row));
67
70
  first = false;
68
71
  }
69
72
  await write(utf8(s));