web-utility-streams 1.0.0 → 1.1.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,18 @@
1
+ import { CsvEncoder } from "./CsvEncoderStream.mjs";
2
+ import { t as TableEncoder } from "./TableEncoder-DlOt86Az.mjs";
3
+ // src/CsvObjectEncoderStream.ts
4
+ var CsvObjectEncoderStream = class extends TransformStream {
5
+ constructor(columns, options = {}) {
6
+ const tableEncoder = new TableEncoder(columns, options);
7
+ const encoder = new CsvEncoder(options);
8
+ super({
9
+ start(controller) {
10
+ if (tableEncoder.headers) controller.enqueue(encoder.formatRow(tableEncoder.headers));
11
+ },
12
+ transform(chunk, controller) {
13
+ controller.enqueue(encoder.formatRow(tableEncoder.row(chunk)));
14
+ }
15
+ });
16
+ }
17
+ };
18
+ export { CsvObjectEncoderStream as default };
@@ -0,0 +1,11 @@
1
+ // src/internal/CsvOptions.ts
2
+ /**
3
+ * Check that a CSV option (delimiter, quote or escape) is a single character
4
+ * that isn't a line break.
5
+ */
6
+ function getCsvChar(className, name, value) {
7
+ if (typeof value !== "string" || value.length !== 1) throw new TypeError(`${className}: ${name} must be a single character, got ${JSON.stringify(value)}`);
8
+ if (value === "\r" || value === "\n") throw new TypeError(`${className}: ${name} cannot be a line break`);
9
+ return value;
10
+ }
11
+ export { getCsvChar as t };
@@ -0,0 +1,123 @@
1
+ // src/internal/TableDecoder.ts
2
+ /**
3
+ * Used in the column index map for headers that appear more than once.
4
+ */
5
+ const DUPLICATE = -1;
6
+ var TableDecoder = class {
7
+ _getObject;
8
+ _headers;
9
+ _index = 0;
10
+ _onHeaders;
11
+ _onDuplicateColumn;
12
+ _onMissingColumn;
13
+ _onInvalidCell;
14
+ _onInvalidRow;
15
+ constructor(getObject, options) {
16
+ this._getObject = getObject;
17
+ this._onHeaders = options?.onHeaders;
18
+ this._onDuplicateColumn = options?.onDuplicateColumn;
19
+ this._onMissingColumn = options?.onMissingColumn;
20
+ this._onInvalidCell = options?.onInvalidCell;
21
+ this._onInvalidRow = options?.onInvalidRow;
22
+ }
23
+ transform(chunk, controller) {
24
+ if (!this._headers) {
25
+ this._headers = new Headers(chunk);
26
+ this._onHeaders?.(this._headers.names);
27
+ return;
28
+ }
29
+ const row = new Row(this._index++, this._headers, chunk);
30
+ let obj;
31
+ try {
32
+ if (chunk.length > this._headers.names.length) throw new Error(`Expected ${this._headers.names.length} columns, got ${chunk.length}`);
33
+ obj = this._getObject(row);
34
+ } catch (err) {
35
+ if (row.columnError && row.columnError.err === err) {
36
+ if (row.columnError.code === "Duplicate" && this._onDuplicateColumn) {
37
+ this._onDuplicateColumn(row.columnError.columnName, controller);
38
+ return;
39
+ } else if (row.columnError.code === "Missing" && this._onMissingColumn) {
40
+ this._onMissingColumn(row.columnError.columnName, controller);
41
+ return;
42
+ } else if (row.columnError.code === "Invalid") {
43
+ if (this._onInvalidCell) {
44
+ const reason = row.columnError.err.cause ?? row.columnError.err;
45
+ this._onInvalidCell(row.index, row.columnError.columnName, reason instanceof Error ? reason : new Error(String(reason)), controller);
46
+ return;
47
+ }
48
+ }
49
+ }
50
+ if (this._onInvalidRow) this._onInvalidRow(row.index, err instanceof Error ? err : new Error(String(err)), controller);
51
+ else throw new Error(`Error decoding row ${row.index}: ${err instanceof Error ? err.message : String(err)}`, { cause: err });
52
+ return;
53
+ }
54
+ controller.enqueue(obj);
55
+ }
56
+ };
57
+ var Headers = class {
58
+ names;
59
+ indexes;
60
+ constructor(names) {
61
+ this.names = names;
62
+ const indexes = /* @__PURE__ */ new Map();
63
+ names.forEach((name, i) => {
64
+ if (indexes.has(name)) indexes.set(name, DUPLICATE);
65
+ else indexes.set(name, i);
66
+ });
67
+ this.indexes = indexes;
68
+ }
69
+ };
70
+ var Row = class {
71
+ index;
72
+ columnError;
73
+ _headers;
74
+ _cells;
75
+ constructor(index, headers, cells) {
76
+ this.index = index;
77
+ this._headers = headers;
78
+ this._cells = cells;
79
+ }
80
+ _throwColumn(code, columnName, cause) {
81
+ const err = new Error(`${code} column: ${columnName}`, { cause });
82
+ this.columnError = {
83
+ code,
84
+ columnName,
85
+ err
86
+ };
87
+ throw err;
88
+ }
89
+ get(columnName) {
90
+ const value = this.getOptional(columnName);
91
+ if (value === void 0) return this._throwColumn("Missing", columnName);
92
+ return value;
93
+ }
94
+ getOptional(columnName) {
95
+ const i = this._headers.indexes.get(columnName);
96
+ if (i === void 0) return void 0;
97
+ if (i === DUPLICATE) return this._throwColumn("Duplicate", columnName);
98
+ return this._cells[i] ?? "";
99
+ }
100
+ _parse(columnName, value, parser) {
101
+ try {
102
+ return typeof parser === "function" ? parser(value) : parser.parse(value);
103
+ } catch (err) {
104
+ return this._throwColumn("Invalid", columnName, err);
105
+ }
106
+ }
107
+ parse(columnName, parser) {
108
+ const value = this.get(columnName);
109
+ return this._parse(columnName, value, parser);
110
+ }
111
+ parseOptional(columnName, parser) {
112
+ const value = this.getOptional(columnName);
113
+ if (value === void 0) return void 0;
114
+ return this._parse(columnName, value, parser);
115
+ }
116
+ toObject() {
117
+ return Object.fromEntries(this._headers.names.map((columnName, i) => {
118
+ if (this._headers.indexes.get(columnName) === DUPLICATE) return this._throwColumn("Duplicate", columnName);
119
+ return [columnName, this._cells[i] ?? ""];
120
+ }));
121
+ }
122
+ };
123
+ export { TableDecoder as t };
@@ -0,0 +1,46 @@
1
+ type TableCellParser<T> = ((value: string) => T) | {
2
+ readonly parse: (value: string) => T;
3
+ };
4
+ interface TableRow<TColumnName extends string = string> {
5
+ /**
6
+ * The 0-based index of this row, not counting the header row. This is not
7
+ * the line number, as quoted fields can contain line breaks.
8
+ */
9
+ readonly index: number;
10
+ /**
11
+ * Get the value of a column. Throws if the column is not in the headers, or
12
+ * appears more than once.
13
+ */
14
+ readonly get: (columnName: TColumnName) => string;
15
+ /**
16
+ * Get the value of a column, or `undefined` if the column is not in the
17
+ * headers. Throws if the column appears more than once.
18
+ */
19
+ readonly getOptional: (columnName: TColumnName) => string | undefined;
20
+ /**
21
+ * Get the value of a column (as with `get`), and parse it. If the parser
22
+ * throws, the cell is treated as invalid.
23
+ */
24
+ readonly parse: <T>(columnName: TColumnName, parser: TableCellParser<T>) => T;
25
+ /**
26
+ * Get the value of a column (as with `getOptional`), and parse it if the
27
+ * column exists. If the parser throws, the cell is treated as invalid.
28
+ */
29
+ readonly parseOptional: <T>(columnName: TColumnName, parser: TableCellParser<T>) => T | undefined;
30
+ /**
31
+ * Get the row as an object, keyed by column name. Throws if any column
32
+ * appears more than once.
33
+ */
34
+ readonly toObject: () => Record<TColumnName, string>;
35
+ }
36
+ interface TableDecoderOptions<TObject> {
37
+ /**
38
+ * Called with the header row, before any objects are emitted.
39
+ */
40
+ onHeaders?: (headers: readonly string[]) => void;
41
+ onDuplicateColumn?: (columnName: string, controller: TransformStreamDefaultController<TObject>) => void;
42
+ onMissingColumn?: (columnName: string, controller: TransformStreamDefaultController<TObject>) => void;
43
+ onInvalidCell?: (rowNumber: number, columnName: string, reason: Error, controller: TransformStreamDefaultController<TObject>) => void;
44
+ onInvalidRow?: (rowNumber: number, reason: Error, controller: TransformStreamDefaultController<TObject>) => void;
45
+ }
46
+ export { TableDecoderOptions as n, TableRow as r, TableCellParser as t };
@@ -0,0 +1,6 @@
1
+ import { n as TableDecoderOptions, r as TableRow, t as TableCellParser } from "./TableDecoder-VilUaURR.mjs";
2
+ export interface TableDecoderStreamOptions<TObject> extends TableDecoderOptions<TObject> {}
3
+ export default class TableDecoderStream<TObject> extends TransformStream<readonly string[], TObject> {
4
+ constructor(getObject: (row: TableRow) => TObject, options?: TableDecoderStreamOptions<TObject>);
5
+ }
6
+ export type { TableCellParser, TableRow };
@@ -0,0 +1,11 @@
1
+ import { t as TableDecoder } from "./TableDecoder-CUD0If2r.mjs";
2
+ // src/TableDecoderStream.ts
3
+ var TableDecoderStream = class extends TransformStream {
4
+ constructor(getObject, options = {}) {
5
+ const tableDecoder = new TableDecoder(getObject, options);
6
+ super({ transform(chunk, controller) {
7
+ tableDecoder.transform(chunk, controller);
8
+ } });
9
+ }
10
+ };
11
+ export { TableDecoderStream as default };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * A column header, and a function to get that column's value for an object.
3
+ * `index` is the 0-based index of the object, not counting the header row.
4
+ */
5
+ type TableColumn<TObject, TColumnName extends string = string> = readonly [header: TColumnName, getValue: (obj: TObject, index: number) => string];
6
+ interface TableEncoderOptions {
7
+ /**
8
+ * Don't write the header row (e.g. when appending to an existing table).
9
+ */
10
+ skipHeaders?: boolean;
11
+ }
12
+ export { TableEncoderOptions as n, TableColumn as t };
@@ -0,0 +1,28 @@
1
+ // src/internal/TableEncoder.ts
2
+ /**
3
+ * Converts objects into rows (arrays of strings), using a function per
4
+ * column. Shared by `TableEncoderStream` and `CsvObjectEncoderStream`.
5
+ */
6
+ var TableEncoder = class {
7
+ /**
8
+ * The header row, or `undefined` if `skipHeaders` was set
9
+ */
10
+ headers;
11
+ _getters;
12
+ _index = 0;
13
+ constructor(columns, { skipHeaders = false } = {}) {
14
+ this.headers = skipHeaders ? void 0 : columns.map(([header]) => header);
15
+ this._getters = columns.map(([, getValue]) => getValue);
16
+ }
17
+ /**
18
+ * Get the row for the next object
19
+ */
20
+ row(obj) {
21
+ const index = this._index++;
22
+ const getters = this._getters;
23
+ const row = new Array(getters.length);
24
+ for (let i = 0; i < getters.length; i++) row[i] = getters[i](obj, index);
25
+ return row;
26
+ }
27
+ };
28
+ export { TableEncoder as t };
@@ -0,0 +1,6 @@
1
+ import { n as TableEncoderOptions, t as TableColumn } from "./TableEncoder-DCH6bFmO.mjs";
2
+ export interface TableEncoderStreamOptions extends TableEncoderOptions {}
3
+ export default class TableEncoderStream<TObject> extends TransformStream<TObject, string[]> {
4
+ constructor(columns: readonly TableColumn<TObject>[], options?: TableEncoderStreamOptions);
5
+ }
6
+ export type { TableColumn };
@@ -0,0 +1,16 @@
1
+ import { t as TableEncoder } from "./TableEncoder-DlOt86Az.mjs";
2
+ // src/TableEncoderStream.ts
3
+ var TableEncoderStream = class extends TransformStream {
4
+ constructor(columns, options = {}) {
5
+ const tableEncoder = new TableEncoder(columns, options);
6
+ super({
7
+ start(controller) {
8
+ if (tableEncoder.headers) controller.enqueue([...tableEncoder.headers]);
9
+ },
10
+ transform(chunk, controller) {
11
+ controller.enqueue(tableEncoder.row(chunk));
12
+ }
13
+ });
14
+ }
15
+ };
16
+ export { TableEncoderStream as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-utility-streams",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Various utility streams that are very useful for working with web streams.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -16,6 +16,22 @@
16
16
  "types": "./lib/consumers.d.mts",
17
17
  "default": "./lib/consumers.mjs"
18
18
  },
19
+ "./CsvDecoderStream": {
20
+ "types": "./lib/CsvDecoderStream.d.mts",
21
+ "default": "./lib/CsvDecoderStream.mjs"
22
+ },
23
+ "./CsvEncoderStream": {
24
+ "types": "./lib/CsvEncoderStream.d.mts",
25
+ "default": "./lib/CsvEncoderStream.mjs"
26
+ },
27
+ "./CsvObjectDecoderStream": {
28
+ "types": "./lib/CsvObjectDecoderStream.d.mts",
29
+ "default": "./lib/CsvObjectDecoderStream.mjs"
30
+ },
31
+ "./CsvObjectEncoderStream": {
32
+ "types": "./lib/CsvObjectEncoderStream.d.mts",
33
+ "default": "./lib/CsvObjectEncoderStream.mjs"
34
+ },
19
35
  "./DebounceStream": {
20
36
  "types": "./lib/DebounceStream.d.mts",
21
37
  "default": "./lib/DebounceStream.mjs"
@@ -84,6 +100,14 @@
84
100
  "types": "./lib/SplitLinesStream.d.mts",
85
101
  "default": "./lib/SplitLinesStream.mjs"
86
102
  },
103
+ "./TableDecoderStream": {
104
+ "types": "./lib/TableDecoderStream.d.mts",
105
+ "default": "./lib/TableDecoderStream.mjs"
106
+ },
107
+ "./TableEncoderStream": {
108
+ "types": "./lib/TableEncoderStream.d.mts",
109
+ "default": "./lib/TableEncoderStream.mjs"
110
+ },
87
111
  "./TimeoutStream": {
88
112
  "types": "./lib/TimeoutStream.d.mts",
89
113
  "default": "./lib/TimeoutStream.mjs"
@@ -114,12 +138,17 @@
114
138
  "@types/jsonstream": "^0.8.33",
115
139
  "@types/ndjson": "^2.0.4",
116
140
  "@types/node": "^24.19.2",
141
+ "@types/papaparse": "^5.5.2",
117
142
  "@types/split2": "^4.2.3",
118
143
  "bfj": "^9.1.4",
144
+ "csv-parse": "^7.0.3",
145
+ "csv-stringify": "^6.9.0",
119
146
  "eventsource-encoder": "^1.0.2",
120
147
  "eventsource-parser": "^4.1.3",
148
+ "fast-csv": "^5.0.8",
121
149
  "JSONStream": "^1.3.5",
122
150
  "ndjson": "^2.0.0",
151
+ "papaparse": "^5.7.1",
123
152
  "prettier": "^3.10.1",
124
153
  "split2": "^4.2.0",
125
154
  "stream-chain": "^4.4.2",