@konstit/csv 0.5.0 → 0.5.1
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/README.md +9 -3
- package/dist/api.js +20 -94
- package/dist/batches.d.ts +3 -4
- package/dist/batches.js +12 -10
- package/dist/cli-command.js +42 -77
- package/dist/cli-schema.d.ts +21 -5
- package/dist/cli-schema.js +37 -22
- package/dist/file-stream.d.ts +7 -3
- package/dist/file-stream.js +13 -12
- package/dist/files.js +33 -76
- package/dist/filter.d.ts +1 -2
- package/dist/index.d.ts +1 -1
- package/dist/inspect-file.d.ts +16 -6
- package/dist/inspect-file.js +67 -17
- package/dist/latin1-rows.d.ts +2 -3
- package/dist/latin1-rows.js +13 -3
- package/dist/native.d.ts +21 -69
- package/dist/native.js +9 -83
- package/dist/normalize.d.ts +1 -40
- package/dist/normalize.js +25 -62
- package/dist/parser.d.ts +17 -202
- package/dist/parser.js +131 -230
- package/dist/strict-schema.d.ts +1 -1
- package/dist/strict-schema.js +4 -3
- package/dist/types.d.ts +37 -5
- package/dist/zip-reader.d.ts +2 -0
- package/dist/zip-reader.js +31 -21
- package/package.json +3 -2
- package/prebuilds/darwin-arm64/libcsv_native.dylib +0 -0
- package/prebuilds/darwin-x64/libcsv_native.dylib +0 -0
- package/prebuilds/linux-x64/libcsv_native.so +0 -0
- package/prebuilds/manifest.json +8 -8
- package/prebuilds/provenance/darwin-arm64.json +1 -1
- package/prebuilds/provenance/darwin-x64.json +1 -1
- package/prebuilds/provenance/linux-x64.json +1 -1
package/README.md
CHANGED
|
@@ -87,6 +87,9 @@ for await (const batch of csv.rows('data.csv')) {
|
|
|
87
87
|
Each iteration yields a **batch of rows** in file order, so use the inner loop to process individual records. Process
|
|
88
88
|
batches as they arrive to avoid collecting the complete file in memory. The header is returned as the first row.
|
|
89
89
|
|
|
90
|
+
Pass `{ skipRows: 1, limit: 10 }` to skip the header and emit up to ten rows. With a filter, `skipRows` counts
|
|
91
|
+
input records and `limit` counts matches across all batches.
|
|
92
|
+
|
|
90
93
|
To count records without materializing rows:
|
|
91
94
|
|
|
92
95
|
```ts
|
|
@@ -199,16 +202,19 @@ Inspect a file, count its records, or emit the first records as newline-delimite
|
|
|
199
202
|
|
|
200
203
|
```sh
|
|
201
204
|
bunx @konstit/csv probe data.csv
|
|
205
|
+
bunx @konstit/csv probe tables.zip --zip-entry parts.tsv --json
|
|
202
206
|
|
|
203
207
|
bunx @konstit/csv count data.csv
|
|
204
208
|
# 3
|
|
205
209
|
|
|
206
|
-
bunx @konstit/csv lines data.csv --json --limit 2
|
|
207
|
-
# ["id","name","state"]
|
|
210
|
+
bunx @konstit/csv lines data.csv --json --skip-rows 1 --limit 2
|
|
208
211
|
# ["1","Ada","SP"]
|
|
212
|
+
# ["2","Grace","RJ"]
|
|
209
213
|
```
|
|
210
214
|
|
|
211
|
-
The CLI detects input encoding and delimiter by default.
|
|
215
|
+
The CLI detects input encoding and delimiter by default. `probe --json` reports the file size in bytes,
|
|
216
|
+
how much of the decompressed payload it inspected, and whether it reached the end. Use `--delimiter` or
|
|
217
|
+
`--encoding` to inspect a known format. Without `--json`, `lines` writes normalized UTF-8 CSV.
|
|
212
218
|
`count` and `lines` support column filters; `lines` also supports
|
|
213
219
|
column selection. Use `bunx @konstit/csv lines --help` or read the
|
|
214
220
|
[CLI guide](https://github.com/Konstit-Engenharia/csv-parser/blob/main/docs/cli.md) for flags and output behavior.
|
package/dist/api.js
CHANGED
|
@@ -23,11 +23,13 @@ import {
|
|
|
23
23
|
resolveInputEncoding
|
|
24
24
|
} from "./normalize.js";
|
|
25
25
|
import { snapshotCsvOptions } from "./options-snapshot.js";
|
|
26
|
-
import { createPreparedParser } from "./parser.js";
|
|
27
26
|
import {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
countFileChunks,
|
|
28
|
+
createPreparedParser,
|
|
29
|
+
parseFileChunks,
|
|
30
|
+
validateRowSelection
|
|
31
|
+
} from "./parser.js";
|
|
32
|
+
import { rejectStrictSchemaUnsupported } from "./strict-schema.js";
|
|
31
33
|
export async function parse(buffer, suppliedOptions = {}) {
|
|
32
34
|
const options = snapshotCsvOptions(suppliedOptions);
|
|
33
35
|
if ("compression" in options && options.compression !== void 0)
|
|
@@ -40,11 +42,8 @@ export async function parse(buffer, suppliedOptions = {}) {
|
|
|
40
42
|
const projected = filters !== void 0 || options.strict !== !0 && columns !== void 0;
|
|
41
43
|
const input = prepareCsvBufferInput(buffer, options.encoding, options.delimiter);
|
|
42
44
|
using parser = createPreparedParser(toParserOptions(options, input.parserEncoding), projected ? columns : void 0, filters);
|
|
43
|
-
const validator = strictSchemaValidator(options);
|
|
44
45
|
const batch = parser.writeBatch(input.bytes, !0);
|
|
45
46
|
try {
|
|
46
|
-
validator?.validateBatch(batch);
|
|
47
|
-
validator?.finish();
|
|
48
47
|
return materializeRows(batch, columns, projected);
|
|
49
48
|
} finally {
|
|
50
49
|
batch.close();
|
|
@@ -60,44 +59,18 @@ export async function* iterateRows(path, options, probeFormat) {
|
|
|
60
59
|
const projected = filters !== void 0 || options.strict !== !0 && columns !== void 0;
|
|
61
60
|
await using input = await prepareCsvFileInput(path, options, "read-ahead", probeFormat);
|
|
62
61
|
const parserOptions = toParserOptions(options, input.parserEncoding, input.delimiter);
|
|
63
|
-
const validator = strictSchemaValidator(options);
|
|
64
62
|
const reusableRows = options.reuseArrays === !0 ? [] : void 0;
|
|
65
|
-
if (options.strict !== !0 &&
|
|
63
|
+
if (options.strict !== !0 && input.parserEncoding === "latin1") {
|
|
64
|
+
validateRowSelection(options);
|
|
66
65
|
const fixedColumns = normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
|
|
67
66
|
const createParser = latin1RowsParser(fixedColumns);
|
|
68
|
-
|
|
67
|
+
const chunks = options.limit === 0 ? [] : input.chunks();
|
|
68
|
+
yield* latin1Rows(chunks, input.delimiter, columns, filters, createParser, reusableRows, parserOptions);
|
|
69
69
|
return;
|
|
70
70
|
}
|
|
71
71
|
using parser = createPreparedParser(parserOptions, projected ? columns : void 0, filters);
|
|
72
|
-
for await (
|
|
73
|
-
|
|
74
|
-
if (batch.rowCount > 0)
|
|
75
|
-
try {
|
|
76
|
-
validator?.validateBatch(batch);
|
|
77
|
-
const values = materializeRows(batch, columns, projected, reusableRows);
|
|
78
|
-
if (values.length > 0)
|
|
79
|
-
yield values;
|
|
80
|
-
} finally {
|
|
81
|
-
batch.close();
|
|
82
|
-
}
|
|
83
|
-
else
|
|
84
|
-
batch.close();
|
|
85
|
-
}
|
|
86
|
-
const batch = parser.endBatch();
|
|
87
|
-
if (batch.rowCount > 0)
|
|
88
|
-
try {
|
|
89
|
-
validator?.validateBatch(batch);
|
|
90
|
-
validator?.finish();
|
|
91
|
-
const values = materializeRows(batch, columns, projected, reusableRows);
|
|
92
|
-
if (values.length > 0)
|
|
93
|
-
yield values;
|
|
94
|
-
} finally {
|
|
95
|
-
batch.close();
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
validator?.finish();
|
|
99
|
-
batch.close();
|
|
100
|
-
}
|
|
72
|
+
for await (using batch of parseFileChunks(input, parser, options.limit))
|
|
73
|
+
yield materializeRows(batch, columns, projected, reusableRows);
|
|
101
74
|
}
|
|
102
75
|
export function batches(path, options = {}) {
|
|
103
76
|
return iterateBatches(path, snapshotCsvOptions(options));
|
|
@@ -110,34 +83,7 @@ async function* iterateBatches(path, options) {
|
|
|
110
83
|
throw Error("strict CSV validation is not supported for projected batches; use csv.rows() for strict projection");
|
|
111
84
|
await using input = await prepareCsvFileInput(path, options);
|
|
112
85
|
using parser = createPreparedParser(toParserOptions(options, input.parserEncoding, input.delimiter), columns, filters);
|
|
113
|
-
|
|
114
|
-
for await (const chunk of input.chunks()) {
|
|
115
|
-
const batch = parser.writeBatch(chunk);
|
|
116
|
-
if (batch.rowCount > 0) {
|
|
117
|
-
try {
|
|
118
|
-
validator?.validateBatch(batch);
|
|
119
|
-
} catch (error) {
|
|
120
|
-
batch.close();
|
|
121
|
-
throw error;
|
|
122
|
-
}
|
|
123
|
-
yield batch;
|
|
124
|
-
} else
|
|
125
|
-
batch.close();
|
|
126
|
-
}
|
|
127
|
-
const batch = parser.endBatch();
|
|
128
|
-
if (batch.rowCount > 0) {
|
|
129
|
-
try {
|
|
130
|
-
validator?.validateBatch(batch);
|
|
131
|
-
validator?.finish();
|
|
132
|
-
} catch (error) {
|
|
133
|
-
batch.close();
|
|
134
|
-
throw error;
|
|
135
|
-
}
|
|
136
|
-
yield batch;
|
|
137
|
-
} else {
|
|
138
|
-
batch.close();
|
|
139
|
-
validator?.finish();
|
|
140
|
-
}
|
|
86
|
+
yield* parseFileChunks(input, parser, options.limit);
|
|
141
87
|
}
|
|
142
88
|
export async function count(path, suppliedOptions = {}) {
|
|
143
89
|
const options = snapshotCsvOptions(suppliedOptions);
|
|
@@ -146,32 +92,7 @@ export async function count(path, suppliedOptions = {}) {
|
|
|
146
92
|
if (options.strict === !0 && filters !== void 0)
|
|
147
93
|
throw Error("strict CSV validation is not supported for count filters");
|
|
148
94
|
using parser = createPreparedParser(toParserOptions(options, input.parserEncoding, input.delimiter), void 0, filters);
|
|
149
|
-
|
|
150
|
-
let total = 0;
|
|
151
|
-
if (options.strict === !0 && filters === void 0) {
|
|
152
|
-
for await (const chunk of input.chunks()) {
|
|
153
|
-
const batch = parser.writeBatch(chunk);
|
|
154
|
-
try {
|
|
155
|
-
validator?.validateBatch(batch);
|
|
156
|
-
total += batch.rowCount;
|
|
157
|
-
} finally {
|
|
158
|
-
batch.close();
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
const batch = parser.endBatch();
|
|
162
|
-
try {
|
|
163
|
-
validator?.validateBatch(batch);
|
|
164
|
-
validator?.finish();
|
|
165
|
-
total += batch.rowCount;
|
|
166
|
-
} finally {
|
|
167
|
-
batch.close();
|
|
168
|
-
}
|
|
169
|
-
return total;
|
|
170
|
-
}
|
|
171
|
-
for await (const chunk of input.chunks())
|
|
172
|
-
total += parser.writeCount(chunk);
|
|
173
|
-
total += parser.endCount();
|
|
174
|
-
return total;
|
|
95
|
+
return await countFileChunks(input, parser, options);
|
|
175
96
|
}
|
|
176
97
|
function findCsvSafeSplitOffsets(path, shardCount, suppliedOptions = {}) {
|
|
177
98
|
const options = snapshotCsvOptions(suppliedOptions);
|
|
@@ -207,7 +128,12 @@ function toParserOptions(options, encoding, delimiter = options.delimiter) {
|
|
|
207
128
|
encoding,
|
|
208
129
|
strict: options.strict,
|
|
209
130
|
columns: options.columns,
|
|
210
|
-
fixedColumns: options.fixedColumns
|
|
131
|
+
fixedColumns: options.fixedColumns,
|
|
132
|
+
skipRows: options.skipRows,
|
|
133
|
+
limit: options.limit,
|
|
134
|
+
expectedHeaders: options.expectedHeaders,
|
|
135
|
+
requireHeader: options.requireHeader,
|
|
136
|
+
minDataRows: options.minDataRows
|
|
211
137
|
};
|
|
212
138
|
}
|
|
213
139
|
function materializeRows(batch, columns, projected, target = []) {
|
package/dist/batches.d.ts
CHANGED
|
@@ -83,8 +83,7 @@ export declare class NativeCsvRowView {
|
|
|
83
83
|
*
|
|
84
84
|
* Missing fields are represented by empty strings, matching `rowsInto()`.
|
|
85
85
|
*
|
|
86
|
-
* @param columns Zero-based field indexes. Duplicate indexes are allowed
|
|
87
|
-
* this low-level accessor.
|
|
86
|
+
* @param columns Zero-based field indexes captured before the first read. Duplicate indexes are allowed.
|
|
88
87
|
* @returns A newly allocated array of decoded UTF-8 strings.
|
|
89
88
|
*/
|
|
90
89
|
pick(columns: CsvColumns): string[];
|
|
@@ -148,7 +147,7 @@ export declare class NativeCsvBatch {
|
|
|
148
147
|
* missing field is represented by `''`.
|
|
149
148
|
*
|
|
150
149
|
* @param target Array to fill. It is truncated or extended to `rowCount`.
|
|
151
|
-
* @param columns Optional zero-based field indexes
|
|
150
|
+
* @param columns Optional zero-based field indexes captured before writing to the target.
|
|
152
151
|
* Duplicate indexes are rejected.
|
|
153
152
|
* @returns The same `target` array passed by the caller.
|
|
154
153
|
* @throws {RangeError} If a column index is invalid, repeated, or an offset
|
|
@@ -259,7 +258,7 @@ export declare class NativeCsvBatch {
|
|
|
259
258
|
* reported as `[-1, -1]`. Both `ranges` and `data` are reused/borrowed and
|
|
260
259
|
* must be consumed synchronously by the callback.
|
|
261
260
|
*
|
|
262
|
-
* @param columns Zero-based field indexes relative to each row.
|
|
261
|
+
* @param columns Zero-based field indexes relative to each row, copied once at the start of the scan.
|
|
263
262
|
* @param callback Receives `(rowIndex, ranges, data)` for each row.
|
|
264
263
|
* @param startRow Inclusive first row; defaults to `0`.
|
|
265
264
|
* @param endRow Exclusive end row; defaults to `rowCount`.
|
package/dist/batches.js
CHANGED
|
@@ -67,10 +67,11 @@ export class NativeCsvRowView {
|
|
|
67
67
|
}
|
|
68
68
|
pick(columns) {
|
|
69
69
|
this.#requireActive();
|
|
70
|
+
const selectedColumns = [...columns];
|
|
70
71
|
const values = [];
|
|
71
|
-
values.length =
|
|
72
|
-
for (let index = 0;index <
|
|
73
|
-
values[index] = this.get(
|
|
72
|
+
values.length = selectedColumns.length;
|
|
73
|
+
for (let index = 0;index < selectedColumns.length; ++index)
|
|
74
|
+
values[index] = this.get(selectedColumns[index] ?? 0) ?? "";
|
|
74
75
|
return values;
|
|
75
76
|
}
|
|
76
77
|
#rowRange(rowIndex) {
|
|
@@ -138,7 +139,7 @@ export class NativeCsvBatch {
|
|
|
138
139
|
NativeCsvRowView[visitBatchRows](this, callback);
|
|
139
140
|
}
|
|
140
141
|
rowsInto(target, columns) {
|
|
141
|
-
normalizeColumns(columns);
|
|
142
|
+
const selectedColumns = normalizeColumns(columns);
|
|
142
143
|
const rowCount = this.rowCount;
|
|
143
144
|
const totalFields = this.totalFields;
|
|
144
145
|
const rowOffsetWords = this.#rowOffsetWordsView();
|
|
@@ -151,9 +152,9 @@ export class NativeCsvBatch {
|
|
|
151
152
|
const fieldEnd = offsetAt(rowOffsetWords, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
|
|
152
153
|
const existing = target[rowIndex];
|
|
153
154
|
const row = existing === void 0 ? [] : existing;
|
|
154
|
-
row.length =
|
|
155
|
-
for (let outputIndex = 0;outputIndex <
|
|
156
|
-
const column =
|
|
155
|
+
row.length = selectedColumns.length;
|
|
156
|
+
for (let outputIndex = 0;outputIndex < selectedColumns.length; ++outputIndex) {
|
|
157
|
+
const column = selectedColumns[outputIndex] ?? 0;
|
|
157
158
|
const fieldIndex = fieldStart + column;
|
|
158
159
|
if (fieldIndex >= fieldEnd) {
|
|
159
160
|
row[outputIndex] = "";
|
|
@@ -294,17 +295,18 @@ export class NativeCsvBatch {
|
|
|
294
295
|
scanColumns(columns, callback, startRow = 0, endRow = this.rowCount) {
|
|
295
296
|
const resolvedEndRow = this.#resolveEndRow(endRow);
|
|
296
297
|
this.#validateRowRange(startRow, resolvedEndRow);
|
|
298
|
+
const selectedColumns = [...columns];
|
|
297
299
|
const rowOffsetWords = this.#rowOffsetWordsView();
|
|
298
300
|
const fieldOffsetWords = this.#fieldOffsetWordsView();
|
|
299
301
|
const data = this.data();
|
|
300
302
|
const totalFields = this.totalFields;
|
|
301
|
-
const ranges = new Float64Array(
|
|
303
|
+
const ranges = new Float64Array(selectedColumns.length * 2);
|
|
302
304
|
for (let rowIndex = startRow;rowIndex < resolvedEndRow; ++rowIndex) {
|
|
303
305
|
this.#requireHandle();
|
|
304
306
|
const rowStart = offsetAt(rowOffsetWords, rowIndex, `row ${rowIndex} offset`, totalFields);
|
|
305
307
|
const rowEnd = offsetAt(rowOffsetWords, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
|
|
306
|
-
for (let columnOffset = 0;columnOffset <
|
|
307
|
-
const columnIndex =
|
|
308
|
+
for (let columnOffset = 0;columnOffset < selectedColumns.length; ++columnOffset) {
|
|
309
|
+
const columnIndex = selectedColumns[columnOffset] ?? 0;
|
|
308
310
|
const fieldIndex = rowStart + columnIndex;
|
|
309
311
|
const rangeIndex = columnOffset * 2;
|
|
310
312
|
if (fieldIndex < rowStart || fieldIndex >= rowEnd) {
|
package/dist/cli-command.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { csv } from "./api.js";
|
|
2
2
|
import {
|
|
3
|
-
csvCommand
|
|
3
|
+
csvCommand,
|
|
4
|
+
parseColumnIndex
|
|
4
5
|
} from "./cli-schema.js";
|
|
5
6
|
import { formatBytes } from "./format-bytes.js";
|
|
6
7
|
import { inspectCsvFile } from "./inspect-file.js";
|
|
7
|
-
import {
|
|
8
|
-
normalizeColumns,
|
|
9
|
-
normalizeFilterColumn
|
|
10
|
-
} from "./normalize.js";
|
|
8
|
+
import { normalizeFilterColumn } from "./normalize.js";
|
|
11
9
|
const MAX_FILTER_NESTING = 128;
|
|
12
10
|
const OUTPUT_BUFFER_SIZE = 65536;
|
|
13
11
|
export async function runCli(arguments_, customOutput) {
|
|
@@ -28,7 +26,7 @@ export async function runCli(arguments_, customOutput) {
|
|
|
28
26
|
case "lines":
|
|
29
27
|
return runLines(subcommand.args, output);
|
|
30
28
|
case "probe":
|
|
31
|
-
return runProbe(subcommand.args
|
|
29
|
+
return runProbe(subcommand.args, output);
|
|
32
30
|
case "completion":
|
|
33
31
|
return runCompletion(subcommand.args.shell, output);
|
|
34
32
|
}
|
|
@@ -38,17 +36,26 @@ async function runCompletion(shell, output) {
|
|
|
38
36
|
output.log(generateCompletion(csvCommand, shell).trimEnd());
|
|
39
37
|
return 0;
|
|
40
38
|
}
|
|
41
|
-
async function runProbe(
|
|
39
|
+
async function runProbe(args, output) {
|
|
42
40
|
try {
|
|
43
|
-
const result = await inspectCsvFile(path, entry);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
const result = await inspectCsvFile(args.path, { zipEntry: args["zip-entry"], encoding: args.encoding, delimiter: args.delimiter });
|
|
42
|
+
if (args.json) {
|
|
43
|
+
output.log(JSON.stringify(result));
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
const lines = [
|
|
47
|
+
`File size: ${formatBytes(result.fileSizeBytes)}`,
|
|
48
|
+
`Encoding: ${result.encoding}${result.encodingSource === "explicit" ? " (explicit)" : ""}`,
|
|
49
|
+
`Delimiter: ${result.delimiter === null ? "undetermined" : JSON.stringify(result.delimiter)}${result.delimiterSource === "explicit" ? " (explicit)" : ""}`,
|
|
48
50
|
`Line endings: ${result.lineEnding}`,
|
|
49
51
|
`Compression: ${result.compression}`,
|
|
50
52
|
`First record columns: ${String(result.firstRecordColumns ?? "unknown")}`
|
|
51
|
-
]
|
|
53
|
+
];
|
|
54
|
+
if (result.zipEntry !== null)
|
|
55
|
+
lines.push(`ZIP entry: ${JSON.stringify(result.zipEntry)}`);
|
|
56
|
+
if (result.delimiterReason !== null)
|
|
57
|
+
lines.push(`Delimiter detail: ${result.delimiterReason}`);
|
|
58
|
+
output.log(lines.join(`
|
|
52
59
|
`));
|
|
53
60
|
return 0;
|
|
54
61
|
} catch (error) {
|
|
@@ -73,14 +80,14 @@ async function runCount(args, output) {
|
|
|
73
80
|
}
|
|
74
81
|
}
|
|
75
82
|
async function runLines(args, output) {
|
|
76
|
-
let
|
|
83
|
+
let options;
|
|
77
84
|
try {
|
|
78
|
-
|
|
85
|
+
options = { ...parseCommonOptions(args), limit: args.limit };
|
|
79
86
|
} catch (error) {
|
|
80
87
|
return usageError(output, error instanceof Error ? error.message : String(error), "csv lines --help");
|
|
81
88
|
}
|
|
82
89
|
try {
|
|
83
|
-
await streamLines(
|
|
90
|
+
await streamLines(args, options, output);
|
|
84
91
|
return 0;
|
|
85
92
|
} catch (error) {
|
|
86
93
|
if (isBrokenPipe(error))
|
|
@@ -90,58 +97,26 @@ async function runLines(args, output) {
|
|
|
90
97
|
return 1;
|
|
91
98
|
}
|
|
92
99
|
}
|
|
93
|
-
function
|
|
94
|
-
const { limit } = values;
|
|
95
|
-
const options = parseCommonOptions(values, values.strict === !0 && limit !== void 0);
|
|
96
|
-
if (limit !== void 0 && options.strict === !0) {
|
|
97
|
-
if (options.compression !== "none")
|
|
98
|
-
throw Error("--strict with --limit does not support compressed input");
|
|
99
|
-
if (options.delimiter === "auto")
|
|
100
|
-
throw Error("--strict with --limit requires a fixed input delimiter");
|
|
101
|
-
if (options.encoding === "auto")
|
|
102
|
-
throw Error("--strict with --limit requires a fixed input encoding");
|
|
103
|
-
if (options.chunkSize !== void 0 && options.chunkSize !== 1)
|
|
104
|
-
throw Error("--strict with --limit requires --chunk-size 1");
|
|
105
|
-
options.chunkSize = 1;
|
|
106
|
-
}
|
|
107
|
-
return {
|
|
108
|
-
json: values.json,
|
|
109
|
-
...limit === void 0 ? {} : { limit },
|
|
110
|
-
options,
|
|
111
|
-
outputDelimiter: values["output-delimiter"],
|
|
112
|
-
path: values.path
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function parseCommonOptions(values, fixedFormat = !1) {
|
|
100
|
+
function parseCommonOptions(values) {
|
|
116
101
|
const options = values.strict ? { strict: !0 } : { where: parseWhereOptions(values) };
|
|
117
|
-
options.delimiter = values.delimiter ??
|
|
118
|
-
options.encoding = values.encoding ??
|
|
102
|
+
options.delimiter = values.delimiter ?? "auto";
|
|
103
|
+
options.encoding = values.encoding ?? "auto";
|
|
104
|
+
options.skipRows = values["skip-rows"];
|
|
119
105
|
if (values["chunk-size"] !== void 0)
|
|
120
106
|
options.chunkSize = values["chunk-size"];
|
|
121
107
|
if (values["fixed-columns"] !== void 0)
|
|
122
108
|
options.fixedColumns = values["fixed-columns"];
|
|
123
109
|
if (values.columns !== void 0)
|
|
124
|
-
options.columns =
|
|
110
|
+
options.columns = values.columns;
|
|
125
111
|
if (values["expected-header"].length > 0)
|
|
126
112
|
options.expectedHeaders = values["expected-header"];
|
|
127
113
|
if (values["require-header"] === !0)
|
|
128
114
|
options.requireHeader = !0;
|
|
129
115
|
if (values["min-data-rows"] !== void 0)
|
|
130
116
|
options.minDataRows = values["min-data-rows"];
|
|
131
|
-
options.compression = parseCompression(values) ??
|
|
117
|
+
options.compression = parseCompression(values) ?? "auto";
|
|
132
118
|
return options;
|
|
133
119
|
}
|
|
134
|
-
function parseColumnIndex(value, label) {
|
|
135
|
-
const number = Number(value);
|
|
136
|
-
if (!Number.isSafeInteger(number) || number < 0)
|
|
137
|
-
throw Error(`${label} must be an integer greater than or equal to 0: ${value}`);
|
|
138
|
-
return number;
|
|
139
|
-
}
|
|
140
|
-
function parseColumns(value, label) {
|
|
141
|
-
const columns = value.length === 0 ? [] : value.split(",").map((column) => parseColumnIndex(column, label));
|
|
142
|
-
normalizeColumns(columns);
|
|
143
|
-
return columns;
|
|
144
|
-
}
|
|
145
120
|
function parseCompression(values) {
|
|
146
121
|
const compression = values.compression;
|
|
147
122
|
const zipEntry = values["zip-entry"];
|
|
@@ -159,34 +134,24 @@ function parseCompression(values) {
|
|
|
159
134
|
...maxDecompressedBytes === void 0 ? {} : { maxDecompressedBytes }
|
|
160
135
|
};
|
|
161
136
|
}
|
|
162
|
-
async function streamLines(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
chunksLength += record.length;
|
|
173
|
-
++emitted;
|
|
174
|
-
if (chunksLength >= OUTPUT_BUFFER_SIZE) {
|
|
175
|
-
await output.write(chunks.join(""));
|
|
176
|
-
chunks = [];
|
|
177
|
-
chunksLength = 0;
|
|
178
|
-
}
|
|
179
|
-
if (emitted === input.limit)
|
|
180
|
-
break batches;
|
|
181
|
-
}
|
|
182
|
-
if (chunksLength > 0) {
|
|
137
|
+
async function streamLines(args, options, output) {
|
|
138
|
+
for await (const rows of csv.rows(args.path, options)) {
|
|
139
|
+
let chunks = [];
|
|
140
|
+
let chunksLength = 0;
|
|
141
|
+
for (const row of rows) {
|
|
142
|
+
const record = args.json ? `${JSON.stringify(row)}
|
|
143
|
+
` : serializeCsvRecord(row, args["output-delimiter"]);
|
|
144
|
+
chunks.push(record);
|
|
145
|
+
chunksLength += record.length;
|
|
146
|
+
if (chunksLength >= OUTPUT_BUFFER_SIZE) {
|
|
183
147
|
await output.write(chunks.join(""));
|
|
184
148
|
chunks = [];
|
|
185
149
|
chunksLength = 0;
|
|
186
150
|
}
|
|
187
151
|
}
|
|
188
|
-
|
|
189
|
-
|
|
152
|
+
if (chunksLength > 0)
|
|
153
|
+
await output.write(chunks.join(""));
|
|
154
|
+
}
|
|
190
155
|
}
|
|
191
156
|
function serializeCsvRecord(row, delimiter) {
|
|
192
157
|
return `${row.map((field) => serializeCsvField(field, delimiter)).join(delimiter)}
|
package/dist/cli-schema.d.ts
CHANGED
|
@@ -6,11 +6,12 @@ declare const count: import("@konstit/cli").HandledCommand<"count", {
|
|
|
6
6
|
encoding: "utf8" | "utf-16le" | "latin1" | "ibm866" | "iso-8859-2" | "iso-8859-3" | "iso-8859-4" | "iso-8859-5" | "iso-8859-6" | "iso-8859-7" | "iso-8859-8" | "iso-8859-8-i" | "iso-8859-10" | "iso-8859-13" | "iso-8859-14" | "iso-8859-15" | "iso-8859-16" | "koi8-r" | "koi8-u" | "macintosh" | "windows-874" | "windows-1250" | "windows-1251" | "windows-1252" | "windows-1253" | "windows-1254" | "windows-1255" | "windows-1256" | "windows-1257" | "windows-1258" | "x-mac-cyrillic" | "gbk" | "gb18030" | "big5" | "euc-jp" | "iso-2022-jp" | "shift_jis" | "euc-kr" | "utf-16be" | "x-user-defined" | "auto" | undefined;
|
|
7
7
|
compression: "auto" | "zip" | "gzip" | "deflate" | "deflate-raw" | "brotli" | "zstd" | "none" | undefined;
|
|
8
8
|
"chunk-size": number | undefined;
|
|
9
|
+
"skip-rows": number | undefined;
|
|
9
10
|
"max-compression-ratio": number | undefined;
|
|
10
11
|
"max-decompressed-bytes": number | undefined;
|
|
11
12
|
strict: boolean;
|
|
12
13
|
"fixed-columns": number | undefined;
|
|
13
|
-
columns:
|
|
14
|
+
columns: number[] | undefined;
|
|
14
15
|
"expected-header": readonly string[];
|
|
15
16
|
"require-header": boolean;
|
|
16
17
|
"min-data-rows": number | undefined;
|
|
@@ -29,11 +30,12 @@ declare const lines: import("@konstit/cli").HandledCommand<"lines", {
|
|
|
29
30
|
encoding: "utf8" | "utf-16le" | "latin1" | "ibm866" | "iso-8859-2" | "iso-8859-3" | "iso-8859-4" | "iso-8859-5" | "iso-8859-6" | "iso-8859-7" | "iso-8859-8" | "iso-8859-8-i" | "iso-8859-10" | "iso-8859-13" | "iso-8859-14" | "iso-8859-15" | "iso-8859-16" | "koi8-r" | "koi8-u" | "macintosh" | "windows-874" | "windows-1250" | "windows-1251" | "windows-1252" | "windows-1253" | "windows-1254" | "windows-1255" | "windows-1256" | "windows-1257" | "windows-1258" | "x-mac-cyrillic" | "gbk" | "gb18030" | "big5" | "euc-jp" | "iso-2022-jp" | "shift_jis" | "euc-kr" | "utf-16be" | "x-user-defined" | "auto" | undefined;
|
|
30
31
|
compression: "auto" | "zip" | "gzip" | "deflate" | "deflate-raw" | "brotli" | "zstd" | "none" | undefined;
|
|
31
32
|
"chunk-size": number | undefined;
|
|
33
|
+
"skip-rows": number | undefined;
|
|
32
34
|
"max-compression-ratio": number | undefined;
|
|
33
35
|
"max-decompressed-bytes": number | undefined;
|
|
34
36
|
strict: boolean;
|
|
35
37
|
"fixed-columns": number | undefined;
|
|
36
|
-
columns:
|
|
38
|
+
columns: number[] | undefined;
|
|
37
39
|
"expected-header": readonly string[];
|
|
38
40
|
"require-header": boolean;
|
|
39
41
|
"min-data-rows": number | undefined;
|
|
@@ -50,6 +52,14 @@ declare const lines: import("@konstit/cli").HandledCommand<"lines", {
|
|
|
50
52
|
}, {}, false, "filters" | "zip-limits" | "schema">;
|
|
51
53
|
export type CountArguments = Infer<typeof count>;
|
|
52
54
|
export type LinesArguments = Infer<typeof lines>;
|
|
55
|
+
declare const probe: import("@konstit/cli").HandledCommand<"probe", {
|
|
56
|
+
path: string;
|
|
57
|
+
"zip-entry": string | undefined;
|
|
58
|
+
delimiter: string | undefined;
|
|
59
|
+
encoding: "utf8" | "utf-16le" | "latin1" | "ibm866" | "iso-8859-2" | "iso-8859-3" | "iso-8859-4" | "iso-8859-5" | "iso-8859-6" | "iso-8859-7" | "iso-8859-8" | "iso-8859-8-i" | "iso-8859-10" | "iso-8859-13" | "iso-8859-14" | "iso-8859-15" | "iso-8859-16" | "koi8-r" | "koi8-u" | "macintosh" | "windows-874" | "windows-1250" | "windows-1251" | "windows-1252" | "windows-1253" | "windows-1254" | "windows-1255" | "windows-1256" | "windows-1257" | "windows-1258" | "x-mac-cyrillic" | "gbk" | "gb18030" | "big5" | "euc-jp" | "iso-2022-jp" | "shift_jis" | "euc-kr" | "utf-16be" | "x-user-defined" | "auto" | undefined;
|
|
60
|
+
json: boolean;
|
|
61
|
+
}, {}, false, string>;
|
|
62
|
+
export type ProbeArguments = Infer<typeof probe>;
|
|
53
63
|
export declare const csvCommand: import("@konstit/cli").HandledCommand<"csv", {}, {
|
|
54
64
|
count: import("@konstit/cli").HandledCommand<"count", {
|
|
55
65
|
path: string;
|
|
@@ -58,11 +68,12 @@ export declare const csvCommand: import("@konstit/cli").HandledCommand<"csv", {}
|
|
|
58
68
|
encoding: "utf8" | "utf-16le" | "latin1" | "ibm866" | "iso-8859-2" | "iso-8859-3" | "iso-8859-4" | "iso-8859-5" | "iso-8859-6" | "iso-8859-7" | "iso-8859-8" | "iso-8859-8-i" | "iso-8859-10" | "iso-8859-13" | "iso-8859-14" | "iso-8859-15" | "iso-8859-16" | "koi8-r" | "koi8-u" | "macintosh" | "windows-874" | "windows-1250" | "windows-1251" | "windows-1252" | "windows-1253" | "windows-1254" | "windows-1255" | "windows-1256" | "windows-1257" | "windows-1258" | "x-mac-cyrillic" | "gbk" | "gb18030" | "big5" | "euc-jp" | "iso-2022-jp" | "shift_jis" | "euc-kr" | "utf-16be" | "x-user-defined" | "auto" | undefined;
|
|
59
69
|
compression: "auto" | "zip" | "gzip" | "deflate" | "deflate-raw" | "brotli" | "zstd" | "none" | undefined;
|
|
60
70
|
"chunk-size": number | undefined;
|
|
71
|
+
"skip-rows": number | undefined;
|
|
61
72
|
"max-compression-ratio": number | undefined;
|
|
62
73
|
"max-decompressed-bytes": number | undefined;
|
|
63
74
|
strict: boolean;
|
|
64
75
|
"fixed-columns": number | undefined;
|
|
65
|
-
columns:
|
|
76
|
+
columns: number[] | undefined;
|
|
66
77
|
"expected-header": readonly string[];
|
|
67
78
|
"require-header": boolean;
|
|
68
79
|
"min-data-rows": number | undefined;
|
|
@@ -81,11 +92,12 @@ export declare const csvCommand: import("@konstit/cli").HandledCommand<"csv", {}
|
|
|
81
92
|
encoding: "utf8" | "utf-16le" | "latin1" | "ibm866" | "iso-8859-2" | "iso-8859-3" | "iso-8859-4" | "iso-8859-5" | "iso-8859-6" | "iso-8859-7" | "iso-8859-8" | "iso-8859-8-i" | "iso-8859-10" | "iso-8859-13" | "iso-8859-14" | "iso-8859-15" | "iso-8859-16" | "koi8-r" | "koi8-u" | "macintosh" | "windows-874" | "windows-1250" | "windows-1251" | "windows-1252" | "windows-1253" | "windows-1254" | "windows-1255" | "windows-1256" | "windows-1257" | "windows-1258" | "x-mac-cyrillic" | "gbk" | "gb18030" | "big5" | "euc-jp" | "iso-2022-jp" | "shift_jis" | "euc-kr" | "utf-16be" | "x-user-defined" | "auto" | undefined;
|
|
82
93
|
compression: "auto" | "zip" | "gzip" | "deflate" | "deflate-raw" | "brotli" | "zstd" | "none" | undefined;
|
|
83
94
|
"chunk-size": number | undefined;
|
|
95
|
+
"skip-rows": number | undefined;
|
|
84
96
|
"max-compression-ratio": number | undefined;
|
|
85
97
|
"max-decompressed-bytes": number | undefined;
|
|
86
98
|
strict: boolean;
|
|
87
99
|
"fixed-columns": number | undefined;
|
|
88
|
-
columns:
|
|
100
|
+
columns: number[] | undefined;
|
|
89
101
|
"expected-header": readonly string[];
|
|
90
102
|
"require-header": boolean;
|
|
91
103
|
"min-data-rows": number | undefined;
|
|
@@ -103,9 +115,13 @@ export declare const csvCommand: import("@konstit/cli").HandledCommand<"csv", {}
|
|
|
103
115
|
probe: import("@konstit/cli").HandledCommand<"probe", {
|
|
104
116
|
path: string;
|
|
105
117
|
"zip-entry": string | undefined;
|
|
106
|
-
|
|
118
|
+
delimiter: string | undefined;
|
|
119
|
+
encoding: "utf8" | "utf-16le" | "latin1" | "ibm866" | "iso-8859-2" | "iso-8859-3" | "iso-8859-4" | "iso-8859-5" | "iso-8859-6" | "iso-8859-7" | "iso-8859-8" | "iso-8859-8-i" | "iso-8859-10" | "iso-8859-13" | "iso-8859-14" | "iso-8859-15" | "iso-8859-16" | "koi8-r" | "koi8-u" | "macintosh" | "windows-874" | "windows-1250" | "windows-1251" | "windows-1252" | "windows-1253" | "windows-1254" | "windows-1255" | "windows-1256" | "windows-1257" | "windows-1258" | "x-mac-cyrillic" | "gbk" | "gb18030" | "big5" | "euc-jp" | "iso-2022-jp" | "shift_jis" | "euc-kr" | "utf-16be" | "x-user-defined" | "auto" | undefined;
|
|
120
|
+
json: boolean;
|
|
121
|
+
}, {}, false, string>;
|
|
107
122
|
completion: import("@konstit/cli").HandledCommand<"completion", {
|
|
108
123
|
shell: "bash" | "zsh" | "fish" | "powershell";
|
|
109
124
|
}, {}, boolean, string>;
|
|
110
125
|
}, true, string>;
|
|
126
|
+
export declare function parseColumnIndex(value: string, label: string): number;
|
|
111
127
|
export {};
|