@poe-platform/safe-bash 0.1.782 → 0.1.784
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 +40 -0
- package/dist/office-package/zip-sync.d.ts +2 -0
- package/dist/office-package/zip-sync.js +111 -0
- package/dist/safe-bash/chunks/chunk-4FDQJUFW.js +3464 -0
- package/dist/safe-bash/chunks/csvkit-K55OYAQC.js +1564 -0
- package/dist/safe-bash/chunks/ffmpeg-BEFTQ6DZ.js +60 -0
- package/dist/safe-bash/chunks/gh-HQWOWLWE.js +41 -0
- package/dist/safe-bash/chunks/git-UFNPAOYZ.js +52 -0
- package/dist/safe-bash/chunks/imagemagick-53BMHH6D.js +134 -0
- package/dist/safe-bash/chunks/implementation-QUTAAYMT.js +105 -0
- package/dist/safe-bash/chunks/pdfimages-4OYI6FLG.js +73 -0
- package/dist/safe-bash/chunks/pdfinfo-I7U6TQPP.js +153 -0
- package/dist/safe-bash/chunks/pdftk-SUA6FMHI.js +79 -0
- package/dist/safe-bash/chunks/pdftoppm-4C5DGEWX.js +139 -0
- package/dist/safe-bash/chunks/pdftotext-Y4WI225Y.js +118 -0
- package/dist/safe-bash/chunks/qpdf-IORB4RV5.js +68 -0
- package/dist/safe-bash/chunks/sips-WHAMJFEU.js +77 -0
- package/dist/safe-bash/chunks/soffice-SYEK6WJS.js +89 -0
- package/dist/safe-bash/chunks/wkhtmltopdf-ZIICWJLF.js +85 -0
- package/dist/safe-bash/command-chunks/chunk-2LP3VMPA.js +58 -0
- package/dist/safe-bash/command-chunks/{chunk-CG4JSOK2.js → chunk-3HDIDEUJ.js} +1 -1
- package/dist/safe-bash/command-chunks/chunk-4DSJDVBM.js +2897 -0
- package/dist/safe-bash/command-chunks/{chunk-GBHSE6ZO.js → chunk-5GRLAARV.js} +1 -1
- package/dist/safe-bash/command-chunks/chunk-7OKDCM5U.js +112 -0
- package/dist/safe-bash/command-chunks/{chunk-FH3D75FC.js → chunk-ADCGH6A7.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-MTKXVRXL.js → chunk-DREUDEQG.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-ADBFNLLK.js → chunk-FXYWQ6S4.js} +16 -14
- package/dist/safe-bash/command-chunks/{chunk-Y2KHC6TZ.js → chunk-IDKD76KR.js} +5 -3
- package/dist/safe-bash/command-chunks/{chunk-K6KBYLQC.js → chunk-JUDOV5MJ.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-3ZK5F23O.js → chunk-L3MLMXL4.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-E5TEVROY.js → chunk-PFBJR2PY.js} +4 -4
- package/dist/safe-bash/command-chunks/{chunk-YGYOVXTO.js → chunk-UUR2S7AD.js} +9 -5
- package/dist/safe-bash/command-chunks/chunk-VIVYKW7Q.js +101 -0
- package/dist/safe-bash/command-chunks/{chunk-I5GLTNVH.js → chunk-X4P42NYK.js} +2 -27
- package/dist/safe-bash/command-chunks/{chunk-XF6TXYLD.js → chunk-Z57TV6TC.js} +4 -54
- package/dist/safe-bash/command-chunks/{chunk-QDPRSPHQ.js → chunk-Z64BW7CG.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-X3IVPNC3.js → chunk-Z7TYZG3A.js} +5 -3
- package/dist/safe-bash/command-chunks/{chunk-GZXLPWT5.js → chunk-ZF3ZVGQC.js} +259 -258
- package/dist/safe-bash/command-chunks/{pptx-ZL6L7KP2-QVZEYFUV.js → pptx-KY2OJWZW-76LBHCHD.js} +4 -3
- package/dist/safe-bash/commands/csvkit/index.d.ts +1 -8
- package/dist/safe-bash/commands/csvkit/index.js +5 -1639
- package/dist/safe-bash/commands/csvkit/sync.d.ts +9 -0
- package/dist/safe-bash/commands/csvkit/sync.js +1639 -0
- package/dist/safe-bash/commands/pandoc/implementation.d.ts +8 -0
- package/dist/safe-bash/commands/pandoc/implementation.js +92 -0
- package/dist/safe-bash/commands/pandoc/index.d.ts +2 -6
- package/dist/safe-bash/commands/pandoc/index.js +2 -90
- package/dist/safe-bash/core.browser.js +808 -6271
- package/dist/safe-bash/core.d.ts +17 -16
- package/dist/safe-bash/core.js +17 -16
- package/dist/safe-bash/lazy-gh.d.ts +10 -0
- package/dist/safe-bash/lazy-gh.js +30 -0
- package/dist/safe-bash/lazy-optional.d.ts +499 -0
- package/dist/safe-bash/lazy-optional.js +609 -0
- package/dist/safe-bash/plugins/composition.d.ts +1 -1
- package/dist/safe-bash/plugins/composition.js +1 -1
- package/dist/safe-bash/plugins/lazy-command.d.ts +9 -0
- package/dist/safe-bash/plugins/lazy-command.js +57 -0
- package/dist/safe-bash/shell/runtime.js +24 -25
- package/dist/safe-bash-command-csvkit/cli/lexical.d.ts +3 -0
- package/dist/safe-bash-command-csvkit/cli/parser.d.ts +1 -2
- package/dist/safe-bash-command-csvkit/codecs/python.js +3 -2
- package/dist/safe-bash-command-csvkit/codecs/utf8.js +2 -1
- package/dist/safe-bash-command-csvkit/index.js +453 -3270
- package/dist/safe-bash-command-csvkit/sync-support.d.ts +7 -0
- package/dist/safe-bash-command-csvkit/sync-support.js +31 -0
- package/dist/safe-bash-command-gh/index.js +25 -17
- package/dist/safe-bash-command-git/index.js +6 -10
- package/dist/safe-bash-command-imagemagick/index.js +2 -2
- package/dist/safe-bash-command-pandoc/citeproc-filters.js +4 -2
- package/dist/safe-bash-command-pandoc/command.js +8 -6
- package/dist/safe-bash-command-pandoc/index.js +8 -6
- package/dist/safe-bash-command-pandoc/lua-filters.js +3 -2
- package/dist/safe-bash-command-pandoc/options.d.ts +5 -0
- package/dist/safe-bash-command-pandoc/options.js +93 -0
- package/dist/safe-bash-command-sips/index.js +2 -2
- package/dist/safe-bash-command-soffice/index.d.ts +1 -2
- package/dist/safe-bash-command-soffice/index.js +22 -18
- package/dist/safe-bash-command-ssconvert/commands.js +2 -2
- package/dist/safe-bash-command-ssconvert/core.js +2 -2
- package/dist/safe-bash-command-ssconvert/formats/csv.js +2 -2
- package/dist/safe-bash-command-ssconvert/formats/xls.js +3 -3
- package/dist/safe-bash-command-ssconvert/formats/xlsx.js +3 -3
- package/dist/safe-bash-command-timeout/index.js +3 -3
- package/dist/safe-bash-command-yq/index.js +5 -5
- package/dist/safe-bash-compression-engine/bounded-codec.js +3 -3
- package/dist/safe-bash-compression-engine/codec-loader.js +2 -2
- package/dist/safe-bash-compression-engine/files.js +7 -7
- package/dist/safe-bash-compression-engine/index.js +6 -6
- package/dist/safe-bash-compression-engine/operand.js +7 -7
- package/dist/safe-bash-compression-engine/stream.js +6 -6
- package/dist/safe-bash-compression-engine/zstd-decode.js +5 -5
- package/dist/safe-bash-contracts/diagnostics.js +3 -3
- package/dist/safe-bash-contracts/escaping.js +9 -9
- package/dist/safe-bash-contracts/filesystem-descriptor.js +1 -1
- package/dist/safe-bash-contracts/filesystem-output.js +10 -10
- package/dist/safe-bash-contracts/index.js +40 -40
- package/dist/safe-bash-io-engine/commands/search/requirements.js +4 -4
- package/dist/safe-bash-io-engine/commands/text-programs/shared.js +4 -4
- package/dist/safe-bash-io-engine/internal.d.ts +9 -0
- package/dist/safe-bash-io-engine/internal.js +3 -3
- package/dist/safe-bash-query-engine/index.js +6 -6
- package/dist/safe-bash-query-engine/interpreter.js +5 -5
- package/dist/safe-bash-query-engine/query-core.js +6 -6
- package/dist/safe-bash-regex-engine/execution/bounded-provider.js +3 -3
- package/dist/safe-bash-regex-engine/index.js +2 -2
- package/dist/safe-bash-regex-engine/text/regex.js +2 -2
- package/dist/safe-bash-xml-engine/index.js +3 -3
- package/package.json +4 -4
- package/dist/safe-bash/command-chunks/{chunk-DC52GWRK.js → chunk-BUKYQBXO.js} +13 -13
- package/dist/safe-bash/command-chunks/{chunk-QJ3ONEBI.js → chunk-DEH3CMGL.js} +3 -3
- package/dist/safe-bash/command-chunks/{chunk-Y64PA6IA.js → chunk-KHZZ4V3L.js} +3 -3
- package/dist/safe-bash/command-chunks/{chunk-I7PKIGII.js → chunk-QIRUOY2W.js} +3 -3
- package/dist/safe-bash/command-chunks/{chunk-HQG2IBZL.js → chunk-TOSM3T3A.js} +6 -6
- package/dist/safe-bash/command-chunks/{chunk-F4IQCXZG.js → chunk-VBZMCL23.js} +4 -4
- package/dist/safe-bash/command-chunks/{chunk-J2NPH7DO.js → chunk-W2T7JWT3.js} +3 -3
|
@@ -0,0 +1,1564 @@
|
|
|
1
|
+
import {
|
|
2
|
+
inflateRaw
|
|
3
|
+
} from "./chunk-4FDQJUFW.js";
|
|
4
|
+
import {
|
|
5
|
+
internal_exports
|
|
6
|
+
} from "./chunk-GJWUEOT6.js";
|
|
7
|
+
import {
|
|
8
|
+
init_platform
|
|
9
|
+
} from "./chunk-Q45QCA4J.js";
|
|
10
|
+
|
|
11
|
+
// packages/safe-bash/src/commands/csvkit/index.ts
|
|
12
|
+
init_platform();
|
|
13
|
+
|
|
14
|
+
// packages/safe-bash/src/commands/csvkit/sync.ts
|
|
15
|
+
init_platform();
|
|
16
|
+
|
|
17
|
+
// packages/office-package/dist/zip-sync.js
|
|
18
|
+
init_platform();
|
|
19
|
+
function readZipArchiveEntries(zipBytes) {
|
|
20
|
+
const map = /* @__PURE__ */ new Map();
|
|
21
|
+
const view = new DataView(zipBytes.buffer, zipBytes.byteOffset, zipBytes.byteLength);
|
|
22
|
+
let eocdPos = -1;
|
|
23
|
+
for (let i = zipBytes.length - 22; i >= Math.max(0, zipBytes.length - 65557); i--) {
|
|
24
|
+
if (view.getUint32(i, true) === 101010256) {
|
|
25
|
+
eocdPos = i;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (eocdPos < 0)
|
|
30
|
+
throw new Error("Invalid ZIP: missing end of central directory");
|
|
31
|
+
const count = view.getUint16(eocdPos + 10, true);
|
|
32
|
+
let cdPos = view.getUint32(eocdPos + 16, true);
|
|
33
|
+
for (let i = 0; i < count; i++) {
|
|
34
|
+
if (cdPos + 46 > zipBytes.length || view.getUint32(cdPos, true) !== 33639248) {
|
|
35
|
+
throw new Error("Invalid ZIP: truncated central directory");
|
|
36
|
+
}
|
|
37
|
+
const method = view.getUint16(cdPos + 10, true);
|
|
38
|
+
const compSize = view.getUint32(cdPos + 20, true);
|
|
39
|
+
const nameLen = view.getUint16(cdPos + 28, true);
|
|
40
|
+
const extraLen = view.getUint16(cdPos + 30, true);
|
|
41
|
+
const commentLen = view.getUint16(cdPos + 32, true);
|
|
42
|
+
const localOffset = view.getUint32(cdPos + 42, true);
|
|
43
|
+
if (cdPos + 46 + nameLen + extraLen + commentLen > zipBytes.length || localOffset + 30 > zipBytes.length || view.getUint32(localOffset, true) !== 67324752) {
|
|
44
|
+
throw new Error("Invalid ZIP: truncated or invalid entry header");
|
|
45
|
+
}
|
|
46
|
+
const name = new TextDecoder().decode(zipBytes.subarray(cdPos + 46, cdPos + 46 + nameLen));
|
|
47
|
+
const localNameLen = view.getUint16(localOffset + 26, true);
|
|
48
|
+
const localExtraLen = view.getUint16(localOffset + 28, true);
|
|
49
|
+
const dataStart = localOffset + 30 + localNameLen + localExtraLen;
|
|
50
|
+
if (dataStart + compSize > zipBytes.length) {
|
|
51
|
+
throw new Error("Invalid ZIP: truncated entry data");
|
|
52
|
+
}
|
|
53
|
+
const rawData = zipBytes.subarray(dataStart, dataStart + compSize);
|
|
54
|
+
if (method === 0) {
|
|
55
|
+
map.set(name, new Uint8Array(rawData));
|
|
56
|
+
} else if (method === 8) {
|
|
57
|
+
map.set(name, inflateRaw(rawData));
|
|
58
|
+
}
|
|
59
|
+
cdPos += 46 + nameLen + extraLen + commentLen;
|
|
60
|
+
}
|
|
61
|
+
return map;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// packages/safe-bash/src/commands/csvkit/sync.ts
|
|
65
|
+
import {
|
|
66
|
+
defaultHeaders,
|
|
67
|
+
inferTable,
|
|
68
|
+
readCsv,
|
|
69
|
+
sniff,
|
|
70
|
+
Decimal,
|
|
71
|
+
pythonValueText,
|
|
72
|
+
writeCsvRow,
|
|
73
|
+
parseColumnIdentifiers,
|
|
74
|
+
matchColumnIdentifier
|
|
75
|
+
} from "../../safe-bash-command-csvkit/sync-support.js";
|
|
76
|
+
var syncUtf8Decoder = new TextDecoder("utf-8", { fatal: true });
|
|
77
|
+
function parseSyncCsvkitCommon(opArgs, mode) {
|
|
78
|
+
const res = {
|
|
79
|
+
tabs: false,
|
|
80
|
+
noDoublequote: false,
|
|
81
|
+
skipInitialSpace: false,
|
|
82
|
+
noHeaderRow: false,
|
|
83
|
+
skipLines: 0,
|
|
84
|
+
lineNumbers: false,
|
|
85
|
+
addBom: false,
|
|
86
|
+
noInference: false,
|
|
87
|
+
blanks: false,
|
|
88
|
+
noLeadingZeroes: false,
|
|
89
|
+
sniffLimit: 1024,
|
|
90
|
+
noNumberEllipsis: false,
|
|
91
|
+
indent: null,
|
|
92
|
+
key: null,
|
|
93
|
+
streamOutput: false
|
|
94
|
+
};
|
|
95
|
+
let positionalDone = false;
|
|
96
|
+
for (let i = 0; i < opArgs.length; i++) {
|
|
97
|
+
const a = opArgs[i];
|
|
98
|
+
if (!positionalDone && a === "--") {
|
|
99
|
+
positionalDone = true;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (!positionalDone && a.startsWith("-") && a !== "-") {
|
|
103
|
+
if (a === "-t" || a === "--tabs") {
|
|
104
|
+
res.tabs = true;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (a === "-b" || a === "--no-doublequote") {
|
|
108
|
+
res.noDoublequote = true;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (a === "-S" || a === "--skipinitialspace") {
|
|
112
|
+
res.skipInitialSpace = true;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (a === "-H" || a === "--no-header-row") {
|
|
116
|
+
res.noHeaderRow = true;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (a === "-l" || a === "--linenumbers") {
|
|
120
|
+
res.lineNumbers = true;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (a === "--add-bom") {
|
|
124
|
+
res.addBom = true;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (a === "-I" || a === "--no-inference") {
|
|
128
|
+
res.noInference = true;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (a === "--blanks") {
|
|
132
|
+
res.blanks = true;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (a === "--no-leading-zeroes") {
|
|
136
|
+
res.noLeadingZeroes = true;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (a === "-d" || a.startsWith("-d") || a === "--delimiter" || a.startsWith("--delimiter=")) {
|
|
140
|
+
const v = a.startsWith("--delimiter=") ? a.slice(12) : a.startsWith("-d=") ? a.slice(3) : a.length > 2 && a.startsWith("-d") ? a.slice(2) : opArgs[++i];
|
|
141
|
+
if (!v || Array.from(v).length !== 1) return void 0;
|
|
142
|
+
res.delimiter = v;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (a === "-q" || a.startsWith("-q") || a === "--quotechar" || a.startsWith("--quotechar=")) {
|
|
146
|
+
const v = a.startsWith("--quotechar=") ? a.slice(12) : a.startsWith("-q=") ? a.slice(3) : a.length > 2 && a.startsWith("-q") ? a.slice(2) : opArgs[++i];
|
|
147
|
+
if (!v || Array.from(v).length !== 1) return void 0;
|
|
148
|
+
res.quotechar = v;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (a === "-p" || a === "--escapechar") {
|
|
152
|
+
const v = opArgs[++i];
|
|
153
|
+
if (!v || Array.from(v).length !== 1) return void 0;
|
|
154
|
+
res.escapechar = v;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (a === "-K" || a.startsWith("-K") || a === "--skip-lines" || a.startsWith("--skip-lines=")) {
|
|
158
|
+
const v = a.startsWith("--skip-lines=") ? a.slice(13) : a.startsWith("-K=") ? a.slice(3) : a.length > 2 && a.startsWith("-K") ? a.slice(2) : opArgs[++i];
|
|
159
|
+
if (v === void 0 || !/^[0-9]+$/.test(v)) return void 0;
|
|
160
|
+
res.skipLines = Number(v);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (a === "-y" || a === "--snifflimit") {
|
|
164
|
+
const v = opArgs[++i];
|
|
165
|
+
if (v === void 0 || !/^-?[0-9]+$/.test(v)) return void 0;
|
|
166
|
+
res.sniffLimit = Number(v);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (mode === "csvlook") {
|
|
170
|
+
if (a === "--no-number-ellipsis") {
|
|
171
|
+
res.noNumberEllipsis = true;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (a === "--max-rows" || a === "--max-columns" || a === "--max-column-width" || a === "--max-precision") {
|
|
175
|
+
const v = opArgs[++i];
|
|
176
|
+
if (v === void 0 || !/^[0-9]+$/.test(v)) return void 0;
|
|
177
|
+
const n = Number(v);
|
|
178
|
+
if (a === "--max-rows") res.maxRows = n;
|
|
179
|
+
else if (a === "--max-columns") res.maxColumns = n;
|
|
180
|
+
else if (a === "--max-column-width") res.maxColumnWidth = n;
|
|
181
|
+
else res.maxPrecision = n;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
if (a === "--stream") {
|
|
186
|
+
res.streamOutput = true;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (a === "-i" || a.startsWith("-i") || a === "--indent" || a.startsWith("--indent=")) {
|
|
190
|
+
const v = a.startsWith("--indent=") ? a.slice(9) : a.startsWith("-i=") ? a.slice(3) : a.length > 2 && a.startsWith("-i") ? a.slice(2) : opArgs[++i];
|
|
191
|
+
if (v === void 0 || !/^[0-9]+$/.test(v)) return void 0;
|
|
192
|
+
res.indent = Number(v);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (a === "-k" || a.startsWith("-k") || a === "--key" || a.startsWith("--key=")) {
|
|
196
|
+
const v = a.startsWith("--key=") ? a.slice(6) : a.startsWith("-k=") ? a.slice(3) : a.length > 2 && a.startsWith("-k") ? a.slice(2) : opArgs[++i];
|
|
197
|
+
if (v === void 0) return void 0;
|
|
198
|
+
res.key = v;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (!a.startsWith("--") && a.length > 2) {
|
|
203
|
+
let ok = true;
|
|
204
|
+
for (let j = 1; j < a.length; j++) {
|
|
205
|
+
const ch = a[j];
|
|
206
|
+
if (ch === "t") res.tabs = true;
|
|
207
|
+
else if (ch === "b") res.noDoublequote = true;
|
|
208
|
+
else if (ch === "S") res.skipInitialSpace = true;
|
|
209
|
+
else if (ch === "H") res.noHeaderRow = true;
|
|
210
|
+
else if (ch === "l") res.lineNumbers = true;
|
|
211
|
+
else if (ch === "I") res.noInference = true;
|
|
212
|
+
else {
|
|
213
|
+
ok = false;
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (ok) continue;
|
|
218
|
+
}
|
|
219
|
+
return void 0;
|
|
220
|
+
}
|
|
221
|
+
if (res.filePath !== void 0) return void 0;
|
|
222
|
+
res.filePath = a;
|
|
223
|
+
}
|
|
224
|
+
return res;
|
|
225
|
+
}
|
|
226
|
+
function loadSyncTypedTable(inBytes, opts, readFileSync) {
|
|
227
|
+
let sourceBytes = inBytes;
|
|
228
|
+
if (opts.filePath !== void 0 && opts.filePath !== "-") {
|
|
229
|
+
if (!readFileSync) return void 0;
|
|
230
|
+
sourceBytes = readFileSync(opts.filePath);
|
|
231
|
+
}
|
|
232
|
+
if (!sourceBytes || sourceBytes.byteLength > 16384) return void 0;
|
|
233
|
+
let text;
|
|
234
|
+
try {
|
|
235
|
+
text = syncUtf8Decoder.decode(sourceBytes);
|
|
236
|
+
} catch {
|
|
237
|
+
return void 0;
|
|
238
|
+
}
|
|
239
|
+
if (text.charCodeAt(0) === 65279) text = text.slice(1);
|
|
240
|
+
if (opts.skipLines > 0) {
|
|
241
|
+
let linesLeft = opts.skipLines;
|
|
242
|
+
let pos = 0;
|
|
243
|
+
while (pos < text.length && linesLeft > 0) {
|
|
244
|
+
const ch = text[pos++];
|
|
245
|
+
if (ch === "\r") {
|
|
246
|
+
if (text[pos] === "\n") pos++;
|
|
247
|
+
linesLeft--;
|
|
248
|
+
} else if (ch === "\n") {
|
|
249
|
+
linesLeft--;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
text = text.slice(pos);
|
|
253
|
+
}
|
|
254
|
+
const explicitDelimiter = opts.tabs ? " " : opts.delimiter;
|
|
255
|
+
let dialect = {
|
|
256
|
+
...explicitDelimiter !== void 0 ? { delimiter: explicitDelimiter } : {},
|
|
257
|
+
...opts.quotechar !== void 0 ? { quotechar: opts.quotechar } : {},
|
|
258
|
+
...opts.escapechar !== void 0 ? { escapechar: opts.escapechar } : {},
|
|
259
|
+
doublequote: !opts.noDoublequote,
|
|
260
|
+
skipinitialspace: opts.skipInitialSpace
|
|
261
|
+
};
|
|
262
|
+
if (!opts.tabs && opts.delimiter === void 0 && opts.quotechar === void 0 && opts.escapechar === void 0 && !opts.noDoublequote && !opts.skipInitialSpace && opts.sniffLimit !== 0 && text.length > 0) {
|
|
263
|
+
const sample = opts.sniffLimit > 0 ? text.slice(0, opts.sniffLimit) : text;
|
|
264
|
+
if (sample.includes(",") && !/[\t;|:\x27]/.test(sample)) {
|
|
265
|
+
dialect = { delimiter: ",", quotechar: '"', doublequote: true, skipinitialspace: false };
|
|
266
|
+
} else {
|
|
267
|
+
const sniffed = sniff(sample);
|
|
268
|
+
if (sniffed) dialect = sniffed;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
let headers;
|
|
272
|
+
const rows = [];
|
|
273
|
+
const rowLimit = opts.maxRows;
|
|
274
|
+
for (const record of readCsv(text, dialect)) {
|
|
275
|
+
const values = record.cells.map((c) => pythonValueText(c));
|
|
276
|
+
const cells = opts.lineNumbers ? [!opts.noHeaderRow && record.line === 1 ? "line_numbers" : String(record.line - (opts.noHeaderRow ? 0 : 1)), ...values] : values;
|
|
277
|
+
if (headers === void 0) {
|
|
278
|
+
headers = opts.noHeaderRow ? defaultHeaders(cells.length) : cells;
|
|
279
|
+
if (headers.some((name) => !name) || new Set(headers).size !== headers.length) return void 0;
|
|
280
|
+
if (!opts.noHeaderRow) {
|
|
281
|
+
if (rowLimit === 0) break;
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
rows.push(cells);
|
|
286
|
+
if (rowLimit !== void 0 && rows.length >= rowLimit) break;
|
|
287
|
+
}
|
|
288
|
+
const inferenceOpts = {
|
|
289
|
+
now: Date.now(),
|
|
290
|
+
timezone: "UTC",
|
|
291
|
+
noInference: opts.noInference,
|
|
292
|
+
numberTextOnly: false,
|
|
293
|
+
blanks: opts.blanks,
|
|
294
|
+
nullValues: [],
|
|
295
|
+
noLeadingZeroes: opts.noLeadingZeroes,
|
|
296
|
+
maxDecimalDigits: Infinity,
|
|
297
|
+
maxDecimalExponent: Infinity
|
|
298
|
+
};
|
|
299
|
+
return inferTable(headers ?? [], rows, inferenceOpts);
|
|
300
|
+
}
|
|
301
|
+
function syncPrecision(values) {
|
|
302
|
+
let whole = 1;
|
|
303
|
+
let places = 0;
|
|
304
|
+
for (const value of values) {
|
|
305
|
+
if (value === null || typeof value !== "object" || value.kind !== "decimal") continue;
|
|
306
|
+
const d = Decimal.parse(value.value).normalized();
|
|
307
|
+
if (d.special || Math.abs(Number(d.toString())) === Infinity) continue;
|
|
308
|
+
places = Math.max(places, -d.exponent);
|
|
309
|
+
whole = Math.max(whole, d.coefficient.toString().length + d.exponent);
|
|
310
|
+
}
|
|
311
|
+
return Math.min(places, 28 - whole);
|
|
312
|
+
}
|
|
313
|
+
function syncNumberText(value, places, ellipsis) {
|
|
314
|
+
const d = Decimal.parse(value);
|
|
315
|
+
if (d.special === "Infinity" || Math.abs(Number(d.toString())) === Infinity) return value;
|
|
316
|
+
places = Math.max(0, places);
|
|
317
|
+
if (d.special) {
|
|
318
|
+
const digits2 = "NaN" + d.payload;
|
|
319
|
+
let grouped2 = "";
|
|
320
|
+
for (let i = 0; i < digits2.length; i++) {
|
|
321
|
+
if (i && (digits2.length - i) % 3 === 0) grouped2 += ",";
|
|
322
|
+
grouped2 += digits2[i];
|
|
323
|
+
}
|
|
324
|
+
return grouped2 + (places ? "." + "0".repeat(places) : "") + ellipsis;
|
|
325
|
+
}
|
|
326
|
+
let coefficient = d.coefficient;
|
|
327
|
+
const shift = d.exponent + places;
|
|
328
|
+
if (shift >= 0) coefficient *= 10n ** BigInt(shift);
|
|
329
|
+
else {
|
|
330
|
+
const divisor = 10n ** BigInt(-shift);
|
|
331
|
+
const rest = coefficient % divisor;
|
|
332
|
+
coefficient /= divisor;
|
|
333
|
+
if (rest * 2n > divisor || rest * 2n === divisor && coefficient % 2n !== 0n) coefficient++;
|
|
334
|
+
}
|
|
335
|
+
if (coefficient.toString().length > 28) throw new Error("InvalidOperation");
|
|
336
|
+
const digits = coefficient.toString().padStart(places + 1, "0");
|
|
337
|
+
const whole = digits.slice(0, digits.length - places);
|
|
338
|
+
let grouped = "";
|
|
339
|
+
for (let i = 0; i < whole.length; i++) {
|
|
340
|
+
if (i && (whole.length - i) % 3 === 0) grouped += ",";
|
|
341
|
+
grouped += whole[i];
|
|
342
|
+
}
|
|
343
|
+
return (d.negative ? "-" : "") + grouped + (places ? "." + digits.slice(-places) : "") + ellipsis;
|
|
344
|
+
}
|
|
345
|
+
function evalSyncCsvlook(inBytes, opArgs, readFileSync) {
|
|
346
|
+
try {
|
|
347
|
+
const opts = parseSyncCsvkitCommon(opArgs, "csvlook");
|
|
348
|
+
if (!opts) return void 0;
|
|
349
|
+
const table = loadSyncTypedTable(inBytes, opts, readFileSync);
|
|
350
|
+
if (!table) return void 0;
|
|
351
|
+
const maxColumns = opts.maxColumns ?? table.headers.length;
|
|
352
|
+
const width = opts.maxColumnWidth;
|
|
353
|
+
const maxPrecision = opts.maxPrecision ?? 3;
|
|
354
|
+
const cpLen = (v) => Array.from(v).length;
|
|
355
|
+
const truncate = (v) => {
|
|
356
|
+
const chars = Array.from(v);
|
|
357
|
+
return width !== void 0 && chars.length > width ? chars.slice(0, width - 3).join("") + "..." : v;
|
|
358
|
+
};
|
|
359
|
+
const names = table.headers.slice(0, maxColumns).map(truncate);
|
|
360
|
+
if (maxColumns < table.headers.length) names.push("...");
|
|
361
|
+
const widths = names.map(cpLen);
|
|
362
|
+
const formats = table.columns.map((column, index) => {
|
|
363
|
+
if (index >= maxColumns || column.type !== "Number") return void 0;
|
|
364
|
+
const places = syncPrecision(table.rows.map((row) => row[index]));
|
|
365
|
+
return { places: Math.min(places, maxPrecision), ellipsis: places > maxPrecision && !opts.noNumberEllipsis ? "\u2026" : "" };
|
|
366
|
+
});
|
|
367
|
+
const renderedRows = table.rows.map((row) => {
|
|
368
|
+
const cells = [];
|
|
369
|
+
for (const [j, value] of row.entries()) {
|
|
370
|
+
const format = formats[j];
|
|
371
|
+
let text = j >= maxColumns ? "..." : value === null ? "" : format && typeof value === "object" && value.kind === "decimal" ? syncNumberText(value.value, format.places, format.ellipsis) : pythonValueText(value).replaceAll("\n", "\u21B5");
|
|
372
|
+
text = truncate(text);
|
|
373
|
+
widths[j] = Math.max(widths[j], cpLen(text));
|
|
374
|
+
cells.push(text);
|
|
375
|
+
if (j >= maxColumns) break;
|
|
376
|
+
}
|
|
377
|
+
return cells;
|
|
378
|
+
});
|
|
379
|
+
const formatRow = (cells) => {
|
|
380
|
+
const fields = cells.map((cell, j) => {
|
|
381
|
+
const col = table.columns[j];
|
|
382
|
+
if (!col) throw new Error("IndexError");
|
|
383
|
+
const spaces = widths[j] - cpLen(cell);
|
|
384
|
+
const padding = " ".repeat(Math.max(0, spaces));
|
|
385
|
+
return col.type === "Text" ? " " + cell + padding + " " : " " + padding + cell + " ";
|
|
386
|
+
});
|
|
387
|
+
return "|" + fields.join("|") + "|\n";
|
|
388
|
+
};
|
|
389
|
+
let out = opts.addBom ? "\uFEFF" : "";
|
|
390
|
+
out += formatRow(names);
|
|
391
|
+
out += "| " + widths.map((size) => "-".repeat(size)).join(" | ") + " |\n";
|
|
392
|
+
for (const row of renderedRows) out += formatRow(row);
|
|
393
|
+
return out;
|
|
394
|
+
} catch {
|
|
395
|
+
return void 0;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
function syncFloatText(value) {
|
|
399
|
+
if (Number.isNaN(value)) return "NaN";
|
|
400
|
+
if (!Number.isFinite(value)) return value < 0 ? "-Infinity" : "Infinity";
|
|
401
|
+
if (Object.is(value, -0)) return "-0.0";
|
|
402
|
+
const exponent = Number(value.toExponential().split("e")[1]);
|
|
403
|
+
if (value !== 0 && (exponent < -4 || exponent >= 16)) {
|
|
404
|
+
const [digits, power] = value.toExponential().split("e");
|
|
405
|
+
const exp = Number(power);
|
|
406
|
+
return digits + "e" + (exp < 0 ? "-" : "+") + String(Math.abs(exp)).padStart(2, "0");
|
|
407
|
+
}
|
|
408
|
+
return String(value) + (Number.isInteger(value) ? ".0" : "");
|
|
409
|
+
}
|
|
410
|
+
function emitSyncJson(cur, indent, d = 0) {
|
|
411
|
+
if (typeof cur === "number") return syncFloatText(cur);
|
|
412
|
+
if (cur === null || typeof cur === "string" || typeof cur === "boolean") return JSON.stringify(cur);
|
|
413
|
+
if (cur instanceof Map) {
|
|
414
|
+
let buf2 = "{";
|
|
415
|
+
let index = 0;
|
|
416
|
+
const childPad2 = indent !== null ? "\n" + " ".repeat(indent * (d + 1)) : "";
|
|
417
|
+
for (const [key, child] of cur.entries()) {
|
|
418
|
+
if (index++) buf2 += indent === null ? ", " : ",";
|
|
419
|
+
if (indent !== null) buf2 += childPad2;
|
|
420
|
+
buf2 += JSON.stringify(key) + ": " + emitSyncJson(child, indent, d + 1);
|
|
421
|
+
}
|
|
422
|
+
if (cur.size && indent !== null) buf2 += "\n" + " ".repeat(indent * d);
|
|
423
|
+
buf2 += "}";
|
|
424
|
+
return buf2;
|
|
425
|
+
}
|
|
426
|
+
const arr = cur;
|
|
427
|
+
let buf = "[";
|
|
428
|
+
const childPad = indent !== null ? "\n" + " ".repeat(indent * (d + 1)) : "";
|
|
429
|
+
for (let index = 0; index < arr.length; index++) {
|
|
430
|
+
if (index) buf += indent === null ? ", " : ",";
|
|
431
|
+
if (indent !== null) buf += childPad;
|
|
432
|
+
buf += emitSyncJson(arr[index], indent, d + 1);
|
|
433
|
+
}
|
|
434
|
+
if (arr.length && indent !== null) buf += "\n" + " ".repeat(indent * d);
|
|
435
|
+
buf += "]";
|
|
436
|
+
return buf;
|
|
437
|
+
}
|
|
438
|
+
function evalSyncCsvjson(inBytes, opArgs, readFileSync) {
|
|
439
|
+
try {
|
|
440
|
+
const opts = parseSyncCsvkitCommon(opArgs, "csvjson");
|
|
441
|
+
if (!opts) return void 0;
|
|
442
|
+
if (opts.streamOutput && opts.key !== null) return void 0;
|
|
443
|
+
if (opts.streamOutput && opts.indent !== null) return void 0;
|
|
444
|
+
const table = loadSyncTypedTable(inBytes, opts, readFileSync);
|
|
445
|
+
if (!table) return void 0;
|
|
446
|
+
const jsonify = (value) => {
|
|
447
|
+
if (typeof value !== "object" || value === null) return value;
|
|
448
|
+
if (value.kind === "decimal") return value.value.includes("NaN") ? NaN : Number(value.value);
|
|
449
|
+
if (value.kind === "datetime") return value.value.replace(" ", "T");
|
|
450
|
+
return pythonValueText(value);
|
|
451
|
+
};
|
|
452
|
+
const rows = table.rows.map(
|
|
453
|
+
(row) => new Map(table.headers.map((name, index) => [name, jsonify(row[index])]))
|
|
454
|
+
);
|
|
455
|
+
let output = rows;
|
|
456
|
+
if (opts.key !== null) {
|
|
457
|
+
const column = table.headers.indexOf(opts.key);
|
|
458
|
+
if (column < 0 && rows.length) return void 0;
|
|
459
|
+
const keyed = /* @__PURE__ */ new Map();
|
|
460
|
+
for (const [index, row] of table.rows.entries()) {
|
|
461
|
+
const cell = row[column];
|
|
462
|
+
const value = cell === null ? "None" : typeof cell === "object" && cell.kind === "decimal" ? Decimal.parse(cell.value).normalized().toString() : pythonValueText(cell);
|
|
463
|
+
if (keyed.has(value)) return void 0;
|
|
464
|
+
keyed.set(value, rows[index]);
|
|
465
|
+
}
|
|
466
|
+
output = keyed;
|
|
467
|
+
}
|
|
468
|
+
let out = opts.addBom ? "\uFEFF" : "";
|
|
469
|
+
if (opts.streamOutput) {
|
|
470
|
+
for (const row of rows) {
|
|
471
|
+
out += emitSyncJson(row, opts.indent) + "\n";
|
|
472
|
+
}
|
|
473
|
+
return out;
|
|
474
|
+
}
|
|
475
|
+
out += emitSyncJson(output, opts.indent);
|
|
476
|
+
return out;
|
|
477
|
+
} catch {
|
|
478
|
+
return void 0;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
function evalSyncCsvsort(inBytes, opArgs, readFileSync) {
|
|
482
|
+
try {
|
|
483
|
+
let columnsSpec = null;
|
|
484
|
+
let reverse = false;
|
|
485
|
+
let ignoreCase = false;
|
|
486
|
+
let namesOnly = false;
|
|
487
|
+
let zeroBased = false;
|
|
488
|
+
const filteredArgs = [];
|
|
489
|
+
let posDone = false;
|
|
490
|
+
for (let i = 0; i < opArgs.length; i++) {
|
|
491
|
+
const a = opArgs[i];
|
|
492
|
+
if (!posDone && a === "--") {
|
|
493
|
+
posDone = true;
|
|
494
|
+
filteredArgs.push(a);
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
if (!posDone && a.startsWith("-") && a !== "-") {
|
|
498
|
+
if (a === "-r" || a === "--reverse") {
|
|
499
|
+
reverse = true;
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
if (a === "-i" || a === "--ignore-case") {
|
|
503
|
+
ignoreCase = true;
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
if (a === "-n" || a === "--names") {
|
|
507
|
+
namesOnly = true;
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (a === "--zero") {
|
|
511
|
+
zeroBased = true;
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
if (a === "-ri" || a === "-ir") {
|
|
515
|
+
reverse = true;
|
|
516
|
+
ignoreCase = true;
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
if (a === "-c" || a.startsWith("-c") || a === "--columns" || a.startsWith("--columns=")) {
|
|
520
|
+
const v = a.startsWith("--columns=") ? a.slice(10) : a.startsWith("-c=") ? a.slice(3) : a.length > 2 && a.startsWith("-c") ? a.slice(2) : opArgs[++i];
|
|
521
|
+
if (v === void 0) return void 0;
|
|
522
|
+
columnsSpec = v;
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
filteredArgs.push(a);
|
|
527
|
+
}
|
|
528
|
+
const opts = parseSyncCsvkitCommon(filteredArgs, "csvlook");
|
|
529
|
+
if (!opts) return void 0;
|
|
530
|
+
const table = loadSyncTypedTable(inBytes, opts, readFileSync);
|
|
531
|
+
if (!table) return void 0;
|
|
532
|
+
let out = opts.addBom ? "\uFEFF" : "";
|
|
533
|
+
if (namesOnly) {
|
|
534
|
+
if (opts.noHeaderRow) return void 0;
|
|
535
|
+
for (let i = 0; i < table.headers.length; i++) {
|
|
536
|
+
out += `${String(i + (zeroBased ? 0 : 1)).padStart(3, " ")}: ${table.headers[i]}
|
|
537
|
+
`;
|
|
538
|
+
}
|
|
539
|
+
return out;
|
|
540
|
+
}
|
|
541
|
+
const columns = parseColumnIdentifiers(columnsSpec, table.headers, zeroBased ? 0 : 1, null, () => {
|
|
542
|
+
}, true);
|
|
543
|
+
const rows = [...table.rows];
|
|
544
|
+
const keyed = rows.map(
|
|
545
|
+
(row) => columns.map((idx) => {
|
|
546
|
+
const val = row[idx];
|
|
547
|
+
return ignoreCase && typeof val === "string" ? val.toUpperCase() : val;
|
|
548
|
+
})
|
|
549
|
+
);
|
|
550
|
+
const indices = rows.map((_, i) => i);
|
|
551
|
+
indices.sort((a, b) => {
|
|
552
|
+
for (let c = 0; c < columns.length; c++) {
|
|
553
|
+
const x = keyed[a][c];
|
|
554
|
+
const y = keyed[b][c];
|
|
555
|
+
let order = 0;
|
|
556
|
+
if (x === null || y === null) order = x === null ? y === null ? 0 : 1 : -1;
|
|
557
|
+
else if (typeof x === "string" && typeof y === "string") {
|
|
558
|
+
order = x < y ? -1 : x > y ? 1 : 0;
|
|
559
|
+
} else if (typeof x === "boolean" && typeof y === "boolean") {
|
|
560
|
+
order = Number(x) - Number(y);
|
|
561
|
+
} else if (typeof x === "object" && typeof y === "object" && x.kind === "decimal" && y.kind === "decimal") {
|
|
562
|
+
if (x.value.includes("NaN") || y.value.includes("NaN")) throw new Error("NaN");
|
|
563
|
+
order = Decimal.parse(x.value).compare(Decimal.parse(y.value));
|
|
564
|
+
} else if (typeof x === "object" && typeof y === "object" && (x.kind === "date" || x.kind === "datetime") && (y.kind === "date" || y.kind === "datetime") && x.kind === y.kind) {
|
|
565
|
+
order = x.value < y.value ? -1 : x.value > y.value ? 1 : 0;
|
|
566
|
+
} else if (typeof x === "object" && typeof y === "object" && x.kind === "timedelta" && y.kind === "timedelta") {
|
|
567
|
+
order = x.microseconds < y.microseconds ? -1 : x.microseconds > y.microseconds ? 1 : 0;
|
|
568
|
+
} else {
|
|
569
|
+
throw new Error("unsupported type");
|
|
570
|
+
}
|
|
571
|
+
if (order !== 0) return reverse ? -order : order;
|
|
572
|
+
}
|
|
573
|
+
return 0;
|
|
574
|
+
});
|
|
575
|
+
const outDialect = { delimiter: ",", quotechar: '"', doublequote: true, lineterminator: "\n" };
|
|
576
|
+
out += writeCsvRow(table.headers, outDialect);
|
|
577
|
+
for (const idx of indices) {
|
|
578
|
+
const formattedCells = rows[idx].map((v) => {
|
|
579
|
+
if (v === null) return "";
|
|
580
|
+
if (typeof v === "object" && v.kind === "datetime") return v.value.replace(" ", "T");
|
|
581
|
+
return pythonValueText(v);
|
|
582
|
+
});
|
|
583
|
+
out += writeCsvRow(formattedCells, outDialect);
|
|
584
|
+
}
|
|
585
|
+
return out;
|
|
586
|
+
} catch {
|
|
587
|
+
return void 0;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
function evalSyncCsvformat(inBytes, opArgs, readFileSync) {
|
|
591
|
+
try {
|
|
592
|
+
let outDelimiter = ",";
|
|
593
|
+
let outTabs = false;
|
|
594
|
+
let outAsv = false;
|
|
595
|
+
let outQuotechar = '"';
|
|
596
|
+
let outQuoting = 0;
|
|
597
|
+
let outNoDoublequote = false;
|
|
598
|
+
let outEscapechar;
|
|
599
|
+
let outLineterminator = "\n";
|
|
600
|
+
let skipHeader = false;
|
|
601
|
+
const filteredArgs = [];
|
|
602
|
+
let posDone = false;
|
|
603
|
+
for (let i = 0; i < opArgs.length; i++) {
|
|
604
|
+
const a = opArgs[i];
|
|
605
|
+
if (!posDone && a === "--") {
|
|
606
|
+
posDone = true;
|
|
607
|
+
filteredArgs.push(a);
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
if (!posDone && a.startsWith("-") && a !== "-") {
|
|
611
|
+
if (a === "-E" || a === "--skip-header") {
|
|
612
|
+
skipHeader = true;
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
if (a === "-T" || a === "--out-tabs") {
|
|
616
|
+
outTabs = true;
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
if (a === "-A" || a === "--out-asv") {
|
|
620
|
+
outAsv = true;
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
if (a === "-B" || a === "--out-no-doublequote") {
|
|
624
|
+
outNoDoublequote = true;
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
if (a === "-D" || a.startsWith("-D") || a === "--out-delimiter" || a.startsWith("--out-delimiter=")) {
|
|
628
|
+
const v = a.startsWith("--out-delimiter=") ? a.slice(16) : a.startsWith("-D=") ? a.slice(3) : a.length > 2 && a.startsWith("-D") ? a.slice(2) : opArgs[++i];
|
|
629
|
+
if (!v || Array.from(v).length !== 1) return void 0;
|
|
630
|
+
outDelimiter = v;
|
|
631
|
+
continue;
|
|
632
|
+
}
|
|
633
|
+
if (a === "-Q" || a.startsWith("-Q") || a === "--out-quotechar" || a.startsWith("--out-quotechar=")) {
|
|
634
|
+
const v = a.startsWith("--out-quotechar=") ? a.slice(16) : a.startsWith("-Q=") ? a.slice(3) : a.length > 2 && a.startsWith("-Q") ? a.slice(2) : opArgs[++i];
|
|
635
|
+
if (!v || Array.from(v).length !== 1) return void 0;
|
|
636
|
+
outQuotechar = v;
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
if (a === "-P" || a === "--out-escapechar") {
|
|
640
|
+
const v = opArgs[++i];
|
|
641
|
+
if (!v || Array.from(v).length !== 1) return void 0;
|
|
642
|
+
outEscapechar = v;
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
if (a === "-M" || a.startsWith("-M") || a === "--out-lineterminator" || a.startsWith("--out-lineterminator=")) {
|
|
646
|
+
const v = a.startsWith("--out-lineterminator=") ? a.slice(21) : a.startsWith("-M=") ? a.slice(3) : a.length > 2 && a.startsWith("-M") ? a.slice(2) : opArgs[++i];
|
|
647
|
+
if (v === void 0) return void 0;
|
|
648
|
+
outLineterminator = v;
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
if (a === "-U" || a.startsWith("-U") || a === "--out-quoting" || a.startsWith("--out-quoting=")) {
|
|
652
|
+
const v = a.startsWith("--out-quoting=") ? a.slice(14) : a.startsWith("-U=") ? a.slice(3) : a.length > 2 && a.startsWith("-U") ? a.slice(2) : opArgs[++i];
|
|
653
|
+
if (v === void 0 || !/^[0123]$/.test(v)) return void 0;
|
|
654
|
+
outQuoting = Number(v);
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
filteredArgs.push(a);
|
|
659
|
+
}
|
|
660
|
+
const opts = parseSyncCsvkitCommon(filteredArgs, "csvlook");
|
|
661
|
+
if (!opts) return void 0;
|
|
662
|
+
let sourceBytes = inBytes;
|
|
663
|
+
if (opts.filePath !== void 0 && opts.filePath !== "-") {
|
|
664
|
+
if (!readFileSync) return void 0;
|
|
665
|
+
sourceBytes = readFileSync(opts.filePath);
|
|
666
|
+
}
|
|
667
|
+
if (!sourceBytes || sourceBytes.byteLength > 16384) return void 0;
|
|
668
|
+
let text = syncUtf8Decoder.decode(sourceBytes);
|
|
669
|
+
if (text.charCodeAt(0) === 65279) text = text.slice(1);
|
|
670
|
+
if (opts.skipLines > 0) {
|
|
671
|
+
let linesLeft = opts.skipLines;
|
|
672
|
+
let pos = 0;
|
|
673
|
+
while (pos < text.length && linesLeft > 0) {
|
|
674
|
+
const ch = text[pos++];
|
|
675
|
+
if (ch === "\r") {
|
|
676
|
+
if (text[pos] === "\n") pos++;
|
|
677
|
+
linesLeft--;
|
|
678
|
+
} else if (ch === "\n") linesLeft--;
|
|
679
|
+
}
|
|
680
|
+
text = text.slice(pos);
|
|
681
|
+
}
|
|
682
|
+
const explicitInDelimiter = opts.tabs ? " " : opts.delimiter;
|
|
683
|
+
let inDialect = {
|
|
684
|
+
...explicitInDelimiter !== void 0 ? { delimiter: explicitInDelimiter } : {},
|
|
685
|
+
...opts.quotechar !== void 0 ? { quotechar: opts.quotechar } : {},
|
|
686
|
+
...opts.escapechar !== void 0 ? { escapechar: opts.escapechar } : {},
|
|
687
|
+
doublequote: !opts.noDoublequote,
|
|
688
|
+
skipinitialspace: opts.skipInitialSpace
|
|
689
|
+
};
|
|
690
|
+
if (!opts.tabs && opts.delimiter === void 0 && opts.quotechar === void 0 && opts.escapechar === void 0 && !opts.noDoublequote && !opts.skipInitialSpace && opts.sniffLimit !== 0 && text.length > 0) {
|
|
691
|
+
const sample = opts.sniffLimit > 0 ? text.slice(0, opts.sniffLimit) : text;
|
|
692
|
+
if (sample.includes(",") && !/[\t;|:\x27]/.test(sample)) {
|
|
693
|
+
inDialect = { delimiter: ",", quotechar: '"', doublequote: true, skipinitialspace: false };
|
|
694
|
+
} else {
|
|
695
|
+
const sniffed = sniff(sample);
|
|
696
|
+
if (sniffed) inDialect = sniffed;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
const outDialect = {
|
|
700
|
+
delimiter: outAsv ? "" : outTabs ? " " : outDelimiter,
|
|
701
|
+
lineterminator: outAsv ? "" : outLineterminator,
|
|
702
|
+
quotechar: outQuotechar,
|
|
703
|
+
quoting: outQuoting,
|
|
704
|
+
doublequote: !outNoDoublequote,
|
|
705
|
+
...outEscapechar !== void 0 ? { escapechar: outEscapechar } : {}
|
|
706
|
+
};
|
|
707
|
+
writeCsvRow([], outDialect);
|
|
708
|
+
let out = opts.addBom ? "\uFEFF" : "";
|
|
709
|
+
let first = true;
|
|
710
|
+
for (const record of readCsv(text, inDialect)) {
|
|
711
|
+
const values = record.cells.map((c) => pythonValueText(c));
|
|
712
|
+
if (first) {
|
|
713
|
+
first = false;
|
|
714
|
+
if (opts.noHeaderRow) {
|
|
715
|
+
if (!skipHeader) {
|
|
716
|
+
const hdr = defaultHeaders(values.length);
|
|
717
|
+
out += writeCsvRow(opts.lineNumbers ? ["line_number", ...hdr] : hdr, outDialect);
|
|
718
|
+
}
|
|
719
|
+
} else {
|
|
720
|
+
if (!skipHeader) {
|
|
721
|
+
out += writeCsvRow(opts.lineNumbers ? ["line_number", ...values] : values, outDialect);
|
|
722
|
+
}
|
|
723
|
+
continue;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
const rowCells = opts.lineNumbers ? [String(record.line - (opts.noHeaderRow ? 0 : 1)), ...values] : values;
|
|
727
|
+
out += writeCsvRow(rowCells, outDialect);
|
|
728
|
+
}
|
|
729
|
+
return out;
|
|
730
|
+
} catch {
|
|
731
|
+
return void 0;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
function evalSyncCsvstat(inBytes, opArgs, readFileSync) {
|
|
735
|
+
try {
|
|
736
|
+
let countOnly = false;
|
|
737
|
+
let namesOnly = false;
|
|
738
|
+
let zeroBased = false;
|
|
739
|
+
let noGrouping = false;
|
|
740
|
+
let columnsSpec = null;
|
|
741
|
+
let metricOp;
|
|
742
|
+
const filteredArgs = [];
|
|
743
|
+
let posDone = false;
|
|
744
|
+
for (let i = 0; i < opArgs.length; i++) {
|
|
745
|
+
const a = opArgs[i];
|
|
746
|
+
if (!posDone && a === "--") {
|
|
747
|
+
posDone = true;
|
|
748
|
+
filteredArgs.push(a);
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
if (!posDone && a.startsWith("-") && a !== "-") {
|
|
752
|
+
if (a === "--count") {
|
|
753
|
+
countOnly = true;
|
|
754
|
+
continue;
|
|
755
|
+
}
|
|
756
|
+
if (a === "-n" || a === "--names") {
|
|
757
|
+
namesOnly = true;
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
if (a === "--zero") {
|
|
761
|
+
zeroBased = true;
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
if (a === "-G" || a === "--no-grouping-separator") {
|
|
765
|
+
noGrouping = true;
|
|
766
|
+
continue;
|
|
767
|
+
}
|
|
768
|
+
if (a === "-c" || a.startsWith("-c") || a === "--columns" || a.startsWith("--columns=")) {
|
|
769
|
+
const v = a.startsWith("--columns=") ? a.slice(10) : a.startsWith("-c=") ? a.slice(3) : a.length > 2 && a.startsWith("-c") ? a.slice(2) : opArgs[++i];
|
|
770
|
+
if (!v) return void 0;
|
|
771
|
+
columnsSpec = v;
|
|
772
|
+
continue;
|
|
773
|
+
}
|
|
774
|
+
const mMap = {
|
|
775
|
+
"--type": "type",
|
|
776
|
+
"--nulls": "nulls",
|
|
777
|
+
"--non-nulls": "nonnulls",
|
|
778
|
+
"--unique": "unique",
|
|
779
|
+
"--min": "min",
|
|
780
|
+
"--max": "max",
|
|
781
|
+
"--sum": "sum",
|
|
782
|
+
"--mean": "mean",
|
|
783
|
+
"--median": "median",
|
|
784
|
+
"--stdev": "stdev",
|
|
785
|
+
"--max-precision": "max_precision",
|
|
786
|
+
"--len": "len"
|
|
787
|
+
};
|
|
788
|
+
if (mMap[a]) {
|
|
789
|
+
if (metricOp !== void 0) return void 0;
|
|
790
|
+
metricOp = mMap[a];
|
|
791
|
+
continue;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
filteredArgs.push(a);
|
|
795
|
+
}
|
|
796
|
+
const modeCount = (countOnly ? 1 : 0) + (namesOnly ? 1 : 0) + (metricOp ? 1 : 0);
|
|
797
|
+
if (modeCount !== 1) return void 0;
|
|
798
|
+
const opts = parseSyncCsvkitCommon(filteredArgs, "csvlook");
|
|
799
|
+
if (!opts) return void 0;
|
|
800
|
+
const table = loadSyncTypedTable(inBytes, opts, readFileSync);
|
|
801
|
+
if (!table) return void 0;
|
|
802
|
+
let out = opts.addBom ? "\uFEFF" : "";
|
|
803
|
+
if (namesOnly) {
|
|
804
|
+
if (opts.noHeaderRow) return void 0;
|
|
805
|
+
for (let i = 0; i < table.headers.length; i++) {
|
|
806
|
+
out += `${String(i + (zeroBased ? 0 : 1)).padStart(3, " ")}: ${table.headers[i]}
|
|
807
|
+
`;
|
|
808
|
+
}
|
|
809
|
+
return out;
|
|
810
|
+
}
|
|
811
|
+
if (countOnly) {
|
|
812
|
+
return out + `${table.rows.length}
|
|
813
|
+
`;
|
|
814
|
+
}
|
|
815
|
+
const ids = parseColumnIdentifiers(columnsSpec, table.headers, zeroBased ? 0 : 1, void 0, () => {
|
|
816
|
+
}, true);
|
|
817
|
+
const fmtDec = (d) => {
|
|
818
|
+
const num = Number(d.toString());
|
|
819
|
+
if (!Number.isFinite(num)) return d.toString();
|
|
820
|
+
let s = num.toFixed(3);
|
|
821
|
+
while (s.endsWith("0")) s = s.slice(0, -1);
|
|
822
|
+
if (s.endsWith(".")) s = s.slice(0, -1);
|
|
823
|
+
if (!noGrouping) {
|
|
824
|
+
const [intPart = "", fracPart] = s.split(".");
|
|
825
|
+
const sign = intPart.startsWith("-") ? "-" : "";
|
|
826
|
+
const digits = sign ? intPart.slice(1) : intPart;
|
|
827
|
+
const grouped = digits.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
828
|
+
s = sign + grouped + (fracPart !== void 0 ? "." + fracPart : "");
|
|
829
|
+
}
|
|
830
|
+
return s;
|
|
831
|
+
};
|
|
832
|
+
for (const id of ids) {
|
|
833
|
+
const colType = table.columns[id].type;
|
|
834
|
+
const values = table.rows.map((r) => r[id]);
|
|
835
|
+
const nonNull = values.filter((v) => v !== null);
|
|
836
|
+
let valStr = "None";
|
|
837
|
+
if (metricOp === "type") valStr = colType;
|
|
838
|
+
else if (metricOp === "nulls") valStr = nonNull.length !== values.length ? "True" : "False";
|
|
839
|
+
else if (metricOp === "nonnulls") valStr = String(nonNull.length);
|
|
840
|
+
else if (metricOp === "unique") {
|
|
841
|
+
const seen = new Set(values.map((v) => v === null ? "null" : typeof v === "object" && v.kind === "decimal" ? "d:" + Decimal.parse(v.value).normalized().toString() : typeof v + ":" + pythonValueText(v)));
|
|
842
|
+
valStr = String(seen.size);
|
|
843
|
+
} else if (metricOp === "len" && colType === "Text") {
|
|
844
|
+
let maxLen = 0;
|
|
845
|
+
for (const v of nonNull) maxLen = Math.max(maxLen, Array.from(String(v)).length);
|
|
846
|
+
valStr = fmtDec(Decimal.parse(String(maxLen)));
|
|
847
|
+
} else if ((metricOp === "min" || metricOp === "max") && colType !== "Number") {
|
|
848
|
+
if (nonNull.length > 0) {
|
|
849
|
+
const strs = nonNull.map((v) => typeof v === "object" && v !== null && "value" in v ? String(v.value) : pythonValueText(v));
|
|
850
|
+
let best = strs[0];
|
|
851
|
+
for (let k = 1; k < strs.length; k++) {
|
|
852
|
+
if (metricOp === "min" ? strs[k] < best : strs[k] > best) best = strs[k];
|
|
853
|
+
}
|
|
854
|
+
valStr = best;
|
|
855
|
+
}
|
|
856
|
+
} else if ((metricOp === "min" || metricOp === "max" || metricOp === "sum" || metricOp === "mean" || metricOp === "median" || metricOp === "stdev" || metricOp === "max_precision") && colType === "Number") {
|
|
857
|
+
if (nonNull.length > 0) {
|
|
858
|
+
const decs = nonNull.map((v) => Decimal.parse(v.value));
|
|
859
|
+
if (metricOp === "min" || metricOp === "max") {
|
|
860
|
+
let best = decs[0];
|
|
861
|
+
for (let k = 1; k < decs.length; k++) {
|
|
862
|
+
if (metricOp === "min" ? decs[k].compare(best) < 0 : decs[k].compare(best) > 0) best = decs[k];
|
|
863
|
+
}
|
|
864
|
+
valStr = fmtDec(best);
|
|
865
|
+
} else if (metricOp === "max_precision") {
|
|
866
|
+
let maxP = 0;
|
|
867
|
+
for (const d of decs) {
|
|
868
|
+
const norm = d.normalized();
|
|
869
|
+
if (!norm.special && -norm.exponent > maxP) maxP = -norm.exponent;
|
|
870
|
+
}
|
|
871
|
+
valStr = fmtDec(Decimal.parse(String(maxP)));
|
|
872
|
+
} else if (metricOp === "median") {
|
|
873
|
+
const sorted = [...decs].sort((a, b) => a.compare(b));
|
|
874
|
+
const mid = Math.floor(sorted.length / 2);
|
|
875
|
+
const med = sorted.length % 2 === 1 ? sorted[mid] : sorted[mid - 1].add(sorted[mid]).divide(Decimal.parse("2"));
|
|
876
|
+
valStr = fmtDec(med);
|
|
877
|
+
} else if (metricOp === "stdev") {
|
|
878
|
+
if (decs.length >= 2) {
|
|
879
|
+
const nums = decs.map((d) => Number(d.toString()));
|
|
880
|
+
if (nums.some((n) => !Number.isFinite(n))) return void 0;
|
|
881
|
+
const mean = nums.reduce((a, b) => a + b, 0) / nums.length;
|
|
882
|
+
const variance = nums.reduce((acc, n) => acc + (n - mean) * (n - mean), 0) / (nums.length - 1);
|
|
883
|
+
valStr = fmtDec(Decimal.parse(String(Math.sqrt(variance))));
|
|
884
|
+
}
|
|
885
|
+
} else {
|
|
886
|
+
let sum = Decimal.parse("0");
|
|
887
|
+
for (const d of decs) sum = sum.add(d);
|
|
888
|
+
valStr = fmtDec(metricOp === "sum" ? sum : sum.divide(Decimal.parse(String(decs.length))));
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
} else {
|
|
892
|
+
return void 0;
|
|
893
|
+
}
|
|
894
|
+
out += (ids.length === 1 ? "" : `${String(id + 1).padStart(3)}. ${table.headers[id]}: `) + valStr + "\n";
|
|
895
|
+
}
|
|
896
|
+
return out;
|
|
897
|
+
} catch {
|
|
898
|
+
return void 0;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
function evalSyncIn2csv(inBytes, opArgs, readFileSync) {
|
|
902
|
+
try {
|
|
903
|
+
let formatSpec;
|
|
904
|
+
let keySpec;
|
|
905
|
+
let schemaSpec;
|
|
906
|
+
let sheetSpec;
|
|
907
|
+
let namesOnly = false;
|
|
908
|
+
const filteredArgs = [];
|
|
909
|
+
let posDone = false;
|
|
910
|
+
for (let i = 0; i < opArgs.length; i++) {
|
|
911
|
+
const a = opArgs[i];
|
|
912
|
+
if (!posDone && a === "--") {
|
|
913
|
+
posDone = true;
|
|
914
|
+
filteredArgs.push(a);
|
|
915
|
+
continue;
|
|
916
|
+
}
|
|
917
|
+
if (!posDone && a.startsWith("-") && a !== "-") {
|
|
918
|
+
if (a === "-n" || a === "--names") {
|
|
919
|
+
namesOnly = true;
|
|
920
|
+
continue;
|
|
921
|
+
}
|
|
922
|
+
if (a === "-f" || a.startsWith("-f") || a === "--format" || a.startsWith("--format=")) {
|
|
923
|
+
const v = a.startsWith("--format=") ? a.slice(9) : a.startsWith("-f=") ? a.slice(3) : a.length > 2 && a.startsWith("-f") ? a.slice(2) : opArgs[++i];
|
|
924
|
+
if (!v) return void 0;
|
|
925
|
+
formatSpec = v.toLowerCase();
|
|
926
|
+
continue;
|
|
927
|
+
}
|
|
928
|
+
if (a === "-k" || a.startsWith("-k") || a === "--key" || a.startsWith("--key=")) {
|
|
929
|
+
const v = a.startsWith("--key=") ? a.slice(6) : a.startsWith("-k=") ? a.slice(3) : a.length > 2 && a.startsWith("-k") ? a.slice(2) : opArgs[++i];
|
|
930
|
+
if (!v) return void 0;
|
|
931
|
+
keySpec = v;
|
|
932
|
+
continue;
|
|
933
|
+
}
|
|
934
|
+
if (a === "--sheet" || a.startsWith("--sheet=")) {
|
|
935
|
+
const v = a.startsWith("--sheet=") ? a.slice(8) : opArgs[++i];
|
|
936
|
+
if (!v) return void 0;
|
|
937
|
+
sheetSpec = v;
|
|
938
|
+
continue;
|
|
939
|
+
}
|
|
940
|
+
if (a === "-s" || a.startsWith("-s") || a === "--schema" || a.startsWith("--schema=")) {
|
|
941
|
+
const v = a.startsWith("--schema=") ? a.slice(9) : a.startsWith("-s=") ? a.slice(3) : a.length > 2 && a.startsWith("-s") ? a.slice(2) : opArgs[++i];
|
|
942
|
+
if (!v) return void 0;
|
|
943
|
+
schemaSpec = v;
|
|
944
|
+
continue;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
filteredArgs.push(a);
|
|
948
|
+
}
|
|
949
|
+
const opts = parseSyncCsvkitCommon(filteredArgs, "csvlook");
|
|
950
|
+
if (!opts) return void 0;
|
|
951
|
+
let sourceBytes = inBytes;
|
|
952
|
+
if (opts.filePath !== void 0 && opts.filePath !== "-") {
|
|
953
|
+
if (!readFileSync) return void 0;
|
|
954
|
+
sourceBytes = readFileSync(opts.filePath);
|
|
955
|
+
}
|
|
956
|
+
if (!sourceBytes || sourceBytes.byteLength > 16384) return void 0;
|
|
957
|
+
let fmt = formatSpec;
|
|
958
|
+
if (!fmt && opts.filePath && opts.filePath !== "-") {
|
|
959
|
+
const lower = opts.filePath.toLowerCase();
|
|
960
|
+
if (lower.endsWith(".geojson")) fmt = "geojson";
|
|
961
|
+
else if (lower.endsWith(".xlsx")) fmt = "xlsx";
|
|
962
|
+
else if (lower.endsWith(".json") || lower.endsWith(".js")) fmt = "json";
|
|
963
|
+
else if (lower.endsWith(".ndjson") || lower.endsWith(".jsonl")) fmt = "ndjson";
|
|
964
|
+
else if (lower.endsWith(".csv") || lower.endsWith(".tsv")) fmt = "csv";
|
|
965
|
+
}
|
|
966
|
+
if (!fmt && schemaSpec !== void 0) fmt = "fixed";
|
|
967
|
+
if (!fmt && keySpec !== void 0) fmt = "json";
|
|
968
|
+
if (fmt === "geojson") {
|
|
969
|
+
if (namesOnly) return void 0;
|
|
970
|
+
let text2 = syncUtf8Decoder.decode(sourceBytes);
|
|
971
|
+
if (text2.charCodeAt(0) === 65279) text2 = text2.slice(1);
|
|
972
|
+
const doc = JSON.parse(text2);
|
|
973
|
+
if (!doc || typeof doc !== "object" || Array.isArray(doc) || doc.type !== "FeatureCollection" || !Array.isArray(doc.features)) {
|
|
974
|
+
return void 0;
|
|
975
|
+
}
|
|
976
|
+
const propKeys = [];
|
|
977
|
+
const seenProps = /* @__PURE__ */ new Set();
|
|
978
|
+
for (const feat of doc.features) {
|
|
979
|
+
if (!feat || typeof feat !== "object" || Array.isArray(feat)) return void 0;
|
|
980
|
+
const props = feat.properties && typeof feat.properties === "object" && !Array.isArray(feat.properties) ? feat.properties : {};
|
|
981
|
+
for (const k of Object.keys(props)) {
|
|
982
|
+
if (!seenProps.has(k)) {
|
|
983
|
+
seenProps.add(k);
|
|
984
|
+
propKeys.push(k);
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
const outDialect2 = { delimiter: ",", quotechar: '"', doublequote: true, lineterminator: "\n" };
|
|
989
|
+
let out2 = writeCsvRow(["id", ...propKeys, "geojson", "type", "longitude", "latitude"], outDialect2);
|
|
990
|
+
for (const feat of doc.features) {
|
|
991
|
+
const props = feat.properties && typeof feat.properties === "object" && !Array.isArray(feat.properties) ? feat.properties : {};
|
|
992
|
+
const geom = feat.geometry && typeof feat.geometry === "object" && !Array.isArray(feat.geometry) ? feat.geometry : null;
|
|
993
|
+
const gType = geom ? String(geom.type ?? "") : "";
|
|
994
|
+
const coords = gType === "Point" && Array.isArray(geom?.coordinates) && geom.coordinates.length >= 2 ? geom.coordinates : void 0;
|
|
995
|
+
const fmtVal = (v, isProp = false) => {
|
|
996
|
+
if (v === void 0 || v === null) return "";
|
|
997
|
+
if (typeof v === "boolean") return v ? "True" : "False";
|
|
998
|
+
if (typeof v === "object") return isProp ? JSON.stringify(v) : "";
|
|
999
|
+
return String(v);
|
|
1000
|
+
};
|
|
1001
|
+
const geomJson = JSON.stringify(geom).replace(/":/g, '": ').replace(/,"/g, ', "');
|
|
1002
|
+
const rowCells = [
|
|
1003
|
+
fmtVal(feat.id),
|
|
1004
|
+
...propKeys.map((k) => fmtVal(props[k], true)),
|
|
1005
|
+
geomJson,
|
|
1006
|
+
gType,
|
|
1007
|
+
coords ? fmtVal(coords[0]) : "",
|
|
1008
|
+
coords ? fmtVal(coords[1]) : ""
|
|
1009
|
+
];
|
|
1010
|
+
out2 += writeCsvRow(rowCells, outDialect2);
|
|
1011
|
+
}
|
|
1012
|
+
return out2;
|
|
1013
|
+
}
|
|
1014
|
+
if (fmt === "xlsx") {
|
|
1015
|
+
if (sourceBytes[0] !== 80 || sourceBytes[1] !== 75) return void 0;
|
|
1016
|
+
const entries = readZipArchiveEntries(sourceBytes);
|
|
1017
|
+
if (entries.has("EncryptionInfo") || entries.has("EncryptedPackage")) return void 0;
|
|
1018
|
+
const wbXmlBytes = entries.get("xl/workbook.xml");
|
|
1019
|
+
if (!wbXmlBytes) return void 0;
|
|
1020
|
+
const wbXml = syncUtf8Decoder.decode(wbXmlBytes);
|
|
1021
|
+
const sheetNames = [];
|
|
1022
|
+
for (const m of wbXml.matchAll(/<sheet\b[^>]*\bname="([^"]+)"/g)) {
|
|
1023
|
+
sheetNames.push(m[1]);
|
|
1024
|
+
}
|
|
1025
|
+
if (sheetNames.length === 0) return void 0;
|
|
1026
|
+
if (namesOnly) {
|
|
1027
|
+
return sheetNames.map((n) => n + "\n").join("");
|
|
1028
|
+
}
|
|
1029
|
+
const targetIdx = sheetSpec ? sheetNames.indexOf(sheetSpec) : 0;
|
|
1030
|
+
if (targetIdx < 0) return void 0;
|
|
1031
|
+
const sheetBytes = entries.get(`xl/worksheets/sheet${targetIdx + 1}.xml`);
|
|
1032
|
+
if (!sheetBytes) return void 0;
|
|
1033
|
+
const sheetXml = syncUtf8Decoder.decode(sheetBytes);
|
|
1034
|
+
if (/<f(?:\s|>)/u.test(sheetXml)) return void 0;
|
|
1035
|
+
const sharedStrings = [];
|
|
1036
|
+
const sstBytes = entries.get("xl/sharedStrings.xml");
|
|
1037
|
+
if (sstBytes) {
|
|
1038
|
+
const sstXml = syncUtf8Decoder.decode(sstBytes);
|
|
1039
|
+
for (const si of sstXml.matchAll(/<si\b[\s\S]*?<\/si>/g)) {
|
|
1040
|
+
const parts = [...si[0].matchAll(/<t(?:\s[^>]*)?>([\s\S]*?)<\/t>/g)].map(
|
|
1041
|
+
(t) => (t[1] ?? "").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, "&")
|
|
1042
|
+
);
|
|
1043
|
+
sharedStrings.push(parts.join(""));
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
const csvRows = [];
|
|
1047
|
+
for (const rm of sheetXml.matchAll(/<row\b[\s\S]*?<\/row>/g)) {
|
|
1048
|
+
const cells = [];
|
|
1049
|
+
for (const cm of rm[0].matchAll(/<c\b([^>]*?)(?:\/>|>([\s\S]*?)<\/c>)/g)) {
|
|
1050
|
+
const attrs = cm[1] ?? "";
|
|
1051
|
+
const body = cm[2] ?? "";
|
|
1052
|
+
const refM = /\br="([A-Z]+)\d+"/i.exec(attrs);
|
|
1053
|
+
if (refM?.[1]) {
|
|
1054
|
+
let col = 0;
|
|
1055
|
+
for (const ch of refM[1].toUpperCase()) col = col * 26 + (ch.charCodeAt(0) - 64);
|
|
1056
|
+
col -= 1;
|
|
1057
|
+
while (cells.length < col) cells.push("");
|
|
1058
|
+
}
|
|
1059
|
+
if (cm[2] === void 0) {
|
|
1060
|
+
cells.push("");
|
|
1061
|
+
continue;
|
|
1062
|
+
}
|
|
1063
|
+
const tM = /\bt="([^"]+)"/.exec(attrs);
|
|
1064
|
+
const cType = tM?.[1] ?? "n";
|
|
1065
|
+
if (cType === "inlineStr") {
|
|
1066
|
+
const tv = /<t(?:\s[^>]*)?>([\s\S]*?)<\/t>/.exec(body);
|
|
1067
|
+
cells.push((tv?.[1] ?? "").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, "&"));
|
|
1068
|
+
} else {
|
|
1069
|
+
const vv = /<v>([\s\S]*?)<\/v>/.exec(body);
|
|
1070
|
+
const raw = (vv?.[1] ?? "").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, "&");
|
|
1071
|
+
cells.push(cType === "s" ? sharedStrings[Number.parseInt(raw, 10)] ?? "" : raw);
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
csvRows.push(cells);
|
|
1075
|
+
}
|
|
1076
|
+
const synthDialect = { delimiter: ",", quotechar: '"', doublequote: true, lineterminator: "\n" };
|
|
1077
|
+
const synthCsv = csvRows.map((r) => writeCsvRow(r, synthDialect)).join("");
|
|
1078
|
+
const synthBytes = new TextEncoder().encode(synthCsv);
|
|
1079
|
+
const table2 = loadSyncTypedTable(synthBytes, { ...opts, filePath: "-" }, void 0);
|
|
1080
|
+
if (!table2) return void 0;
|
|
1081
|
+
let out2 = opts.addBom ? "\uFEFF" : "";
|
|
1082
|
+
out2 += writeCsvRow(table2.headers, synthDialect);
|
|
1083
|
+
for (const row of table2.rows) {
|
|
1084
|
+
out2 += writeCsvRow(
|
|
1085
|
+
row.map((v) => v === null ? "" : typeof v === "object" && v.kind === "datetime" ? v.value.replace(" ", "T") : pythonValueText(v)),
|
|
1086
|
+
synthDialect
|
|
1087
|
+
);
|
|
1088
|
+
}
|
|
1089
|
+
return out2;
|
|
1090
|
+
}
|
|
1091
|
+
if (fmt === "fixed") {
|
|
1092
|
+
if (!schemaSpec || !readFileSync) return void 0;
|
|
1093
|
+
const schemaBytes = readFileSync(schemaSpec);
|
|
1094
|
+
if (!schemaBytes || schemaBytes.byteLength > 16384) return void 0;
|
|
1095
|
+
let schemaText = syncUtf8Decoder.decode(schemaBytes);
|
|
1096
|
+
if (schemaText.charCodeAt(0) === 65279) schemaText = schemaText.slice(1);
|
|
1097
|
+
const schemaRows = Array.from(readCsv(schemaText, { delimiter: ",", quotechar: '"', doublequote: true }), (record) => record.cells);
|
|
1098
|
+
if (schemaRows.length < 2) return void 0;
|
|
1099
|
+
const sHead = schemaRows[0];
|
|
1100
|
+
const colIdx = sHead.indexOf("column");
|
|
1101
|
+
const startIdx = sHead.indexOf("start");
|
|
1102
|
+
const lenIdx = sHead.indexOf("length");
|
|
1103
|
+
if (colIdx < 0 || startIdx < 0 || lenIdx < 0) return void 0;
|
|
1104
|
+
let oneBased;
|
|
1105
|
+
const fields = [];
|
|
1106
|
+
for (let r = 1; r < schemaRows.length; r++) {
|
|
1107
|
+
const row = schemaRows[r];
|
|
1108
|
+
if (row.length === 0 || row.length === 1 && row[0] === "") continue;
|
|
1109
|
+
const stNum = Number(row[startIdx]);
|
|
1110
|
+
const lnNum = Number(row[lenIdx]);
|
|
1111
|
+
if (!Number.isSafeInteger(stNum) || !Number.isSafeInteger(lnNum) || stNum < 0 || lnNum < 0) return void 0;
|
|
1112
|
+
oneBased ??= stNum === 1;
|
|
1113
|
+
fields.push({ name: row[colIdx], start: stNum - (oneBased ? 1 : 0), length: lnNum });
|
|
1114
|
+
}
|
|
1115
|
+
let inText = syncUtf8Decoder.decode(sourceBytes);
|
|
1116
|
+
if (inText.charCodeAt(0) === 65279) inText = inText.slice(1);
|
|
1117
|
+
const rawLines = inText.split(/\r?\n/);
|
|
1118
|
+
if (rawLines.length > 0 && rawLines[rawLines.length - 1] === "") rawLines.pop();
|
|
1119
|
+
const outDialect2 = { delimiter: ",", quotechar: '"', doublequote: true, lineterminator: "\n" };
|
|
1120
|
+
let out2 = opts.addBom ? "\uFEFF" : "";
|
|
1121
|
+
out2 += writeCsvRow(fields.map((f) => f.name), outDialect2);
|
|
1122
|
+
for (let idx = opts.skipLines; idx < rawLines.length; idx++) {
|
|
1123
|
+
const chars = Array.from(rawLines[idx]);
|
|
1124
|
+
const cells = fields.map((f) => chars.slice(f.start, f.start + f.length).join("").trim());
|
|
1125
|
+
out2 += writeCsvRow(cells, outDialect2);
|
|
1126
|
+
}
|
|
1127
|
+
return out2;
|
|
1128
|
+
}
|
|
1129
|
+
if (namesOnly) {
|
|
1130
|
+
if (fmt !== "json") return void 0;
|
|
1131
|
+
let text2 = syncUtf8Decoder.decode(sourceBytes);
|
|
1132
|
+
if (text2.charCodeAt(0) === 65279) text2 = text2.slice(1);
|
|
1133
|
+
const root = JSON.parse(text2);
|
|
1134
|
+
if (!root || typeof root !== "object" || Array.isArray(root)) return void 0;
|
|
1135
|
+
return Object.keys(root).map((k) => k + "\n").join("");
|
|
1136
|
+
}
|
|
1137
|
+
if (fmt === "csv") {
|
|
1138
|
+
const table2 = loadSyncTypedTable(inBytes, opts, readFileSync);
|
|
1139
|
+
if (!table2) return void 0;
|
|
1140
|
+
const outDialect2 = { delimiter: ",", quotechar: '"', doublequote: true, lineterminator: "\n" };
|
|
1141
|
+
let out2 = opts.addBom ? "\uFEFF" : "";
|
|
1142
|
+
out2 += writeCsvRow(table2.headers, outDialect2);
|
|
1143
|
+
for (const row of table2.rows) {
|
|
1144
|
+
out2 += writeCsvRow(
|
|
1145
|
+
row.map((v) => v === null ? "" : typeof v === "object" && v.kind === "datetime" ? v.value.replace(" ", "T") : pythonValueText(v)),
|
|
1146
|
+
outDialect2
|
|
1147
|
+
);
|
|
1148
|
+
}
|
|
1149
|
+
return out2;
|
|
1150
|
+
}
|
|
1151
|
+
if (fmt !== "json" && fmt !== "ndjson") return void 0;
|
|
1152
|
+
if (fmt === "ndjson" && keySpec !== void 0) return void 0;
|
|
1153
|
+
let text = syncUtf8Decoder.decode(sourceBytes);
|
|
1154
|
+
if (text.charCodeAt(0) === 65279) text = text.slice(1);
|
|
1155
|
+
let parsedItems;
|
|
1156
|
+
if (fmt === "ndjson") {
|
|
1157
|
+
parsedItems = [];
|
|
1158
|
+
for (const line of text.split(/\r?\n/)) {
|
|
1159
|
+
const trimmed = line.trim();
|
|
1160
|
+
if (!trimmed) continue;
|
|
1161
|
+
parsedItems.push(JSON.parse(trimmed));
|
|
1162
|
+
}
|
|
1163
|
+
} else {
|
|
1164
|
+
let root = JSON.parse(text);
|
|
1165
|
+
if (root && typeof root === "object" && !Array.isArray(root)) {
|
|
1166
|
+
if (!keySpec || !(keySpec in root)) return void 0;
|
|
1167
|
+
root = root[keySpec];
|
|
1168
|
+
}
|
|
1169
|
+
if (!Array.isArray(root)) return void 0;
|
|
1170
|
+
parsedItems = root;
|
|
1171
|
+
}
|
|
1172
|
+
const headers = [];
|
|
1173
|
+
const known = /* @__PURE__ */ new Set();
|
|
1174
|
+
const rawMaps = [];
|
|
1175
|
+
const flatten = (val, path, row) => {
|
|
1176
|
+
if (val !== null && typeof val === "object") {
|
|
1177
|
+
if (Array.isArray(val)) {
|
|
1178
|
+
for (let idx = 0; idx < val.length; idx++) flatten(val[idx], path + String(idx) + "/", row);
|
|
1179
|
+
} else {
|
|
1180
|
+
for (const [k, child] of Object.entries(val)) flatten(child, path + k + "/", row);
|
|
1181
|
+
}
|
|
1182
|
+
return;
|
|
1183
|
+
}
|
|
1184
|
+
let start = 0;
|
|
1185
|
+
let end = path.length;
|
|
1186
|
+
while (path[start] === "/") start++;
|
|
1187
|
+
while (end > start && path[end - 1] === "/") end--;
|
|
1188
|
+
const name = path.slice(start, end);
|
|
1189
|
+
if (!name) throw new Error("empty header");
|
|
1190
|
+
const cell = val === null || val === void 0 ? null : typeof val === "boolean" ? val ? "True" : "False" : String(val);
|
|
1191
|
+
row.set(name, cell);
|
|
1192
|
+
};
|
|
1193
|
+
for (const item of parsedItems) {
|
|
1194
|
+
if (item === null || typeof item !== "object" || Array.isArray(item)) return void 0;
|
|
1195
|
+
const row = /* @__PURE__ */ new Map();
|
|
1196
|
+
flatten(item, "", row);
|
|
1197
|
+
for (const name of row.keys()) {
|
|
1198
|
+
if (!known.has(name)) {
|
|
1199
|
+
known.add(name);
|
|
1200
|
+
headers.push(name);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
rawMaps.push(row);
|
|
1204
|
+
}
|
|
1205
|
+
if (headers.length === 0) return void 0;
|
|
1206
|
+
const stringRows = rawMaps.map((m, rIdx) => {
|
|
1207
|
+
const cells = headers.map((h) => m.get(h) ?? "");
|
|
1208
|
+
return opts.lineNumbers ? [String(rIdx + 1), ...cells] : cells;
|
|
1209
|
+
});
|
|
1210
|
+
const finalHeaders = opts.lineNumbers ? ["line_number", ...headers] : headers;
|
|
1211
|
+
const inferenceOpts = {
|
|
1212
|
+
now: Date.now(),
|
|
1213
|
+
timezone: "UTC",
|
|
1214
|
+
noInference: opts.noInference,
|
|
1215
|
+
numberTextOnly: false,
|
|
1216
|
+
blanks: opts.blanks,
|
|
1217
|
+
nullValues: [],
|
|
1218
|
+
noLeadingZeroes: opts.noLeadingZeroes,
|
|
1219
|
+
maxDecimalDigits: Infinity,
|
|
1220
|
+
maxDecimalExponent: Infinity
|
|
1221
|
+
};
|
|
1222
|
+
const table = inferTable(finalHeaders, stringRows, inferenceOpts);
|
|
1223
|
+
const outDialect = { delimiter: ",", quotechar: '"', doublequote: true, lineterminator: "\n" };
|
|
1224
|
+
let out = opts.addBom ? "\uFEFF" : "";
|
|
1225
|
+
out += writeCsvRow(table.headers, outDialect);
|
|
1226
|
+
for (const row of table.rows) {
|
|
1227
|
+
out += writeCsvRow(
|
|
1228
|
+
row.map((v) => v === null ? "" : typeof v === "object" && v.kind === "datetime" ? v.value.replace(" ", "T") : pythonValueText(v)),
|
|
1229
|
+
outDialect
|
|
1230
|
+
);
|
|
1231
|
+
}
|
|
1232
|
+
return out;
|
|
1233
|
+
} catch {
|
|
1234
|
+
return void 0;
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
function evalSyncCsvstack(inBytes, opArgs, readFileSync) {
|
|
1238
|
+
try {
|
|
1239
|
+
let groupsSpec;
|
|
1240
|
+
let groupName = "group";
|
|
1241
|
+
let groupByFilenames = false;
|
|
1242
|
+
let noHeaderRow = false;
|
|
1243
|
+
let lineNumbers = false;
|
|
1244
|
+
let addBom = false;
|
|
1245
|
+
let delimiter = ",";
|
|
1246
|
+
let tabs = false;
|
|
1247
|
+
const files = [];
|
|
1248
|
+
let posDone = false;
|
|
1249
|
+
for (let i = 0; i < opArgs.length; i++) {
|
|
1250
|
+
const a = opArgs[i];
|
|
1251
|
+
if (!posDone && a === "--") {
|
|
1252
|
+
posDone = true;
|
|
1253
|
+
continue;
|
|
1254
|
+
}
|
|
1255
|
+
if (!posDone && a.startsWith("-") && a !== "-") {
|
|
1256
|
+
if (a === "-H" || a === "--no-header-row") {
|
|
1257
|
+
noHeaderRow = true;
|
|
1258
|
+
continue;
|
|
1259
|
+
}
|
|
1260
|
+
if (a === "-l" || a === "--linenumbers") {
|
|
1261
|
+
lineNumbers = true;
|
|
1262
|
+
continue;
|
|
1263
|
+
}
|
|
1264
|
+
if (a === "--add-bom") {
|
|
1265
|
+
addBom = true;
|
|
1266
|
+
continue;
|
|
1267
|
+
}
|
|
1268
|
+
if (a === "-t" || a === "--tabs") {
|
|
1269
|
+
tabs = true;
|
|
1270
|
+
continue;
|
|
1271
|
+
}
|
|
1272
|
+
if (a === "-d" || a === "--delimiter" || a.startsWith("--delimiter=")) {
|
|
1273
|
+
const v = a.startsWith("--delimiter=") ? a.slice(12) : opArgs[++i];
|
|
1274
|
+
if (!v || Array.from(v).length !== 1) return void 0;
|
|
1275
|
+
delimiter = v;
|
|
1276
|
+
continue;
|
|
1277
|
+
}
|
|
1278
|
+
if (a === "--filenames") {
|
|
1279
|
+
groupByFilenames = true;
|
|
1280
|
+
continue;
|
|
1281
|
+
}
|
|
1282
|
+
if (a === "-g" || a === "--groups") {
|
|
1283
|
+
const v = opArgs[++i];
|
|
1284
|
+
if (v === void 0) return void 0;
|
|
1285
|
+
groupsSpec = v;
|
|
1286
|
+
continue;
|
|
1287
|
+
}
|
|
1288
|
+
if (a === "-n" || a === "--group-name") {
|
|
1289
|
+
const v = opArgs[++i];
|
|
1290
|
+
if (v === void 0) return void 0;
|
|
1291
|
+
groupName = v;
|
|
1292
|
+
continue;
|
|
1293
|
+
}
|
|
1294
|
+
return void 0;
|
|
1295
|
+
}
|
|
1296
|
+
files.push(a);
|
|
1297
|
+
}
|
|
1298
|
+
if (files.length === 0) files.push("-");
|
|
1299
|
+
if (noHeaderRow) return void 0;
|
|
1300
|
+
const groups = groupsSpec !== void 0 && !groupByFilenames ? groupsSpec.split(",") : void 0;
|
|
1301
|
+
if (groups && groups.length !== files.length) return void 0;
|
|
1302
|
+
const grouped = groupsSpec !== void 0 || groupByFilenames;
|
|
1303
|
+
const parsedFiles = [];
|
|
1304
|
+
const unionHeaders = [];
|
|
1305
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1306
|
+
for (let idx = 0; idx < files.length; idx++) {
|
|
1307
|
+
const f = files[idx];
|
|
1308
|
+
const bytes = f === "-" ? inBytes : readFileSync ? readFileSync(f) : void 0;
|
|
1309
|
+
if (!bytes || bytes.byteLength > 16384) return void 0;
|
|
1310
|
+
let text = syncUtf8Decoder.decode(bytes);
|
|
1311
|
+
if (text.charCodeAt(0) === 65279) text = text.slice(1);
|
|
1312
|
+
const recs = [...readCsv(text, { delimiter: tabs ? " " : delimiter, quotechar: '"', doublequote: true, skipinitialspace: false })];
|
|
1313
|
+
const hdr = recs[0] ? recs[0].cells.map((c) => pythonValueText(c)) : [];
|
|
1314
|
+
for (const h of hdr) {
|
|
1315
|
+
if (!seen.has(h)) {
|
|
1316
|
+
seen.add(h);
|
|
1317
|
+
unionHeaders.push(h);
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
const groupVal = groups ? groups[idx] : f.split("/").at(-1);
|
|
1321
|
+
parsedFiles.push({
|
|
1322
|
+
group: groupVal,
|
|
1323
|
+
headers: hdr,
|
|
1324
|
+
rows: recs.slice(1).map((r) => r.cells.map((c) => pythonValueText(c)))
|
|
1325
|
+
});
|
|
1326
|
+
}
|
|
1327
|
+
const outHeaders = grouped ? [groupName, ...unionHeaders] : [...unionHeaders];
|
|
1328
|
+
const outDialect = { delimiter: ",", quotechar: '"', doublequote: true, lineterminator: "\n" };
|
|
1329
|
+
let out = addBom ? "\uFEFF" : "";
|
|
1330
|
+
out += writeCsvRow(lineNumbers ? ["line_number", ...outHeaders] : outHeaders, outDialect);
|
|
1331
|
+
let lineNum = 1;
|
|
1332
|
+
for (const pf of parsedFiles) {
|
|
1333
|
+
for (const r of pf.rows) {
|
|
1334
|
+
const rowMap = /* @__PURE__ */ new Map();
|
|
1335
|
+
for (let c = 0; c < pf.headers.length; c++) rowMap.set(pf.headers[c], r[c] ?? "");
|
|
1336
|
+
const cells = unionHeaders.map((h) => rowMap.get(h) ?? "");
|
|
1337
|
+
if (grouped) cells.unshift(pf.group);
|
|
1338
|
+
if (lineNumbers) cells.unshift(String(lineNum++));
|
|
1339
|
+
out += writeCsvRow(cells, outDialect);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
return out;
|
|
1343
|
+
} catch {
|
|
1344
|
+
return void 0;
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
function evalSyncCsvjoin(inBytes, opArgs, readFileSync) {
|
|
1348
|
+
try {
|
|
1349
|
+
let columnsSpec;
|
|
1350
|
+
let joinMode = "inner";
|
|
1351
|
+
let noInference = false;
|
|
1352
|
+
let addBom = false;
|
|
1353
|
+
let delimiter;
|
|
1354
|
+
let tabs = false;
|
|
1355
|
+
const files = [];
|
|
1356
|
+
let posDone = false;
|
|
1357
|
+
for (let i = 0; i < opArgs.length; i++) {
|
|
1358
|
+
const a = opArgs[i];
|
|
1359
|
+
if (!posDone && a === "--") {
|
|
1360
|
+
posDone = true;
|
|
1361
|
+
continue;
|
|
1362
|
+
}
|
|
1363
|
+
if (!posDone && a.startsWith("-") && a !== "-") {
|
|
1364
|
+
if (a === "--left") {
|
|
1365
|
+
joinMode = "left";
|
|
1366
|
+
continue;
|
|
1367
|
+
}
|
|
1368
|
+
if (a === "--right") {
|
|
1369
|
+
joinMode = "right";
|
|
1370
|
+
continue;
|
|
1371
|
+
}
|
|
1372
|
+
if (a === "--outer") {
|
|
1373
|
+
joinMode = "outer";
|
|
1374
|
+
continue;
|
|
1375
|
+
}
|
|
1376
|
+
if (a === "-I" || a === "--no-inference") {
|
|
1377
|
+
noInference = true;
|
|
1378
|
+
continue;
|
|
1379
|
+
}
|
|
1380
|
+
if (a === "--add-bom") {
|
|
1381
|
+
addBom = true;
|
|
1382
|
+
continue;
|
|
1383
|
+
}
|
|
1384
|
+
if (a === "-t" || a === "--tabs") {
|
|
1385
|
+
tabs = true;
|
|
1386
|
+
continue;
|
|
1387
|
+
}
|
|
1388
|
+
if (a === "-d" || a === "--delimiter" || a.startsWith("--delimiter=")) {
|
|
1389
|
+
const v = a.startsWith("--delimiter=") ? a.slice(12) : opArgs[++i];
|
|
1390
|
+
if (!v || Array.from(v).length !== 1) return void 0;
|
|
1391
|
+
delimiter = v;
|
|
1392
|
+
continue;
|
|
1393
|
+
}
|
|
1394
|
+
if (a === "-c" || a === "--columns") {
|
|
1395
|
+
const v = opArgs[++i];
|
|
1396
|
+
if (v === void 0) return void 0;
|
|
1397
|
+
columnsSpec = v;
|
|
1398
|
+
continue;
|
|
1399
|
+
}
|
|
1400
|
+
return void 0;
|
|
1401
|
+
}
|
|
1402
|
+
files.push(a);
|
|
1403
|
+
}
|
|
1404
|
+
if (files.length !== 2) return void 0;
|
|
1405
|
+
const leftOpts = {
|
|
1406
|
+
...delimiter === void 0 ? {} : { delimiter },
|
|
1407
|
+
tabs,
|
|
1408
|
+
noDoublequote: false,
|
|
1409
|
+
skipInitialSpace: false,
|
|
1410
|
+
noHeaderRow: false,
|
|
1411
|
+
skipLines: 0,
|
|
1412
|
+
lineNumbers: false,
|
|
1413
|
+
addBom: false,
|
|
1414
|
+
noInference,
|
|
1415
|
+
blanks: false,
|
|
1416
|
+
noLeadingZeroes: false,
|
|
1417
|
+
sniffLimit: 1024,
|
|
1418
|
+
noNumberEllipsis: false,
|
|
1419
|
+
indent: null,
|
|
1420
|
+
key: null,
|
|
1421
|
+
streamOutput: false,
|
|
1422
|
+
filePath: files[0]
|
|
1423
|
+
};
|
|
1424
|
+
const rightOpts = { ...leftOpts, filePath: files[1] };
|
|
1425
|
+
const leftTable = loadSyncTypedTable(inBytes, leftOpts, readFileSync);
|
|
1426
|
+
const rightTable = loadSyncTypedTable(inBytes, rightOpts, readFileSync);
|
|
1427
|
+
if (!leftTable || !rightTable) return void 0;
|
|
1428
|
+
if (!columnsSpec) {
|
|
1429
|
+
if (joinMode !== "inner") return void 0;
|
|
1430
|
+
const outHeaders2 = [
|
|
1431
|
+
...leftTable.headers,
|
|
1432
|
+
...rightTable.headers.map((h) => leftTable.headers.includes(h) ? h + "2" : h)
|
|
1433
|
+
];
|
|
1434
|
+
if (new Set(outHeaders2).size !== outHeaders2.length) return void 0;
|
|
1435
|
+
const maxRows = Math.max(leftTable.rows.length, rightTable.rows.length);
|
|
1436
|
+
const outDialect2 = { delimiter: ",", quotechar: '"', doublequote: true, lineterminator: "\n" };
|
|
1437
|
+
let out2 = addBom ? "\uFEFF" : "";
|
|
1438
|
+
out2 += writeCsvRow(outHeaders2, outDialect2);
|
|
1439
|
+
for (let r = 0; r < maxRows; r++) {
|
|
1440
|
+
const lRow = leftTable.rows[r] ?? leftTable.headers.map(() => null);
|
|
1441
|
+
const rRow = rightTable.rows[r] ?? rightTable.headers.map(() => null);
|
|
1442
|
+
out2 += writeCsvRow(
|
|
1443
|
+
[...lRow, ...rRow].map((v) => v === null ? "" : typeof v === "object" && v.kind === "datetime" ? v.value.replace(" ", "T") : pythonValueText(v)),
|
|
1444
|
+
outDialect2
|
|
1445
|
+
);
|
|
1446
|
+
}
|
|
1447
|
+
return out2;
|
|
1448
|
+
}
|
|
1449
|
+
const colParts = columnsSpec.split(",");
|
|
1450
|
+
if (colParts.length !== 1 && colParts.length !== 2) return void 0;
|
|
1451
|
+
const leftKey = matchColumnIdentifier(leftTable.headers, colParts[0], 1);
|
|
1452
|
+
const rightKey = matchColumnIdentifier(rightTable.headers, colParts[1] ?? colParts[0], 1);
|
|
1453
|
+
const full = joinMode === "outer" || joinMode === "right";
|
|
1454
|
+
const inner = joinMode === "inner";
|
|
1455
|
+
const keyVal = (v) => {
|
|
1456
|
+
if (v === null) return "null";
|
|
1457
|
+
if (typeof v === "string") return "text:" + v;
|
|
1458
|
+
if (typeof v === "boolean") return v ? "number:1:0" : "number:0";
|
|
1459
|
+
if (v.kind === "decimal") {
|
|
1460
|
+
const d = Decimal.parse(v.value).normalized();
|
|
1461
|
+
return "number:" + d.toString();
|
|
1462
|
+
}
|
|
1463
|
+
return "other:" + pythonValueText(v);
|
|
1464
|
+
};
|
|
1465
|
+
const includedRight = rightTable.headers.map((_, idx) => idx).filter((idx) => full || idx !== rightKey);
|
|
1466
|
+
const outHeaders = [
|
|
1467
|
+
...leftTable.headers,
|
|
1468
|
+
...includedRight.map((idx) => leftTable.headers.includes(rightTable.headers[idx]) ? rightTable.headers[idx] + "2" : rightTable.headers[idx])
|
|
1469
|
+
];
|
|
1470
|
+
if (new Set(outHeaders).size !== outHeaders.length) return void 0;
|
|
1471
|
+
const rightHash = /* @__PURE__ */ new Map();
|
|
1472
|
+
const rightKeys = [];
|
|
1473
|
+
for (const row of rightTable.rows) {
|
|
1474
|
+
const k = keyVal(row[rightKey]);
|
|
1475
|
+
rightKeys.push(k);
|
|
1476
|
+
const list = rightHash.get(k);
|
|
1477
|
+
if (list) list.push(row);
|
|
1478
|
+
else rightHash.set(k, [row]);
|
|
1479
|
+
}
|
|
1480
|
+
const joinedRows = [];
|
|
1481
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
1482
|
+
for (const lRow of leftTable.rows) {
|
|
1483
|
+
const k = keyVal(lRow[leftKey]);
|
|
1484
|
+
const matches = k === "null" ? void 0 : rightHash.get(k);
|
|
1485
|
+
if (matches) {
|
|
1486
|
+
seenKeys.add(k);
|
|
1487
|
+
for (const rRow of matches) {
|
|
1488
|
+
joinedRows.push([...lRow, ...includedRight.map((idx) => rRow[idx])]);
|
|
1489
|
+
}
|
|
1490
|
+
} else if (!inner && joinMode !== "right") {
|
|
1491
|
+
joinedRows.push([...lRow, ...includedRight.map(() => null)]);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
if (full) {
|
|
1495
|
+
for (let i = 0; i < rightTable.rows.length; i++) {
|
|
1496
|
+
const k = rightKeys[i];
|
|
1497
|
+
if (k === "null" || !seenKeys.has(k)) {
|
|
1498
|
+
const rRow = rightTable.rows[i];
|
|
1499
|
+
joinedRows.push([...leftTable.headers.map(() => null), ...includedRight.map((idx) => rRow[idx])]);
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
const outDialect = { delimiter: ",", quotechar: '"', doublequote: true, lineterminator: "\n" };
|
|
1504
|
+
let out = addBom ? "\uFEFF" : "";
|
|
1505
|
+
out += writeCsvRow(outHeaders, outDialect);
|
|
1506
|
+
for (const row of joinedRows) {
|
|
1507
|
+
out += writeCsvRow(
|
|
1508
|
+
row.map((v) => v === null ? "" : typeof v === "object" && v.kind === "datetime" ? v.value.replace(" ", "T") : pythonValueText(v)),
|
|
1509
|
+
outDialect
|
|
1510
|
+
);
|
|
1511
|
+
}
|
|
1512
|
+
return out;
|
|
1513
|
+
} catch {
|
|
1514
|
+
return void 0;
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
// packages/safe-bash/src/commands/csvkit/index.ts
|
|
1519
|
+
import { createCsvkitCommands as createRawCsvkitCommands } from "../../safe-bash-command-csvkit/index.js";
|
|
1520
|
+
export * from "../../safe-bash-command-csvkit/index.js";
|
|
1521
|
+
function isDefaultCsvkitOptions(options) {
|
|
1522
|
+
if (!options) return true;
|
|
1523
|
+
return options.limits === void 0 && options.codecs === void 0 && options.locale === void 0 && options.clock === void 0 && options.terminal === void 0 && options.compression === void 0 && options.databases === void 0 && options.sqlDialects === void 0 && options.interpreter === void 0 && options.openMatchFile === void 0 && options.sniffing === void 0 && options.columnWarnings === void 0 && options.probeInputOpen === void 0;
|
|
1524
|
+
}
|
|
1525
|
+
function createCsvkitCommands(options = {}) {
|
|
1526
|
+
const defs = createRawCsvkitCommands(options);
|
|
1527
|
+
Object.assign(internal_exports.syncCommandEvaluators, { evalSyncCsvlook, evalSyncCsvjson, evalSyncCsvsort, evalSyncCsvformat, evalSyncCsvstat, evalSyncIn2csv, evalSyncCsvstack, evalSyncCsvjoin });
|
|
1528
|
+
if (isDefaultCsvkitOptions(options)) {
|
|
1529
|
+
for (let i = 0; i < defs.length; i++) internal_exports.builtInDirectContextExecutors.add(defs[i].execute);
|
|
1530
|
+
}
|
|
1531
|
+
return defs;
|
|
1532
|
+
}
|
|
1533
|
+
function csvkitCommands(options = {}) {
|
|
1534
|
+
const commands = createCsvkitCommands(options);
|
|
1535
|
+
const replace = options.replace ?? false;
|
|
1536
|
+
return {
|
|
1537
|
+
name: "csvkit-commands",
|
|
1538
|
+
setup(host) {
|
|
1539
|
+
if (!replace) {
|
|
1540
|
+
for (const command of commands) {
|
|
1541
|
+
const existing = host.commands.get(command.name);
|
|
1542
|
+
if (existing && !(command.fallback && !existing.fallback)) {
|
|
1543
|
+
throw new Error(`Command already registered: ${command.name}`);
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
for (const command of commands) {
|
|
1548
|
+
host.commands.register(command, { replace });
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
export {
|
|
1554
|
+
createCsvkitCommands,
|
|
1555
|
+
csvkitCommands,
|
|
1556
|
+
evalSyncCsvformat,
|
|
1557
|
+
evalSyncCsvjoin,
|
|
1558
|
+
evalSyncCsvjson,
|
|
1559
|
+
evalSyncCsvlook,
|
|
1560
|
+
evalSyncCsvsort,
|
|
1561
|
+
evalSyncCsvstack,
|
|
1562
|
+
evalSyncCsvstat,
|
|
1563
|
+
evalSyncIn2csv
|
|
1564
|
+
};
|