@gzl10/ts-helpers 4.2.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/CHANGELOG.md +320 -0
- package/README.md +233 -0
- package/USAGE-GUIDE.md +800 -0
- package/dist/browser/async.js +15 -0
- package/dist/browser/async.js.map +1 -0
- package/dist/browser/chunk-4O7ZPIJN.js +383 -0
- package/dist/browser/chunk-4O7ZPIJN.js.map +1 -0
- package/dist/browser/chunk-75XNTC34.js +60 -0
- package/dist/browser/chunk-75XNTC34.js.map +1 -0
- package/dist/browser/chunk-C3D7YZVE.js +299 -0
- package/dist/browser/chunk-C3D7YZVE.js.map +1 -0
- package/dist/browser/chunk-CZL6C2EI.js +452 -0
- package/dist/browser/chunk-CZL6C2EI.js.map +1 -0
- package/dist/browser/chunk-D4FZFIVA.js +240 -0
- package/dist/browser/chunk-D4FZFIVA.js.map +1 -0
- package/dist/browser/chunk-IL7NG7IC.js +72 -0
- package/dist/browser/chunk-IL7NG7IC.js.map +1 -0
- package/dist/browser/chunk-NSBPE2FW.js +17 -0
- package/dist/browser/chunk-NSBPE2FW.js.map +1 -0
- package/dist/browser/chunk-SLQVNPTH.js +27 -0
- package/dist/browser/chunk-SLQVNPTH.js.map +1 -0
- package/dist/browser/chunk-WG7ILCUB.js +195 -0
- package/dist/browser/chunk-WG7ILCUB.js.map +1 -0
- package/dist/browser/chunk-WJA4JDMZ.js +278 -0
- package/dist/browser/chunk-WJA4JDMZ.js.map +1 -0
- package/dist/browser/chunk-ZFVYLUTT.js +65 -0
- package/dist/browser/chunk-ZFVYLUTT.js.map +1 -0
- package/dist/browser/chunk-ZYTSVMTI.js +263 -0
- package/dist/browser/chunk-ZYTSVMTI.js.map +1 -0
- package/dist/browser/dates.js +78 -0
- package/dist/browser/dates.js.map +1 -0
- package/dist/browser/environment-detection.js +21 -0
- package/dist/browser/environment-detection.js.map +1 -0
- package/dist/browser/environment.js +34 -0
- package/dist/browser/environment.js.map +1 -0
- package/dist/browser/errors.js +18 -0
- package/dist/browser/errors.js.map +1 -0
- package/dist/browser/index.js +412 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/math.js +51 -0
- package/dist/browser/math.js.map +1 -0
- package/dist/browser/number.js +10 -0
- package/dist/browser/number.js.map +1 -0
- package/dist/browser/objects.js +31 -0
- package/dist/browser/objects.js.map +1 -0
- package/dist/browser/strings.js +80 -0
- package/dist/browser/strings.js.map +1 -0
- package/dist/browser/validation-core.js +54 -0
- package/dist/browser/validation-core.js.map +1 -0
- package/dist/browser/validation-crypto.js +28 -0
- package/dist/browser/validation-crypto.js.map +1 -0
- package/dist/browser/validators.js +98 -0
- package/dist/browser/validators.js.map +1 -0
- package/dist/cjs/async.js +86 -0
- package/dist/cjs/async.js.map +1 -0
- package/dist/cjs/dates.js +285 -0
- package/dist/cjs/dates.js.map +1 -0
- package/dist/cjs/environment-detection.js +84 -0
- package/dist/cjs/environment-detection.js.map +1 -0
- package/dist/cjs/environment.js +261 -0
- package/dist/cjs/environment.js.map +1 -0
- package/dist/cjs/errors.js +80 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/index.js +2035 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/math.js +388 -0
- package/dist/cjs/math.js.map +1 -0
- package/dist/cjs/number.js +37 -0
- package/dist/cjs/number.js.map +1 -0
- package/dist/cjs/objects.js +249 -0
- package/dist/cjs/objects.js.map +1 -0
- package/dist/cjs/strings.js +253 -0
- package/dist/cjs/strings.js.map +1 -0
- package/dist/cjs/validation.js +450 -0
- package/dist/cjs/validation.js.map +1 -0
- package/dist/esm/async.js +15 -0
- package/dist/esm/async.js.map +1 -0
- package/dist/esm/chunk-4O7ZPIJN.js +383 -0
- package/dist/esm/chunk-4O7ZPIJN.js.map +1 -0
- package/dist/esm/chunk-75XNTC34.js +60 -0
- package/dist/esm/chunk-75XNTC34.js.map +1 -0
- package/dist/esm/chunk-BDOBKBKA.js +72 -0
- package/dist/esm/chunk-BDOBKBKA.js.map +1 -0
- package/dist/esm/chunk-C3D7YZVE.js +299 -0
- package/dist/esm/chunk-C3D7YZVE.js.map +1 -0
- package/dist/esm/chunk-CZL6C2EI.js +452 -0
- package/dist/esm/chunk-CZL6C2EI.js.map +1 -0
- package/dist/esm/chunk-EBLSTOEC.js +263 -0
- package/dist/esm/chunk-EBLSTOEC.js.map +1 -0
- package/dist/esm/chunk-NSBPE2FW.js +17 -0
- package/dist/esm/chunk-NSBPE2FW.js.map +1 -0
- package/dist/esm/chunk-SLQVNPTH.js +27 -0
- package/dist/esm/chunk-SLQVNPTH.js.map +1 -0
- package/dist/esm/chunk-WG7ILCUB.js +195 -0
- package/dist/esm/chunk-WG7ILCUB.js.map +1 -0
- package/dist/esm/chunk-WJA4JDMZ.js +278 -0
- package/dist/esm/chunk-WJA4JDMZ.js.map +1 -0
- package/dist/esm/chunk-ZFVYLUTT.js +65 -0
- package/dist/esm/chunk-ZFVYLUTT.js.map +1 -0
- package/dist/esm/dates.js +78 -0
- package/dist/esm/dates.js.map +1 -0
- package/dist/esm/environment-detection.js +21 -0
- package/dist/esm/environment-detection.js.map +1 -0
- package/dist/esm/environment.js +34 -0
- package/dist/esm/environment.js.map +1 -0
- package/dist/esm/errors.js +18 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/index.js +380 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/math.js +51 -0
- package/dist/esm/math.js.map +1 -0
- package/dist/esm/number.js +10 -0
- package/dist/esm/number.js.map +1 -0
- package/dist/esm/objects.js +31 -0
- package/dist/esm/objects.js.map +1 -0
- package/dist/esm/strings.js +80 -0
- package/dist/esm/strings.js.map +1 -0
- package/dist/esm/validation.js +54 -0
- package/dist/esm/validation.js.map +1 -0
- package/dist/node/async.js +93 -0
- package/dist/node/async.js.map +1 -0
- package/dist/node/csv.js +102 -0
- package/dist/node/csv.js.map +1 -0
- package/dist/node/data.js +880 -0
- package/dist/node/data.js.map +1 -0
- package/dist/node/dates.js +324 -0
- package/dist/node/dates.js.map +1 -0
- package/dist/node/environment.js +278 -0
- package/dist/node/environment.js.map +1 -0
- package/dist/node/errors.js +89 -0
- package/dist/node/errors.js.map +1 -0
- package/dist/node/index.js +3151 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/json.js +107 -0
- package/dist/node/json.js.map +1 -0
- package/dist/node/math.js +413 -0
- package/dist/node/math.js.map +1 -0
- package/dist/node/number.js +42 -0
- package/dist/node/number.js.map +1 -0
- package/dist/node/objects.js +264 -0
- package/dist/node/objects.js.map +1 -0
- package/dist/node/strings.js +293 -0
- package/dist/node/strings.js.map +1 -0
- package/dist/node/tree.js +89 -0
- package/dist/node/tree.js.map +1 -0
- package/dist/node/validation-core.js +477 -0
- package/dist/node/validation-core.js.map +1 -0
- package/dist/node/validation-crypto.js +179 -0
- package/dist/node/validation-crypto.js.map +1 -0
- package/dist/node/validation.js +677 -0
- package/dist/node/validation.js.map +1 -0
- package/dist/node/validators.js +123 -0
- package/dist/node/validators.js.map +1 -0
- package/dist/node-esm/async.js +15 -0
- package/dist/node-esm/async.js.map +1 -0
- package/dist/node-esm/chunk-3YOF7NPT.js +299 -0
- package/dist/node-esm/chunk-3YOF7NPT.js.map +1 -0
- package/dist/node-esm/chunk-64TBXJQS.js +263 -0
- package/dist/node-esm/chunk-64TBXJQS.js.map +1 -0
- package/dist/node-esm/chunk-75XNTC34.js +60 -0
- package/dist/node-esm/chunk-75XNTC34.js.map +1 -0
- package/dist/node-esm/chunk-C4PKXIPB.js +278 -0
- package/dist/node-esm/chunk-C4PKXIPB.js.map +1 -0
- package/dist/node-esm/chunk-CMDFZME3.js +452 -0
- package/dist/node-esm/chunk-CMDFZME3.js.map +1 -0
- package/dist/node-esm/chunk-DZZPUYMP.js +74 -0
- package/dist/node-esm/chunk-DZZPUYMP.js.map +1 -0
- package/dist/node-esm/chunk-HTSEHRHI.js +195 -0
- package/dist/node-esm/chunk-HTSEHRHI.js.map +1 -0
- package/dist/node-esm/chunk-JCAUVOPH.js +27 -0
- package/dist/node-esm/chunk-JCAUVOPH.js.map +1 -0
- package/dist/node-esm/chunk-KBHE3K2F.js +505 -0
- package/dist/node-esm/chunk-KBHE3K2F.js.map +1 -0
- package/dist/node-esm/chunk-LYTET5NX.js +65 -0
- package/dist/node-esm/chunk-LYTET5NX.js.map +1 -0
- package/dist/node-esm/chunk-PZ5AY32C.js +10 -0
- package/dist/node-esm/chunk-PZ5AY32C.js.map +1 -0
- package/dist/node-esm/chunk-UKGXL2QO.js +383 -0
- package/dist/node-esm/chunk-UKGXL2QO.js.map +1 -0
- package/dist/node-esm/chunk-XAEYT23H.js +164 -0
- package/dist/node-esm/chunk-XAEYT23H.js.map +1 -0
- package/dist/node-esm/csv.js +63 -0
- package/dist/node-esm/csv.js.map +1 -0
- package/dist/node-esm/data.js +32 -0
- package/dist/node-esm/data.js.map +1 -0
- package/dist/node-esm/dates.js +78 -0
- package/dist/node-esm/dates.js.map +1 -0
- package/dist/node-esm/environment.js +34 -0
- package/dist/node-esm/environment.js.map +1 -0
- package/dist/node-esm/errors.js +18 -0
- package/dist/node-esm/errors.js.map +1 -0
- package/dist/node-esm/index.js +426 -0
- package/dist/node-esm/index.js.map +1 -0
- package/dist/node-esm/json.js +68 -0
- package/dist/node-esm/json.js.map +1 -0
- package/dist/node-esm/math.js +51 -0
- package/dist/node-esm/math.js.map +1 -0
- package/dist/node-esm/number.js +10 -0
- package/dist/node-esm/number.js.map +1 -0
- package/dist/node-esm/objects.js +31 -0
- package/dist/node-esm/objects.js.map +1 -0
- package/dist/node-esm/strings.js +80 -0
- package/dist/node-esm/strings.js.map +1 -0
- package/dist/node-esm/tree.js +8 -0
- package/dist/node-esm/tree.js.map +1 -0
- package/dist/node-esm/validation-core.js +54 -0
- package/dist/node-esm/validation-core.js.map +1 -0
- package/dist/node-esm/validation-crypto.js +26 -0
- package/dist/node-esm/validation-crypto.js.map +1 -0
- package/dist/node-esm/validation.js +606 -0
- package/dist/node-esm/validation.js.map +1 -0
- package/dist/node-esm/validators.js +98 -0
- package/dist/node-esm/validators.js.map +1 -0
- package/dist/types/async-C8gvbSG-.d.ts +453 -0
- package/dist/types/async.d.ts +1 -0
- package/dist/types/csv.d.ts +226 -0
- package/dist/types/data.d.ts +1561 -0
- package/dist/types/dates-hTiE0Z11.d.ts +298 -0
- package/dist/types/dates.d.ts +1 -0
- package/dist/types/environment-B8eLS7KT.d.ts +420 -0
- package/dist/types/environment-detection.d.ts +102 -0
- package/dist/types/environment.d.ts +1 -0
- package/dist/types/errors.d.ts +147 -0
- package/dist/types/index.d.ts +211 -0
- package/dist/types/json.d.ts +284 -0
- package/dist/types/math-BQ9Lwdp7.d.ts +2060 -0
- package/dist/types/math.d.ts +1 -0
- package/dist/types/number-CYnQfLWj.d.ts +44 -0
- package/dist/types/number.d.ts +1 -0
- package/dist/types/objects-BohS8GCS.d.ts +1185 -0
- package/dist/types/objects.d.ts +1 -0
- package/dist/types/strings-CiqRPYLL.d.ts +1349 -0
- package/dist/types/strings.d.ts +1 -0
- package/dist/types/tree.d.ts +284 -0
- package/dist/types/validation-core-DfHF8rCG.d.ts +238 -0
- package/dist/types/validation-crypto-browser.d.ts +56 -0
- package/dist/types/validation-crypto-node.d.ts +31 -0
- package/dist/types/validation.d.ts +1 -0
- package/dist/types/validators.d.ts +216 -0
- package/package.json +253 -0
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataError,
|
|
3
|
+
createValidationError
|
|
4
|
+
} from "./chunk-75XNTC34.js";
|
|
5
|
+
import {
|
|
6
|
+
isBrowser,
|
|
7
|
+
isNode
|
|
8
|
+
} from "./chunk-64TBXJQS.js";
|
|
9
|
+
import {
|
|
10
|
+
__export
|
|
11
|
+
} from "./chunk-PZ5AY32C.js";
|
|
12
|
+
|
|
13
|
+
// src/data.ts
|
|
14
|
+
var data_exports = {};
|
|
15
|
+
__export(data_exports, {
|
|
16
|
+
detectFileExtension: () => detectFileExtension,
|
|
17
|
+
detectFormatFromFilename: () => detectFormatFromFilename,
|
|
18
|
+
detectUniversalFormat: () => detectUniversalFormat,
|
|
19
|
+
exportData: () => exportData,
|
|
20
|
+
exportTree: () => exportTree,
|
|
21
|
+
exportTxt: () => exportTxt,
|
|
22
|
+
importData: () => importData,
|
|
23
|
+
importTree: () => importTree,
|
|
24
|
+
importTxt: () => importTxt,
|
|
25
|
+
readFileAsText: () => readFileAsText,
|
|
26
|
+
validateCSVData: () => validateCSVData,
|
|
27
|
+
validateExportData: () => validateExportData
|
|
28
|
+
});
|
|
29
|
+
var readFileAsText = async (fileOrPath, encoding = "utf8") => {
|
|
30
|
+
if (isNode() && typeof fileOrPath === "string") {
|
|
31
|
+
const fs = await import("fs/promises");
|
|
32
|
+
return fs.readFile(fileOrPath, encoding);
|
|
33
|
+
} else if (isBrowser() && fileOrPath instanceof File) {
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const reader = new FileReader();
|
|
36
|
+
reader.onload = (e) => resolve(e.target?.result);
|
|
37
|
+
reader.onerror = () => reject(new Error("Error reading file"));
|
|
38
|
+
reader.readAsText(fileOrPath);
|
|
39
|
+
});
|
|
40
|
+
} else {
|
|
41
|
+
throw new DataError(
|
|
42
|
+
"Invalid parameter for current environment",
|
|
43
|
+
"ENVIRONMENT_NOT_SUPPORTED" /* ENVIRONMENT_NOT_SUPPORTED */,
|
|
44
|
+
{
|
|
45
|
+
data: {
|
|
46
|
+
isNodeEnv: isNode(),
|
|
47
|
+
isBrowserEnv: isBrowser(),
|
|
48
|
+
parameterType: typeof fileOrPath
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
function validateExportData(data) {
|
|
55
|
+
if (!Array.isArray(data)) {
|
|
56
|
+
throw createValidationError("Data must be an array", "data", typeof data);
|
|
57
|
+
}
|
|
58
|
+
if (data.length === 0) {
|
|
59
|
+
throw createValidationError("Data array cannot be empty", "data.length", data.length);
|
|
60
|
+
}
|
|
61
|
+
const firstItem = data[0];
|
|
62
|
+
const isObjectArray = typeof firstItem === "object" && firstItem !== null && !Array.isArray(firstItem);
|
|
63
|
+
const isArrayOfArrays = Array.isArray(firstItem);
|
|
64
|
+
if (!isObjectArray && !isArrayOfArrays) {
|
|
65
|
+
throw createValidationError(
|
|
66
|
+
"Data must be an array of objects or an array of arrays",
|
|
67
|
+
"data[0]",
|
|
68
|
+
typeof firstItem
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
if (isObjectArray) {
|
|
72
|
+
for (let i = 1; i < data.length; i++) {
|
|
73
|
+
const item = data[i];
|
|
74
|
+
if (typeof item !== "object" || item === null || Array.isArray(item)) {
|
|
75
|
+
throw createValidationError(
|
|
76
|
+
"All elements must be objects if first one is an object",
|
|
77
|
+
`data[${i}]`,
|
|
78
|
+
typeof item
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} else if (isArrayOfArrays) {
|
|
83
|
+
const firstRowLength = firstItem.length;
|
|
84
|
+
for (let i = 1; i < data.length; i++) {
|
|
85
|
+
if (!Array.isArray(data[i])) {
|
|
86
|
+
throw createValidationError(`Row ${i} is not an array`, `data[${i}]`, typeof data[i]);
|
|
87
|
+
}
|
|
88
|
+
if (data[i].length !== firstRowLength) {
|
|
89
|
+
throw createValidationError(
|
|
90
|
+
`Row ${i} has ${data[i].length} columns, but first has ${firstRowLength}`,
|
|
91
|
+
`data[${i}].length`,
|
|
92
|
+
{ expected: firstRowLength, actual: data[i].length }
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
var validateCSVData = validateExportData;
|
|
100
|
+
function detectFormatFromFilename(filename) {
|
|
101
|
+
const parts = filename.toLowerCase().split(".");
|
|
102
|
+
const extension = parts.length > 1 ? parts.pop() : void 0;
|
|
103
|
+
switch (extension) {
|
|
104
|
+
case "csv":
|
|
105
|
+
return "csv";
|
|
106
|
+
case "json":
|
|
107
|
+
return "json";
|
|
108
|
+
case "tree":
|
|
109
|
+
return "tree";
|
|
110
|
+
case "txt":
|
|
111
|
+
return "txt";
|
|
112
|
+
default:
|
|
113
|
+
if (!extension) {
|
|
114
|
+
throw new DataError(
|
|
115
|
+
"Could not detect file format. Must have a valid extension (.csv, .json, .tree, .txt)",
|
|
116
|
+
"UNSUPPORTED_FORMAT" /* UNSUPPORTED_FORMAT */,
|
|
117
|
+
{ data: { filename } }
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
throw new DataError(
|
|
121
|
+
`Unsupported file format: .${extension}. Use .csv, .json, .tree or .txt`,
|
|
122
|
+
"UNSUPPORTED_FORMAT" /* UNSUPPORTED_FORMAT */,
|
|
123
|
+
{ data: { filename, extension } }
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function detectFileExtension(filename) {
|
|
128
|
+
if (!filename || typeof filename !== "string") {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
const parts = filename.trim().split(".");
|
|
132
|
+
if (parts.length < 2) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
const extension = parts.pop()?.toLowerCase();
|
|
136
|
+
return extension || null;
|
|
137
|
+
}
|
|
138
|
+
function detectUniversalFormat(filename) {
|
|
139
|
+
const extension = detectFileExtension(filename);
|
|
140
|
+
if (!extension) {
|
|
141
|
+
return {
|
|
142
|
+
extension: null,
|
|
143
|
+
category: "unknown",
|
|
144
|
+
isText: false,
|
|
145
|
+
isBinary: false,
|
|
146
|
+
mimeType: null
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
const formatInfo = {
|
|
150
|
+
// Data formats
|
|
151
|
+
json: { category: "data", isText: true, isBinary: false, mimeType: "application/json" },
|
|
152
|
+
csv: { category: "data", isText: true, isBinary: false, mimeType: "text/csv" },
|
|
153
|
+
xml: { category: "data", isText: true, isBinary: false, mimeType: "application/xml" },
|
|
154
|
+
yaml: { category: "data", isText: true, isBinary: false, mimeType: "application/yaml" },
|
|
155
|
+
yml: { category: "data", isText: true, isBinary: false, mimeType: "application/yaml" },
|
|
156
|
+
tree: { category: "data", isText: true, isBinary: false, mimeType: "text/plain" },
|
|
157
|
+
tsv: { category: "data", isText: true, isBinary: false, mimeType: "text/tab-separated-values" },
|
|
158
|
+
sql: { category: "data", isText: true, isBinary: false, mimeType: "application/sql" },
|
|
159
|
+
db: { category: "data", isText: false, isBinary: true, mimeType: "application/x-sqlite3" },
|
|
160
|
+
sqlite: { category: "data", isText: false, isBinary: true, mimeType: "application/x-sqlite3" },
|
|
161
|
+
// Text and code formats
|
|
162
|
+
txt: { category: "text", isText: true, isBinary: false, mimeType: "text/plain" },
|
|
163
|
+
md: { category: "text", isText: true, isBinary: false, mimeType: "text/markdown" },
|
|
164
|
+
rst: { category: "text", isText: true, isBinary: false, mimeType: "text/x-rst" },
|
|
165
|
+
rtf: { category: "text", isText: true, isBinary: false, mimeType: "application/rtf" },
|
|
166
|
+
log: { category: "text", isText: true, isBinary: false, mimeType: "text/plain" },
|
|
167
|
+
// Web formats
|
|
168
|
+
html: { category: "web", isText: true, isBinary: false, mimeType: "text/html" },
|
|
169
|
+
htm: { category: "web", isText: true, isBinary: false, mimeType: "text/html" },
|
|
170
|
+
css: { category: "web", isText: true, isBinary: false, mimeType: "text/css" },
|
|
171
|
+
scss: { category: "web", isText: true, isBinary: false, mimeType: "text/x-scss" },
|
|
172
|
+
sass: { category: "web", isText: true, isBinary: false, mimeType: "text/x-sass" },
|
|
173
|
+
less: { category: "web", isText: true, isBinary: false, mimeType: "text/x-less" },
|
|
174
|
+
// Programming languages
|
|
175
|
+
js: { category: "code", isText: true, isBinary: false, mimeType: "application/javascript" },
|
|
176
|
+
mjs: { category: "code", isText: true, isBinary: false, mimeType: "application/javascript" },
|
|
177
|
+
jsx: { category: "code", isText: true, isBinary: false, mimeType: "text/jsx" },
|
|
178
|
+
ts: { category: "code", isText: true, isBinary: false, mimeType: "application/typescript" },
|
|
179
|
+
tsx: { category: "code", isText: true, isBinary: false, mimeType: "text/tsx" },
|
|
180
|
+
py: { category: "code", isText: true, isBinary: false, mimeType: "text/x-python" },
|
|
181
|
+
java: { category: "code", isText: true, isBinary: false, mimeType: "text/x-java-source" },
|
|
182
|
+
php: { category: "code", isText: true, isBinary: false, mimeType: "application/x-httpd-php" },
|
|
183
|
+
rb: { category: "code", isText: true, isBinary: false, mimeType: "text/x-ruby" },
|
|
184
|
+
go: { category: "code", isText: true, isBinary: false, mimeType: "text/x-go" },
|
|
185
|
+
rs: { category: "code", isText: true, isBinary: false, mimeType: "text/x-rust" },
|
|
186
|
+
c: { category: "code", isText: true, isBinary: false, mimeType: "text/x-c" },
|
|
187
|
+
cpp: { category: "code", isText: true, isBinary: false, mimeType: "text/x-c++" },
|
|
188
|
+
cc: { category: "code", isText: true, isBinary: false, mimeType: "text/x-c++" },
|
|
189
|
+
h: { category: "code", isText: true, isBinary: false, mimeType: "text/x-c" },
|
|
190
|
+
cs: { category: "code", isText: true, isBinary: false, mimeType: "text/x-csharp" },
|
|
191
|
+
sh: { category: "code", isText: true, isBinary: false, mimeType: "application/x-sh" },
|
|
192
|
+
bash: { category: "code", isText: true, isBinary: false, mimeType: "application/x-sh" },
|
|
193
|
+
ps1: { category: "code", isText: true, isBinary: false, mimeType: "application/x-powershell" },
|
|
194
|
+
// Microsoft Office documents
|
|
195
|
+
doc: { category: "document", isText: false, isBinary: true, mimeType: "application/msword" },
|
|
196
|
+
docx: {
|
|
197
|
+
category: "document",
|
|
198
|
+
isText: false,
|
|
199
|
+
isBinary: true,
|
|
200
|
+
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
201
|
+
},
|
|
202
|
+
xls: {
|
|
203
|
+
category: "spreadsheet",
|
|
204
|
+
isText: false,
|
|
205
|
+
isBinary: true,
|
|
206
|
+
mimeType: "application/vnd.ms-excel"
|
|
207
|
+
},
|
|
208
|
+
xlsx: {
|
|
209
|
+
category: "spreadsheet",
|
|
210
|
+
isText: false,
|
|
211
|
+
isBinary: true,
|
|
212
|
+
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
213
|
+
},
|
|
214
|
+
ppt: {
|
|
215
|
+
category: "presentation",
|
|
216
|
+
isText: false,
|
|
217
|
+
isBinary: true,
|
|
218
|
+
mimeType: "application/vnd.ms-powerpoint"
|
|
219
|
+
},
|
|
220
|
+
pptx: {
|
|
221
|
+
category: "presentation",
|
|
222
|
+
isText: false,
|
|
223
|
+
isBinary: true,
|
|
224
|
+
mimeType: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
|
225
|
+
},
|
|
226
|
+
// LibreOffice/OpenOffice
|
|
227
|
+
odt: {
|
|
228
|
+
category: "document",
|
|
229
|
+
isText: false,
|
|
230
|
+
isBinary: true,
|
|
231
|
+
mimeType: "application/vnd.oasis.opendocument.text"
|
|
232
|
+
},
|
|
233
|
+
ods: {
|
|
234
|
+
category: "spreadsheet",
|
|
235
|
+
isText: false,
|
|
236
|
+
isBinary: true,
|
|
237
|
+
mimeType: "application/vnd.oasis.opendocument.spreadsheet"
|
|
238
|
+
},
|
|
239
|
+
odp: {
|
|
240
|
+
category: "presentation",
|
|
241
|
+
isText: false,
|
|
242
|
+
isBinary: true,
|
|
243
|
+
mimeType: "application/vnd.oasis.opendocument.presentation"
|
|
244
|
+
},
|
|
245
|
+
// Other document formats
|
|
246
|
+
pdf: { category: "document", isText: false, isBinary: true, mimeType: "application/pdf" },
|
|
247
|
+
epub: { category: "document", isText: false, isBinary: true, mimeType: "application/epub+zip" },
|
|
248
|
+
mobi: {
|
|
249
|
+
category: "document",
|
|
250
|
+
isText: false,
|
|
251
|
+
isBinary: true,
|
|
252
|
+
mimeType: "application/x-mobipocket-ebook"
|
|
253
|
+
},
|
|
254
|
+
// Image formats
|
|
255
|
+
jpg: { category: "image", isText: false, isBinary: true, mimeType: "image/jpeg" },
|
|
256
|
+
jpeg: { category: "image", isText: false, isBinary: true, mimeType: "image/jpeg" },
|
|
257
|
+
png: { category: "image", isText: false, isBinary: true, mimeType: "image/png" },
|
|
258
|
+
gif: { category: "image", isText: false, isBinary: true, mimeType: "image/gif" },
|
|
259
|
+
svg: { category: "image", isText: true, isBinary: false, mimeType: "image/svg+xml" },
|
|
260
|
+
webp: { category: "image", isText: false, isBinary: true, mimeType: "image/webp" },
|
|
261
|
+
avif: { category: "image", isText: false, isBinary: true, mimeType: "image/avif" },
|
|
262
|
+
bmp: { category: "image", isText: false, isBinary: true, mimeType: "image/bmp" },
|
|
263
|
+
tiff: { category: "image", isText: false, isBinary: true, mimeType: "image/tiff" },
|
|
264
|
+
tif: { category: "image", isText: false, isBinary: true, mimeType: "image/tiff" },
|
|
265
|
+
ico: { category: "image", isText: false, isBinary: true, mimeType: "image/x-icon" },
|
|
266
|
+
psd: {
|
|
267
|
+
category: "image",
|
|
268
|
+
isText: false,
|
|
269
|
+
isBinary: true,
|
|
270
|
+
mimeType: "image/vnd.adobe.photoshop"
|
|
271
|
+
},
|
|
272
|
+
// Audio formats
|
|
273
|
+
mp3: { category: "audio", isText: false, isBinary: true, mimeType: "audio/mpeg" },
|
|
274
|
+
wav: { category: "audio", isText: false, isBinary: true, mimeType: "audio/wav" },
|
|
275
|
+
flac: { category: "audio", isText: false, isBinary: true, mimeType: "audio/flac" },
|
|
276
|
+
ogg: { category: "audio", isText: false, isBinary: true, mimeType: "audio/ogg" },
|
|
277
|
+
aac: { category: "audio", isText: false, isBinary: true, mimeType: "audio/aac" },
|
|
278
|
+
m4a: { category: "audio", isText: false, isBinary: true, mimeType: "audio/m4a" },
|
|
279
|
+
wma: { category: "audio", isText: false, isBinary: true, mimeType: "audio/x-ms-wma" },
|
|
280
|
+
// Video formats
|
|
281
|
+
mp4: { category: "video", isText: false, isBinary: true, mimeType: "video/mp4" },
|
|
282
|
+
avi: { category: "video", isText: false, isBinary: true, mimeType: "video/x-msvideo" },
|
|
283
|
+
mov: { category: "video", isText: false, isBinary: true, mimeType: "video/quicktime" },
|
|
284
|
+
wmv: { category: "video", isText: false, isBinary: true, mimeType: "video/x-ms-wmv" },
|
|
285
|
+
flv: { category: "video", isText: false, isBinary: true, mimeType: "video/x-flv" },
|
|
286
|
+
webm: { category: "video", isText: false, isBinary: true, mimeType: "video/webm" },
|
|
287
|
+
mkv: { category: "video", isText: false, isBinary: true, mimeType: "video/x-matroska" },
|
|
288
|
+
"3gp": { category: "video", isText: false, isBinary: true, mimeType: "video/3gpp" },
|
|
289
|
+
// Archive formats
|
|
290
|
+
zip: { category: "archive", isText: false, isBinary: true, mimeType: "application/zip" },
|
|
291
|
+
rar: {
|
|
292
|
+
category: "archive",
|
|
293
|
+
isText: false,
|
|
294
|
+
isBinary: true,
|
|
295
|
+
mimeType: "application/x-rar-compressed"
|
|
296
|
+
},
|
|
297
|
+
"7z": {
|
|
298
|
+
category: "archive",
|
|
299
|
+
isText: false,
|
|
300
|
+
isBinary: true,
|
|
301
|
+
mimeType: "application/x-7z-compressed"
|
|
302
|
+
},
|
|
303
|
+
tar: { category: "archive", isText: false, isBinary: true, mimeType: "application/x-tar" },
|
|
304
|
+
gz: { category: "archive", isText: false, isBinary: true, mimeType: "application/gzip" },
|
|
305
|
+
bz2: { category: "archive", isText: false, isBinary: true, mimeType: "application/x-bzip2" },
|
|
306
|
+
xz: { category: "archive", isText: false, isBinary: true, mimeType: "application/x-xz" },
|
|
307
|
+
// Configuration files
|
|
308
|
+
ini: { category: "config", isText: true, isBinary: false, mimeType: "text/plain" },
|
|
309
|
+
cfg: { category: "config", isText: true, isBinary: false, mimeType: "text/plain" },
|
|
310
|
+
conf: { category: "config", isText: true, isBinary: false, mimeType: "text/plain" },
|
|
311
|
+
toml: { category: "config", isText: true, isBinary: false, mimeType: "application/toml" },
|
|
312
|
+
env: { category: "config", isText: true, isBinary: false, mimeType: "text/plain" },
|
|
313
|
+
// Font formats
|
|
314
|
+
ttf: { category: "font", isText: false, isBinary: true, mimeType: "font/ttf" },
|
|
315
|
+
otf: { category: "font", isText: false, isBinary: true, mimeType: "font/otf" },
|
|
316
|
+
woff: { category: "font", isText: false, isBinary: true, mimeType: "font/woff" },
|
|
317
|
+
woff2: { category: "font", isText: false, isBinary: true, mimeType: "font/woff2" },
|
|
318
|
+
eot: {
|
|
319
|
+
category: "font",
|
|
320
|
+
isText: false,
|
|
321
|
+
isBinary: true,
|
|
322
|
+
mimeType: "application/vnd.ms-fontobject"
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
const info = formatInfo[extension] || {
|
|
326
|
+
category: "unknown",
|
|
327
|
+
isText: false,
|
|
328
|
+
isBinary: false,
|
|
329
|
+
mimeType: null
|
|
330
|
+
};
|
|
331
|
+
return {
|
|
332
|
+
extension,
|
|
333
|
+
...info
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
async function exportTree(data, filePath, options) {
|
|
337
|
+
if (!Array.isArray(data)) {
|
|
338
|
+
throw createValidationError(
|
|
339
|
+
"Data for tree format must be an array of nodes",
|
|
340
|
+
"data",
|
|
341
|
+
typeof data
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
const { renderTreeAsText } = await import("./tree.js");
|
|
345
|
+
const treeText = renderTreeAsText(data, options);
|
|
346
|
+
if (isNode()) {
|
|
347
|
+
const fs = await import("fs/promises");
|
|
348
|
+
await fs.writeFile(filePath, treeText, { encoding: "utf-8" });
|
|
349
|
+
} else {
|
|
350
|
+
const blob = new Blob([treeText], { type: "text/plain;charset=utf-8" });
|
|
351
|
+
const url = URL.createObjectURL(blob);
|
|
352
|
+
const link = document.createElement("a");
|
|
353
|
+
link.href = url;
|
|
354
|
+
link.download = filePath.split("/").pop() || "tree.tree";
|
|
355
|
+
document.body.appendChild(link);
|
|
356
|
+
link.click();
|
|
357
|
+
document.body.removeChild(link);
|
|
358
|
+
URL.revokeObjectURL(url);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
async function importTree(filePath, _options) {
|
|
362
|
+
if (isNode()) {
|
|
363
|
+
const fs = await import("fs/promises");
|
|
364
|
+
return fs.readFile(filePath, { encoding: "utf-8" });
|
|
365
|
+
} else {
|
|
366
|
+
throw new DataError(
|
|
367
|
+
".tree file import is not supported in browser. Use readFileAsText() with a File object.",
|
|
368
|
+
"ENVIRONMENT_NOT_SUPPORTED" /* ENVIRONMENT_NOT_SUPPORTED */,
|
|
369
|
+
{ data: { environment: "browser", operation: "importTree" } }
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
async function exportTxt(data, filePath, options) {
|
|
374
|
+
const { separator = "\n", stringify, indent = 2 } = options || {};
|
|
375
|
+
let textContent;
|
|
376
|
+
if (stringify) {
|
|
377
|
+
textContent = stringify(data);
|
|
378
|
+
} else if (typeof data === "string") {
|
|
379
|
+
textContent = data;
|
|
380
|
+
} else if (Array.isArray(data)) {
|
|
381
|
+
textContent = data.map((item) => typeof item === "string" ? item : JSON.stringify(item, null, indent)).join(separator);
|
|
382
|
+
} else if (typeof data === "object" && data !== null) {
|
|
383
|
+
textContent = JSON.stringify(data, null, indent);
|
|
384
|
+
} else {
|
|
385
|
+
textContent = String(data);
|
|
386
|
+
}
|
|
387
|
+
if (isNode()) {
|
|
388
|
+
const fs = await import("fs/promises");
|
|
389
|
+
await fs.writeFile(filePath, textContent, { encoding: "utf-8" });
|
|
390
|
+
} else {
|
|
391
|
+
const blob = new Blob([textContent], { type: "text/plain;charset=utf-8" });
|
|
392
|
+
const url = URL.createObjectURL(blob);
|
|
393
|
+
const link = document.createElement("a");
|
|
394
|
+
link.href = url;
|
|
395
|
+
link.download = filePath.split("/").pop() || "file.txt";
|
|
396
|
+
document.body.appendChild(link);
|
|
397
|
+
link.click();
|
|
398
|
+
document.body.removeChild(link);
|
|
399
|
+
URL.revokeObjectURL(url);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
function sanitizeTextContent(content) {
|
|
403
|
+
return content.replace(/\r\n|\r/g, "\n").replace(/[\x00-\x08\x0E-\x1F\x7F]/g, "").replace(/\n{4,}/g, "\n\n\n").replace(/[ \t]+$/gm, "");
|
|
404
|
+
}
|
|
405
|
+
async function importTxt(filePath, options = {}) {
|
|
406
|
+
const {
|
|
407
|
+
maxFileSize = 10 * 1024 * 1024,
|
|
408
|
+
// 10MB default
|
|
409
|
+
maxLength = 1e6,
|
|
410
|
+
// 1M characters default
|
|
411
|
+
validateSecurity = true,
|
|
412
|
+
sanitize = true
|
|
413
|
+
} = options;
|
|
414
|
+
if (isNode()) {
|
|
415
|
+
const fs = await import("fs/promises");
|
|
416
|
+
const path = await import("path");
|
|
417
|
+
const { isValidFilePath, isValidFileSize, isValidTextContent } = await import("./validators.js");
|
|
418
|
+
if (!isValidFilePath(filePath)) {
|
|
419
|
+
throw new Error("Invalid or unsafe file path");
|
|
420
|
+
}
|
|
421
|
+
const resolvedPath = path.resolve(filePath);
|
|
422
|
+
try {
|
|
423
|
+
const stats = await fs.stat(resolvedPath);
|
|
424
|
+
if (!isValidFileSize(stats.size, maxFileSize)) {
|
|
425
|
+
throw new Error(`File too large: ${stats.size} bytes (max: ${maxFileSize})`);
|
|
426
|
+
}
|
|
427
|
+
} catch (error) {
|
|
428
|
+
if (error.code === "ENOENT") {
|
|
429
|
+
throw new Error(`File not found: ${filePath}`);
|
|
430
|
+
}
|
|
431
|
+
throw error;
|
|
432
|
+
}
|
|
433
|
+
const content = await fs.readFile(resolvedPath, { encoding: "utf-8" });
|
|
434
|
+
if (validateSecurity && !isValidTextContent(content, { maxLength })) {
|
|
435
|
+
throw new Error("File contains potentially dangerous content or exceeds security limits");
|
|
436
|
+
}
|
|
437
|
+
return sanitize ? sanitizeTextContent(content) : content;
|
|
438
|
+
} else {
|
|
439
|
+
throw new Error(
|
|
440
|
+
".txt file import is not supported in browser. Use readFileAsText() with a File object."
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
async function exportData(data, filePath, options) {
|
|
445
|
+
const format = detectFormatFromFilename(filePath);
|
|
446
|
+
if (format === "csv") {
|
|
447
|
+
validateExportData(data);
|
|
448
|
+
}
|
|
449
|
+
switch (format) {
|
|
450
|
+
case "csv": {
|
|
451
|
+
const { exportCSV } = await import("./csv.js");
|
|
452
|
+
return exportCSV(data, filePath, options);
|
|
453
|
+
}
|
|
454
|
+
case "json": {
|
|
455
|
+
const { exportJSON } = await import("./json.js");
|
|
456
|
+
return exportJSON(data, filePath, options);
|
|
457
|
+
}
|
|
458
|
+
case "tree": {
|
|
459
|
+
return exportTree(data, filePath, options);
|
|
460
|
+
}
|
|
461
|
+
case "txt": {
|
|
462
|
+
return exportTxt(data, filePath, options);
|
|
463
|
+
}
|
|
464
|
+
default:
|
|
465
|
+
throw new Error(`Unsupported format: ${format}`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
async function importData(filePath, options) {
|
|
469
|
+
const format = detectFormatFromFilename(filePath);
|
|
470
|
+
switch (format) {
|
|
471
|
+
case "csv": {
|
|
472
|
+
const { importCSV } = await import("./csv.js");
|
|
473
|
+
return importCSV(filePath, options);
|
|
474
|
+
}
|
|
475
|
+
case "json": {
|
|
476
|
+
const { importJSON } = await import("./json.js");
|
|
477
|
+
return importJSON(filePath, options);
|
|
478
|
+
}
|
|
479
|
+
case "tree": {
|
|
480
|
+
return importTree(filePath, options);
|
|
481
|
+
}
|
|
482
|
+
case "txt": {
|
|
483
|
+
return importTxt(filePath, options);
|
|
484
|
+
}
|
|
485
|
+
default:
|
|
486
|
+
throw new Error(`Unsupported format: ${format}`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
export {
|
|
491
|
+
readFileAsText,
|
|
492
|
+
validateExportData,
|
|
493
|
+
validateCSVData,
|
|
494
|
+
detectFormatFromFilename,
|
|
495
|
+
detectFileExtension,
|
|
496
|
+
detectUniversalFormat,
|
|
497
|
+
exportTree,
|
|
498
|
+
importTree,
|
|
499
|
+
exportTxt,
|
|
500
|
+
importTxt,
|
|
501
|
+
exportData,
|
|
502
|
+
importData,
|
|
503
|
+
data_exports
|
|
504
|
+
};
|
|
505
|
+
//# sourceMappingURL=chunk-KBHE3K2F.js.map
|