@tmlmobilidade/files 20251229.932.45 → 20260103.2047.14
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/dist/files.d.ts +28 -14
- package/dist/files.js +39 -23
- package/package.json +1 -1
package/dist/files.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import JSZip from 'jszip';
|
|
2
|
-
import { ParseConfig } from 'papaparse';
|
|
2
|
+
import { type ParseConfig } from 'papaparse';
|
|
3
3
|
interface UpdateCsvFieldParams {
|
|
4
4
|
column: string;
|
|
5
5
|
csvString: string;
|
|
@@ -8,9 +8,18 @@ interface UpdateCsvFieldParams {
|
|
|
8
8
|
}
|
|
9
9
|
export declare class Files {
|
|
10
10
|
/**
|
|
11
|
-
* Blob to
|
|
11
|
+
* Converts a Blob to a File.
|
|
12
|
+
* @param blob The Blob object to convert.
|
|
13
|
+
* @param fileName The name of the resulting File.
|
|
14
|
+
* @returns The resulting File.
|
|
12
15
|
*/
|
|
13
16
|
static blobToFile(blob: Blob, fileName: string): File;
|
|
17
|
+
/**
|
|
18
|
+
* Returns the file extension from a file name.
|
|
19
|
+
* @param fileName The name of the file.
|
|
20
|
+
* @returns The file extension.
|
|
21
|
+
* @throws Error if the file has no extension or if the extension is not supported.
|
|
22
|
+
*/
|
|
14
23
|
static getFileExtension(fileName: string): string;
|
|
15
24
|
/**
|
|
16
25
|
* Gets the file extension from a MIME type.
|
|
@@ -18,23 +27,28 @@ export declare class Files {
|
|
|
18
27
|
* @returns The file extension, or an empty string if not found.
|
|
19
28
|
*/
|
|
20
29
|
static getFileExtensionFromMimeType(mimeType: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the MIME type from a file extension.
|
|
32
|
+
* @param fileName The name of the file to get the MIME type for.
|
|
33
|
+
* @returns The MIME type.
|
|
34
|
+
*/
|
|
21
35
|
static getMimeTypeFromFileExtension(fileName: string): string;
|
|
22
36
|
/**
|
|
23
37
|
* Parses a CSV string into an array of objects using PapaParse.
|
|
24
|
-
* @param csvString
|
|
25
|
-
* @param options
|
|
26
|
-
* @param options.header
|
|
27
|
-
* @param options.skipEmptyLines
|
|
28
|
-
* @param options.rest
|
|
38
|
+
* @param csvString The CSV string to parse
|
|
39
|
+
* @param options Parse configuration options
|
|
40
|
+
* @param options.header Whether to interpret first row as field names. Defaults to true.
|
|
41
|
+
* @param options.skipEmptyLines Whether to skip empty lines in the CSV. Defaults to true.
|
|
42
|
+
* @param options.rest Additional PapaParse configuration options
|
|
29
43
|
* @returns Promise resolving to array of parsed objects
|
|
30
44
|
* @throws Error if parsing fails with details of parsing errors
|
|
31
45
|
*/
|
|
32
46
|
static parseCsv<T>(csvString: string, { header, skipEmptyLines, ...options }: ParseConfig<T>): Promise<T[]>;
|
|
33
47
|
/**
|
|
34
48
|
* Reads and extracts a single file from a ZIP archive.
|
|
35
|
-
* @param zipFilePath
|
|
36
|
-
* @param fileName
|
|
37
|
-
* @param encoding
|
|
49
|
+
* @param zipFilePath The zip file to read from, can be a File object (browser), string path (Node.js), or URL
|
|
50
|
+
* @param fileName The name of the file to extract from the ZIP
|
|
51
|
+
* @param encoding The encoding to use when reading the file. See JSZip documentation for supported formats.
|
|
38
52
|
* @returns A Promise resolving to the file contents in the specified encoding
|
|
39
53
|
* @throws Error if the file is not found in the ZIP
|
|
40
54
|
*/
|
|
@@ -47,10 +61,10 @@ export declare class Files {
|
|
|
47
61
|
static unzip(zipFilePath: File | string | URL): Promise<JSZip>;
|
|
48
62
|
/**
|
|
49
63
|
* Updates a CSV string with an object.
|
|
50
|
-
* @param csvString
|
|
51
|
-
* @param column
|
|
52
|
-
* @param row
|
|
53
|
-
* @param value
|
|
64
|
+
* @param csvString The CSV string to update
|
|
65
|
+
* @param column The column name to update
|
|
66
|
+
* @param row The row index to update
|
|
67
|
+
* @param value The value to update the column with
|
|
54
68
|
* @returns A Promise resolving to the updated CSV string
|
|
55
69
|
*/
|
|
56
70
|
static updateCsvField<T>(params: UpdateCsvFieldParams[]): Promise<string>;
|
package/dist/files.js
CHANGED
|
@@ -8,20 +8,33 @@ import papaparse from 'papaparse';
|
|
|
8
8
|
export class Files {
|
|
9
9
|
//
|
|
10
10
|
/**
|
|
11
|
-
* Blob to
|
|
11
|
+
* Converts a Blob to a File.
|
|
12
|
+
* @param blob The Blob object to convert.
|
|
13
|
+
* @param fileName The name of the resulting File.
|
|
14
|
+
* @returns The resulting File.
|
|
12
15
|
*/
|
|
13
16
|
static blobToFile(blob, fileName) {
|
|
14
17
|
return new File([blob], fileName);
|
|
15
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns the file extension from a file name.
|
|
21
|
+
* @param fileName The name of the file.
|
|
22
|
+
* @returns The file extension.
|
|
23
|
+
* @throws Error if the file has no extension or if the extension is not supported.
|
|
24
|
+
*/
|
|
16
25
|
static getFileExtension(fileName) {
|
|
17
|
-
|
|
18
|
-
|
|
26
|
+
// Extract the file extension from the file name.
|
|
27
|
+
const extension = fileName.split('.').pop().toLowerCase();
|
|
28
|
+
// Throw an error if the file has no extension
|
|
29
|
+
// or if the extension is not supported.
|
|
30
|
+
if (!extension)
|
|
19
31
|
throw new Error('File has no extension');
|
|
20
|
-
|
|
32
|
+
// Get the MIME type for the extension.
|
|
21
33
|
const mimeType = mimeTypes[extension];
|
|
22
|
-
if
|
|
34
|
+
// Throw an error if the extension is not supported.
|
|
35
|
+
if (!mimeType)
|
|
23
36
|
throw new Error(`Unsupported file extension: ${extension}`);
|
|
24
|
-
|
|
37
|
+
// Return the file extension.
|
|
25
38
|
return extension;
|
|
26
39
|
}
|
|
27
40
|
/**
|
|
@@ -37,17 +50,22 @@ export class Files {
|
|
|
37
50
|
return '';
|
|
38
51
|
return extension;
|
|
39
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Gets the MIME type from a file extension.
|
|
55
|
+
* @param fileName The name of the file to get the MIME type for.
|
|
56
|
+
* @returns The MIME type.
|
|
57
|
+
*/
|
|
40
58
|
static getMimeTypeFromFileExtension(fileName) {
|
|
41
59
|
const extension = Files.getFileExtension(fileName);
|
|
42
60
|
return mimeTypes[extension];
|
|
43
61
|
}
|
|
44
62
|
/**
|
|
45
63
|
* Parses a CSV string into an array of objects using PapaParse.
|
|
46
|
-
* @param csvString
|
|
47
|
-
* @param options
|
|
48
|
-
* @param options.header
|
|
49
|
-
* @param options.skipEmptyLines
|
|
50
|
-
* @param options.rest
|
|
64
|
+
* @param csvString The CSV string to parse
|
|
65
|
+
* @param options Parse configuration options
|
|
66
|
+
* @param options.header Whether to interpret first row as field names. Defaults to true.
|
|
67
|
+
* @param options.skipEmptyLines Whether to skip empty lines in the CSV. Defaults to true.
|
|
68
|
+
* @param options.rest Additional PapaParse configuration options
|
|
51
69
|
* @returns Promise resolving to array of parsed objects
|
|
52
70
|
* @throws Error if parsing fails with details of parsing errors
|
|
53
71
|
*/
|
|
@@ -60,18 +78,17 @@ export class Files {
|
|
|
60
78
|
}
|
|
61
79
|
/**
|
|
62
80
|
* Reads and extracts a single file from a ZIP archive.
|
|
63
|
-
* @param zipFilePath
|
|
64
|
-
* @param fileName
|
|
65
|
-
* @param encoding
|
|
81
|
+
* @param zipFilePath The zip file to read from, can be a File object (browser), string path (Node.js), or URL
|
|
82
|
+
* @param fileName The name of the file to extract from the ZIP
|
|
83
|
+
* @param encoding The encoding to use when reading the file. See JSZip documentation for supported formats.
|
|
66
84
|
* @returns A Promise resolving to the file contents in the specified encoding
|
|
67
85
|
* @throws Error if the file is not found in the ZIP
|
|
68
86
|
*/
|
|
69
87
|
static async readFileFromZip(zipFilePath, fileName, encoding) {
|
|
70
88
|
const zip = await Files.unzip(zipFilePath);
|
|
71
89
|
const file = zip.file(fileName);
|
|
72
|
-
if (!file)
|
|
73
|
-
throw new Error(`File ${fileName} not found in
|
|
74
|
-
}
|
|
90
|
+
if (!file)
|
|
91
|
+
throw new Error(`File ${fileName} not found in the zip archive.`);
|
|
75
92
|
return await file.async(encoding);
|
|
76
93
|
}
|
|
77
94
|
/**
|
|
@@ -112,18 +129,17 @@ export class Files {
|
|
|
112
129
|
}
|
|
113
130
|
/**
|
|
114
131
|
* Updates a CSV string with an object.
|
|
115
|
-
* @param csvString
|
|
116
|
-
* @param column
|
|
117
|
-
* @param row
|
|
118
|
-
* @param value
|
|
132
|
+
* @param csvString The CSV string to update
|
|
133
|
+
* @param column The column name to update
|
|
134
|
+
* @param row The row index to update
|
|
135
|
+
* @param value The value to update the column with
|
|
119
136
|
* @returns A Promise resolving to the updated CSV string
|
|
120
137
|
*/
|
|
121
138
|
static async updateCsvField(params) {
|
|
122
139
|
let csv = params[0].csvString;
|
|
123
140
|
for (const param of params) {
|
|
124
|
-
const { column, rowIndex, value } = param;
|
|
125
141
|
const data = await this.parseCsv(csv, { header: true });
|
|
126
|
-
const updatedData = data.map((row, index) => (index === rowIndex ? { ...row, [column]: value } : row));
|
|
142
|
+
const updatedData = data.map((row, index) => (index === param.rowIndex ? { ...row, [param.column]: param.value } : row));
|
|
127
143
|
csv = papaparse.unparse(updatedData, { header: true });
|
|
128
144
|
}
|
|
129
145
|
return csv;
|