@konstit/csv 0.4.3 → 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 +50 -28
- package/THIRD_PARTY_NOTICES.md +7 -1
- package/dist/api.d.ts +39 -249
- package/dist/api.js +56 -394
- package/dist/batches.d.ts +8 -57
- package/dist/batches.js +77 -52
- package/dist/cli-command.d.ts +7 -0
- package/dist/cli-command.js +315 -0
- package/dist/cli-schema.d.ts +127 -0
- package/dist/cli-schema.js +234 -0
- package/dist/cli.js +2 -604
- package/dist/file-stream.d.ts +62 -3
- package/dist/file-stream.js +242 -36
- package/dist/files.d.ts +11 -11
- package/dist/files.js +38 -79
- package/dist/filter.d.ts +5 -8
- package/dist/format-bytes.d.ts +1 -0
- package/dist/format-bytes.js +11 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -14
- package/dist/inspect-file.d.ts +22 -0
- package/dist/inspect-file.js +138 -0
- package/dist/latin1-rows.d.ts +4 -3
- package/dist/latin1-rows.js +72 -24
- package/dist/materialize-field.d.ts +2 -0
- package/dist/materialize-field.js +20 -15
- package/dist/native.d.ts +50 -78
- package/dist/native.js +36 -112
- package/dist/normalize.d.ts +14 -40
- package/dist/normalize.js +66 -64
- package/dist/options-snapshot.js +3 -2
- package/dist/options.d.ts +4 -30
- package/dist/options.js +0 -6
- package/dist/parser.d.ts +23 -209
- package/dist/parser.js +146 -261
- package/dist/strict-schema.d.ts +6 -4
- package/dist/strict-schema.js +4 -3
- package/dist/types.d.ts +119 -221
- package/dist/zip-reader.d.ts +13 -1
- package/dist/zip-reader.js +36 -17
- package/package.json +36 -33
- 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 +9 -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/dist/worker-count.d.ts +0 -34
- package/dist/worker-count.js +0 -101
- package/dist/worker-factory.d.ts +0 -3
- package/dist/worker-factory.js +0 -9
- package/dist/worker-filter.d.ts +0 -29
- package/dist/worker-filter.js +0 -46
- package/dist/worker-module.d.ts +0 -1
- package/dist/worker-module.js +0 -4
- package/dist/worker-pool.d.ts +0 -93
- package/dist/worker-pool.js +0 -298
- package/dist/worker-rows.d.ts +0 -36
- package/dist/worker-rows.js +0 -127
- package/dist/workers/count.worker.d.ts +0 -1
- package/dist/workers/count.worker.js +0 -36
- package/dist/workers/rows.worker.d.ts +0 -1
- package/dist/workers/rows.worker.js +0 -56
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { type Infer } from '@konstit/cli';
|
|
2
|
+
declare const count: import("@konstit/cli").HandledCommand<"count", {
|
|
3
|
+
path: string;
|
|
4
|
+
"zip-entry": string | undefined;
|
|
5
|
+
delimiter: string | undefined;
|
|
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
|
+
compression: "auto" | "zip" | "gzip" | "deflate" | "deflate-raw" | "brotli" | "zstd" | "none" | undefined;
|
|
8
|
+
"chunk-size": number | undefined;
|
|
9
|
+
"skip-rows": number | undefined;
|
|
10
|
+
"max-compression-ratio": number | undefined;
|
|
11
|
+
"max-decompressed-bytes": number | undefined;
|
|
12
|
+
strict: boolean;
|
|
13
|
+
"fixed-columns": number | undefined;
|
|
14
|
+
columns: number[] | undefined;
|
|
15
|
+
"expected-header": readonly string[];
|
|
16
|
+
"require-header": boolean;
|
|
17
|
+
"min-data-rows": number | undefined;
|
|
18
|
+
where: unknown;
|
|
19
|
+
"where-eq": readonly import("@konstit/cli").KeyValuePair[];
|
|
20
|
+
"where-neq": readonly import("@konstit/cli").KeyValuePair[];
|
|
21
|
+
"where-in": readonly import("@konstit/cli").KeyValuePair[];
|
|
22
|
+
"where-noin": readonly import("@konstit/cli").KeyValuePair[];
|
|
23
|
+
"where-prefix": readonly import("@konstit/cli").KeyValuePair[];
|
|
24
|
+
"where-regex": readonly import("@konstit/cli").KeyValuePair[];
|
|
25
|
+
}, {}, false, "filters" | "zip-limits" | "schema">;
|
|
26
|
+
declare const lines: import("@konstit/cli").HandledCommand<"lines", {
|
|
27
|
+
path: string;
|
|
28
|
+
"zip-entry": string | undefined;
|
|
29
|
+
delimiter: string | undefined;
|
|
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;
|
|
31
|
+
compression: "auto" | "zip" | "gzip" | "deflate" | "deflate-raw" | "brotli" | "zstd" | "none" | undefined;
|
|
32
|
+
"chunk-size": number | undefined;
|
|
33
|
+
"skip-rows": number | undefined;
|
|
34
|
+
"max-compression-ratio": number | undefined;
|
|
35
|
+
"max-decompressed-bytes": number | undefined;
|
|
36
|
+
strict: boolean;
|
|
37
|
+
"fixed-columns": number | undefined;
|
|
38
|
+
columns: number[] | undefined;
|
|
39
|
+
"expected-header": readonly string[];
|
|
40
|
+
"require-header": boolean;
|
|
41
|
+
"min-data-rows": number | undefined;
|
|
42
|
+
where: unknown;
|
|
43
|
+
"where-eq": readonly import("@konstit/cli").KeyValuePair[];
|
|
44
|
+
"where-neq": readonly import("@konstit/cli").KeyValuePair[];
|
|
45
|
+
"where-in": readonly import("@konstit/cli").KeyValuePair[];
|
|
46
|
+
"where-noin": readonly import("@konstit/cli").KeyValuePair[];
|
|
47
|
+
"where-prefix": readonly import("@konstit/cli").KeyValuePair[];
|
|
48
|
+
"where-regex": readonly import("@konstit/cli").KeyValuePair[];
|
|
49
|
+
json: boolean;
|
|
50
|
+
"output-delimiter": string;
|
|
51
|
+
limit: number | undefined;
|
|
52
|
+
}, {}, false, "filters" | "zip-limits" | "schema">;
|
|
53
|
+
export type CountArguments = Infer<typeof count>;
|
|
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>;
|
|
63
|
+
export declare const csvCommand: import("@konstit/cli").HandledCommand<"csv", {}, {
|
|
64
|
+
count: import("@konstit/cli").HandledCommand<"count", {
|
|
65
|
+
path: string;
|
|
66
|
+
"zip-entry": string | undefined;
|
|
67
|
+
delimiter: string | undefined;
|
|
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;
|
|
69
|
+
compression: "auto" | "zip" | "gzip" | "deflate" | "deflate-raw" | "brotli" | "zstd" | "none" | undefined;
|
|
70
|
+
"chunk-size": number | undefined;
|
|
71
|
+
"skip-rows": number | undefined;
|
|
72
|
+
"max-compression-ratio": number | undefined;
|
|
73
|
+
"max-decompressed-bytes": number | undefined;
|
|
74
|
+
strict: boolean;
|
|
75
|
+
"fixed-columns": number | undefined;
|
|
76
|
+
columns: number[] | undefined;
|
|
77
|
+
"expected-header": readonly string[];
|
|
78
|
+
"require-header": boolean;
|
|
79
|
+
"min-data-rows": number | undefined;
|
|
80
|
+
where: unknown;
|
|
81
|
+
"where-eq": readonly import("@konstit/cli").KeyValuePair[];
|
|
82
|
+
"where-neq": readonly import("@konstit/cli").KeyValuePair[];
|
|
83
|
+
"where-in": readonly import("@konstit/cli").KeyValuePair[];
|
|
84
|
+
"where-noin": readonly import("@konstit/cli").KeyValuePair[];
|
|
85
|
+
"where-prefix": readonly import("@konstit/cli").KeyValuePair[];
|
|
86
|
+
"where-regex": readonly import("@konstit/cli").KeyValuePair[];
|
|
87
|
+
}, {}, false, "filters" | "zip-limits" | "schema">;
|
|
88
|
+
lines: import("@konstit/cli").HandledCommand<"lines", {
|
|
89
|
+
path: string;
|
|
90
|
+
"zip-entry": string | undefined;
|
|
91
|
+
delimiter: string | undefined;
|
|
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;
|
|
93
|
+
compression: "auto" | "zip" | "gzip" | "deflate" | "deflate-raw" | "brotli" | "zstd" | "none" | undefined;
|
|
94
|
+
"chunk-size": number | undefined;
|
|
95
|
+
"skip-rows": number | undefined;
|
|
96
|
+
"max-compression-ratio": number | undefined;
|
|
97
|
+
"max-decompressed-bytes": number | undefined;
|
|
98
|
+
strict: boolean;
|
|
99
|
+
"fixed-columns": number | undefined;
|
|
100
|
+
columns: number[] | undefined;
|
|
101
|
+
"expected-header": readonly string[];
|
|
102
|
+
"require-header": boolean;
|
|
103
|
+
"min-data-rows": number | undefined;
|
|
104
|
+
where: unknown;
|
|
105
|
+
"where-eq": readonly import("@konstit/cli").KeyValuePair[];
|
|
106
|
+
"where-neq": readonly import("@konstit/cli").KeyValuePair[];
|
|
107
|
+
"where-in": readonly import("@konstit/cli").KeyValuePair[];
|
|
108
|
+
"where-noin": readonly import("@konstit/cli").KeyValuePair[];
|
|
109
|
+
"where-prefix": readonly import("@konstit/cli").KeyValuePair[];
|
|
110
|
+
"where-regex": readonly import("@konstit/cli").KeyValuePair[];
|
|
111
|
+
json: boolean;
|
|
112
|
+
"output-delimiter": string;
|
|
113
|
+
limit: number | undefined;
|
|
114
|
+
}, {}, false, "filters" | "zip-limits" | "schema">;
|
|
115
|
+
probe: import("@konstit/cli").HandledCommand<"probe", {
|
|
116
|
+
path: string;
|
|
117
|
+
"zip-entry": string | undefined;
|
|
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>;
|
|
122
|
+
completion: import("@konstit/cli").HandledCommand<"completion", {
|
|
123
|
+
shell: "bash" | "zsh" | "fish" | "powershell";
|
|
124
|
+
}, {}, boolean, string>;
|
|
125
|
+
}, true, string>;
|
|
126
|
+
export declare function parseColumnIndex(value: string, label: string): number;
|
|
127
|
+
export {};
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import {
|
|
2
|
+
command,
|
|
3
|
+
completionShells,
|
|
4
|
+
flag,
|
|
5
|
+
option,
|
|
6
|
+
parsers,
|
|
7
|
+
positional
|
|
8
|
+
} from "@konstit/cli";
|
|
9
|
+
import packageInfo from "../package.json";
|
|
10
|
+
import {
|
|
11
|
+
normalizeColumns,
|
|
12
|
+
resolveInputEncoding
|
|
13
|
+
} from "./normalize.js";
|
|
14
|
+
const encodings = parsers.enum([
|
|
15
|
+
"auto",
|
|
16
|
+
{ value: "utf8", aliases: ["utf-8"] },
|
|
17
|
+
{ value: "latin1", aliases: ["iso88591", "iso-8859-1"] },
|
|
18
|
+
"utf-16le",
|
|
19
|
+
"utf-16be",
|
|
20
|
+
"windows-874",
|
|
21
|
+
"windows-1250",
|
|
22
|
+
"windows-1251",
|
|
23
|
+
{ value: "windows-1252", aliases: ["cp1252"] },
|
|
24
|
+
"windows-1253",
|
|
25
|
+
"windows-1254",
|
|
26
|
+
"windows-1255",
|
|
27
|
+
"windows-1256",
|
|
28
|
+
"windows-1257",
|
|
29
|
+
"windows-1258",
|
|
30
|
+
"iso-8859-2",
|
|
31
|
+
"iso-8859-3",
|
|
32
|
+
"iso-8859-4",
|
|
33
|
+
"iso-8859-5",
|
|
34
|
+
"iso-8859-6",
|
|
35
|
+
"iso-8859-7",
|
|
36
|
+
"iso-8859-8",
|
|
37
|
+
"iso-8859-8-i",
|
|
38
|
+
"iso-8859-10",
|
|
39
|
+
"iso-8859-13",
|
|
40
|
+
"iso-8859-14",
|
|
41
|
+
"iso-8859-15",
|
|
42
|
+
"iso-8859-16",
|
|
43
|
+
"big5",
|
|
44
|
+
"euc-jp",
|
|
45
|
+
"euc-kr",
|
|
46
|
+
"gb18030",
|
|
47
|
+
"gbk",
|
|
48
|
+
"ibm866",
|
|
49
|
+
"iso-2022-jp",
|
|
50
|
+
"koi8-r",
|
|
51
|
+
"koi8-u",
|
|
52
|
+
"macintosh",
|
|
53
|
+
{ value: "shift_jis", aliases: ["sjis"] },
|
|
54
|
+
"x-mac-cyrillic",
|
|
55
|
+
"x-user-defined"
|
|
56
|
+
]);
|
|
57
|
+
const encodingParser = {
|
|
58
|
+
...encodings,
|
|
59
|
+
parse(label) {
|
|
60
|
+
try {
|
|
61
|
+
return encodings.parse(label);
|
|
62
|
+
} catch {
|
|
63
|
+
const encoding = resolveInputEncoding(label);
|
|
64
|
+
return encodings.parse(typeof encoding === "string" ? encoding : encoding.encoding);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const compressionFormats = ["auto", "none", "gzip", "deflate", "deflate-raw", "brotli", "zstd", "zip"];
|
|
69
|
+
const positiveUint32 = parsers.range(1, 4294967295, { integer: !0 });
|
|
70
|
+
const rowCount = parsers.range(0, Number.MAX_SAFE_INTEGER, { integer: !0 });
|
|
71
|
+
const byteSize = parsers.custom(parsers.byteSize.label, (input) => {
|
|
72
|
+
const bare = Number(input);
|
|
73
|
+
return parsers.byteSize.parse(Number.isNaN(bare) ? input : `${bare}B`);
|
|
74
|
+
});
|
|
75
|
+
const positiveByteSize = parsers.refine(byteSize, (bytes) => bytes > 0, "expected at least 1 byte");
|
|
76
|
+
const chunkSize = parsers.refine(positiveByteSize, (bytes) => bytes <= 67108864, "expected at most 64 MiB");
|
|
77
|
+
const inputDelimiter = parsers.regex(/^(?:auto|[\p{ASCII}--["\r\n]])$/v);
|
|
78
|
+
const outputDelimiter = parsers.regex(/^[\p{ASCII}--["\r\n]]$/v);
|
|
79
|
+
const columns = parsers.custom("INDEXES", (value) => {
|
|
80
|
+
const indexes = value.length === 0 ? [] : value.split(",").map((column) => parseColumnIndex(column, "columns"));
|
|
81
|
+
normalizeColumns(indexes);
|
|
82
|
+
return indexes;
|
|
83
|
+
});
|
|
84
|
+
const path = positional("path", parsers.string, { required: !0, valueHint: "file", describe: "Input file path" });
|
|
85
|
+
const zipEntry = option("zip-entry", parsers.refine(parsers.string, (value) => value.length > 0, "expected a non-empty ZIP entry name"), {
|
|
86
|
+
valueName: "PATH",
|
|
87
|
+
describe: "Select an entry inside a ZIP archive"
|
|
88
|
+
});
|
|
89
|
+
const formatArgs = [
|
|
90
|
+
option("delimiter", inputDelimiter, { valueName: "CHAR|auto", describe: "One safe ASCII character or auto (default: auto)" }),
|
|
91
|
+
option("encoding", encodingParser, {
|
|
92
|
+
valueName: "ENCODING",
|
|
93
|
+
describe: "Input encoding or decoder alias (default: auto)"
|
|
94
|
+
})
|
|
95
|
+
];
|
|
96
|
+
const fileArgs = [
|
|
97
|
+
path,
|
|
98
|
+
...formatArgs,
|
|
99
|
+
option("compression", parsers.enum(compressionFormats), {
|
|
100
|
+
valueName: "FORMAT",
|
|
101
|
+
describe: `Input compression (default: auto). Choices: ${compressionFormats.join(", ")}`
|
|
102
|
+
}),
|
|
103
|
+
option("chunk-size", chunkSize, {
|
|
104
|
+
describe: "Read 1 B through 64 MiB per chunk (default: 1MiB). Example: 256KiB"
|
|
105
|
+
}),
|
|
106
|
+
option("skip-rows", rowCount, { valueName: "N", describe: "Skip the first N input records before filtering, including any header" }),
|
|
107
|
+
zipEntry,
|
|
108
|
+
option("max-compression-ratio", positiveUint32, {
|
|
109
|
+
valueName: "RATIO",
|
|
110
|
+
describe: "Maximum ZIP expansion ratio, an integer from 1 through 4294967295"
|
|
111
|
+
}),
|
|
112
|
+
option("max-decompressed-bytes", positiveByteSize, { describe: "Maximum decompressed ZIP size (default: 64GiB)" }),
|
|
113
|
+
flag("strict", { describe: "Enable strict RFC 4180 validation" }),
|
|
114
|
+
option("fixed-columns", positiveUint32, { valueName: "COUNT", describe: "Require this positive field count in strict mode" }),
|
|
115
|
+
option("columns", columns, {
|
|
116
|
+
describe: "Zero-based column indexes in output order. The count command validates the indexes only"
|
|
117
|
+
}),
|
|
118
|
+
option("expected-header", parsers.string, { repeatable: !0, describe: "Expected header value. Repeat once per column in strict mode" }),
|
|
119
|
+
flag("require-header", { describe: "Require a header row in strict mode" }),
|
|
120
|
+
option("min-data-rows", rowCount, {
|
|
121
|
+
valueName: "COUNT",
|
|
122
|
+
describe: "Require at least this many data rows in strict mode"
|
|
123
|
+
}),
|
|
124
|
+
option("where", parsers.json(), { valueName: "JSON", describe: "Advanced JSON can nest all, any, and not." }),
|
|
125
|
+
option("where-eq", parsers.keyValue, { repeatable: !0, valueName: "COLUMN=VALUE", describe: "Match an exact value" }),
|
|
126
|
+
option("where-neq", parsers.keyValue, { repeatable: !0, valueName: "COLUMN=VALUE", describe: "Exclude an exact value" }),
|
|
127
|
+
option("where-in", parsers.keyValue, { repeatable: !0, valueName: "COLUMN=VALUE", describe: "Match any listed value for this column" }),
|
|
128
|
+
option("where-noin", parsers.keyValue, {
|
|
129
|
+
repeatable: !0,
|
|
130
|
+
valueName: "COLUMN=VALUE",
|
|
131
|
+
describe: "Exclude all listed values for this column"
|
|
132
|
+
}),
|
|
133
|
+
option("where-prefix", parsers.keyValue, { repeatable: !0, valueName: "COLUMN=VALUE", describe: "Match a value prefix" }),
|
|
134
|
+
option("where-regex", parsers.keyValue, {
|
|
135
|
+
repeatable: !0,
|
|
136
|
+
valueName: "COLUMN=/RE/FLAGS",
|
|
137
|
+
describe: "Match an RE2-compatible regex (flags: i, m, s, u)"
|
|
138
|
+
})
|
|
139
|
+
];
|
|
140
|
+
const fileRelations = {
|
|
141
|
+
requires: [["zip-entry", "compression"]],
|
|
142
|
+
requiresIf: [["compression", "zip", "zip-entry"]],
|
|
143
|
+
groups: [
|
|
144
|
+
{
|
|
145
|
+
name: "zip-limits",
|
|
146
|
+
members: ["max-compression-ratio", "max-decompressed-bytes"],
|
|
147
|
+
multiple: !0,
|
|
148
|
+
requires: ["zip-entry"]
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
name: "schema",
|
|
152
|
+
members: ["fixed-columns", "expected-header", "require-header", "min-data-rows"],
|
|
153
|
+
multiple: !0,
|
|
154
|
+
requires: ["strict"]
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
name: "filters",
|
|
158
|
+
members: ["where", "where-eq", "where-neq", "where-in", "where-noin", "where-prefix", "where-regex"],
|
|
159
|
+
multiple: !0,
|
|
160
|
+
conflicts: ["strict"]
|
|
161
|
+
}
|
|
162
|
+
]
|
|
163
|
+
};
|
|
164
|
+
const fileHelp = `ZIP input automatically selects the only data file, ignoring OS metadata.
|
|
165
|
+
Explicit ZIP options require --compression zip and --zip-entry.
|
|
166
|
+
Filters use original input column indexes. Friendly filter clauses use AND.
|
|
167
|
+
Strict mode cannot use filter options.
|
|
168
|
+
|
|
169
|
+
Encodings: ${encodings.choices?.map(({ value, aliases }) => aliases ? `${value} (${aliases.join(", ")})` : value).join(", ")}
|
|
170
|
+
Other Bun TextDecoder labels are also accepted.`;
|
|
171
|
+
const count = command("count", {
|
|
172
|
+
...fileRelations,
|
|
173
|
+
about: "Count CSV records, including the header row",
|
|
174
|
+
args: fileArgs,
|
|
175
|
+
afterHelp: `${fileHelp}
|
|
176
|
+
|
|
177
|
+
Examples:
|
|
178
|
+
csv count data.csv --chunk-size 256KiB
|
|
179
|
+
csv count data.csv --where-eq 2=SP
|
|
180
|
+
csv count data.csv --where-regex '1=/^ana/i'`
|
|
181
|
+
});
|
|
182
|
+
const lines = command("lines", {
|
|
183
|
+
...fileRelations,
|
|
184
|
+
conflicts: [["json", "output-delimiter"]],
|
|
185
|
+
about: "Stream matching records as normalized UTF-8 CSV or NDJSON",
|
|
186
|
+
args: [
|
|
187
|
+
...fileArgs,
|
|
188
|
+
flag("json", { describe: "Emit one JSON string array per record (NDJSON)" }),
|
|
189
|
+
option("output-delimiter", outputDelimiter, {
|
|
190
|
+
valueName: "CHAR",
|
|
191
|
+
default: ",",
|
|
192
|
+
describe: "One safe ASCII output delimiter (default: comma). Incompatible with --json"
|
|
193
|
+
}),
|
|
194
|
+
option("limit", rowCount, {
|
|
195
|
+
valueName: "N",
|
|
196
|
+
describe: "Emit at most N matching records (0 emits none)"
|
|
197
|
+
})
|
|
198
|
+
],
|
|
199
|
+
afterHelp: `${fileHelp}
|
|
200
|
+
|
|
201
|
+
Strict validation includes skipped records and stops when the output limit is reached.
|
|
202
|
+
Header and minimum data row checks apply to the consumed input prefix.
|
|
203
|
+
|
|
204
|
+
Examples:
|
|
205
|
+
csv lines data.csv --json --skip-rows 1 --limit 10
|
|
206
|
+
csv lines data.csv --columns 0,2 --where-eq 3=active`
|
|
207
|
+
});
|
|
208
|
+
const probe = command("probe", {
|
|
209
|
+
about: "Inspect file size, compression, encoding, delimiter, line endings, and first record width",
|
|
210
|
+
args: [path, ...formatArgs, zipEntry, flag("json", { describe: "Emit the inspection as one JSON object" })],
|
|
211
|
+
afterHelp: "Samples up to 64 KiB of decompressed CSV. For a ZIP archive with multiple data files, select one with --zip-entry."
|
|
212
|
+
});
|
|
213
|
+
export const csvCommand = command("csv", {
|
|
214
|
+
about: "Inspect, count, and stream CSV records",
|
|
215
|
+
version: packageInfo.version,
|
|
216
|
+
defaultToHelp: !0,
|
|
217
|
+
subcommandRequired: !0,
|
|
218
|
+
subcommands: [
|
|
219
|
+
count,
|
|
220
|
+
lines,
|
|
221
|
+
probe,
|
|
222
|
+
command("completion", {
|
|
223
|
+
about: "Print a shell completion script for csv",
|
|
224
|
+
args: [positional("shell", parsers.enum(completionShells), { required: !0, describe: completionShells.join(", ") })]
|
|
225
|
+
})
|
|
226
|
+
],
|
|
227
|
+
afterHelp: "Run without installing: bunx @konstit/csv <command> <path>"
|
|
228
|
+
});
|
|
229
|
+
export function parseColumnIndex(value, label) {
|
|
230
|
+
const number = Number(value);
|
|
231
|
+
if (!Number.isSafeInteger(number) || number < 0)
|
|
232
|
+
throw Error(`${label} must be an integer greater than or equal to 0: ${value}`);
|
|
233
|
+
return number;
|
|
234
|
+
}
|