@teselagen/file-utils 0.3.9 → 0.3.11

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 ADDED
@@ -0,0 +1,11 @@
1
+ # file-utils
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build file-utils` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test file-utils` to execute the unit tests via [Jest](https://jestjs.io).
package/index.js CHANGED
@@ -14690,6 +14690,8 @@ const cleanCsvExport = /* @__PURE__ */ __name((rows) => {
14690
14690
  return rows;
14691
14691
  }, "cleanCsvExport");
14692
14692
  const filterFilesInZip = /* @__PURE__ */ __name((file, accepted) => __async(exports, null, function* () {
14693
+ if (!file || Array.isArray(file) && !file.length)
14694
+ return [];
14693
14695
  const zipExtracted = yield extractZipFiles(file);
14694
14696
  const acceptedFiles = [];
14695
14697
  for (const extFile of zipExtracted) {
package/index.mjs CHANGED
@@ -14688,6 +14688,8 @@ const cleanCsvExport = /* @__PURE__ */ __name((rows) => {
14688
14688
  return rows;
14689
14689
  }, "cleanCsvExport");
14690
14690
  const filterFilesInZip = /* @__PURE__ */ __name((file, accepted) => __async(void 0, null, function* () {
14691
+ if (!file || Array.isArray(file) && !file.length)
14692
+ return [];
14691
14693
  const zipExtracted = yield extractZipFiles(file);
14692
14694
  const acceptedFiles = [];
14693
14695
  for (const extFile of zipExtracted) {
package/index.umd.js CHANGED
@@ -14692,6 +14692,8 @@ var __async = (__this, __arguments, generator) => {
14692
14692
  return rows;
14693
14693
  }, "cleanCsvExport");
14694
14694
  const filterFilesInZip = /* @__PURE__ */ __name((file, accepted) => __async(this, null, function* () {
14695
+ if (!file || Array.isArray(file) && !file.length)
14696
+ return [];
14695
14697
  const zipExtracted = yield extractZipFiles(file);
14696
14698
  const acceptedFiles = [];
14697
14699
  for (const extFile of zipExtracted) {
package/package.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "name": "@teselagen/file-utils",
3
- "version": "0.3.9",
4
- "type": "commonjs",
3
+ "version": "0.3.11",
5
4
  "dependencies": {
6
5
  "bluebird": "^3.7.2",
7
6
  "jszip": "^3.10.1",
package/src/file-utils.js CHANGED
@@ -14,38 +14,39 @@ const logDebug = (...args) => {
14
14
 
15
15
  export const allowedCsvFileTypes = [".csv", ".txt", ".xlsx"];
16
16
 
17
- export const isZipFile = (file) => {
17
+ export const isZipFile = file => {
18
18
  if (getExt(file) === "zip") return true;
19
19
  if (getExt(file) === "geneious") return false;
20
20
  const type = file.mimetype || file.type;
21
21
  return type === "application/zip" || type === "application/x-zip-compressed";
22
22
  };
23
23
 
24
- export const getExt = (file) => file?.name?.split(".").pop();
25
- export const isExcelFile = (file) => getExt(file) === "xlsx";
26
- export const isCsvFile = (file) => getExt(file) === "csv";
27
- export const isTextFile = (file) => ["text", "txt"].includes(getExt(file));
24
+ export const getExt = file => file?.name?.split(".").pop();
25
+ export const isExcelFile = file => getExt(file) === "xlsx";
26
+ export const isCsvFile = file => getExt(file) === "csv";
27
+ export const isTextFile = file => ["text", "txt"].includes(getExt(file));
28
28
 
29
- export const isCsvOrExcelFile = (file) => isCsvFile(file) || isExcelFile(file);
29
+ export const isCsvOrExcelFile = file => isCsvFile(file) || isExcelFile(file);
30
30
 
31
- export const extractZipFiles = async (allFiles) => {
31
+ export const extractZipFiles = async allFiles => {
32
32
  if (!Array.isArray(allFiles)) allFiles = [allFiles];
33
33
  // make a copy so we don't mutate the form value
34
34
  allFiles = [...allFiles];
35
35
  const zipFiles = remove(allFiles, isZipFile);
36
36
  if (!zipFiles.length) return allFiles;
37
37
  const zipFilesArray = Array.isArray(zipFiles) ? zipFiles : [zipFiles];
38
- const parsedZips = await Promise.map(zipFilesArray, (file) =>
38
+ const parsedZips = await Promise.map(zipFilesArray, file =>
39
39
  loadAsync(
40
- file instanceof (typeof Blob !== 'undefined' ? Blob : require("buffer").Blob)
40
+ file instanceof
41
+ (typeof Blob !== "undefined" ? Blob : require("buffer").Blob)
41
42
  ? file
42
43
  : file.originFileObj
43
44
  )
44
45
  );
45
- const zippedFiles = flatMap(parsedZips, (zip) =>
46
- Object.keys(zip.files).map((key) => zip.files[key])
46
+ const zippedFiles = flatMap(parsedZips, zip =>
47
+ Object.keys(zip.files).map(key => zip.files[key])
47
48
  );
48
- const unzippedFiles = await Promise.map(zippedFiles, (file) => {
49
+ const unzippedFiles = await Promise.map(zippedFiles, file => {
49
50
  // converts the compressed file to a string of its contents
50
51
  return file.async("blob").then(function (fileData) {
51
52
  const newFileObj = new File([fileData], file.name);
@@ -85,7 +86,7 @@ export const setupCsvParserOptions = (parserOptions = {}) => {
85
86
  const papaParseOpts = { ...rest };
86
87
  if (camelCaseHeaders) {
87
88
  logDebug("[CSV-PARSER] camelCasing headers");
88
- papaParseOpts.transformHeader = (header) => {
89
+ papaParseOpts.transformHeader = header => {
89
90
  let transHeader = header;
90
91
  if (!startsWith(header.trim(), "ext-")) {
91
92
  transHeader = camelCase(header);
@@ -103,7 +104,7 @@ export const setupCsvParserOptions = (parserOptions = {}) => {
103
104
  return transHeader;
104
105
  };
105
106
  } else if (lowerCaseHeaders) {
106
- papaParseOpts.transformHeader = (header) => {
107
+ papaParseOpts.transformHeader = header => {
107
108
  let transHeader = header;
108
109
  if (!startsWith(header, "ext-")) {
109
110
  transHeader = header.toLowerCase();
@@ -125,8 +126,7 @@ export const setupCsvParserOptions = (parserOptions = {}) => {
125
126
  return papaParseOpts;
126
127
  };
127
128
 
128
- const normalizeCsvHeaderHelper = (h) =>
129
- snakeCase(h.toUpperCase()).toUpperCase();
129
+ const normalizeCsvHeaderHelper = h => snakeCase(h.toUpperCase()).toUpperCase();
130
130
 
131
131
  export function normalizeCsvHeader(header) {
132
132
  if (header.startsWith("ext-") || header.startsWith("EXT-")) {
@@ -140,7 +140,7 @@ export const parseCsvFile = (csvFile, parserOptions = {}) => {
140
140
  const opts = {
141
141
  ...defaultCsvParserOptions,
142
142
  ...setupCsvParserOptions(parserOptions),
143
- complete: (results) => {
143
+ complete: results => {
144
144
  if (
145
145
  results &&
146
146
  results.data?.length &&
@@ -154,7 +154,7 @@ export const parseCsvFile = (csvFile, parserOptions = {}) => {
154
154
  }
155
155
  return resolve(results);
156
156
  },
157
- error: (error) => {
157
+ error: error => {
158
158
  reject(error);
159
159
  }
160
160
  };
@@ -232,7 +232,7 @@ export const validateCSVRequiredHeaders = (
232
232
  requiredHeaders,
233
233
  filename
234
234
  ) => {
235
- const missingRequiredHeaders = requiredHeaders.filter((field) => {
235
+ const missingRequiredHeaders = requiredHeaders.filter(field => {
236
236
  return !fields.includes(field);
237
237
  });
238
238
  if (missingRequiredHeaders.length) {
@@ -244,7 +244,7 @@ export const validateCSVRequiredHeaders = (
244
244
  };
245
245
 
246
246
  export const validateCSVRow = (row, requiredHeaders, index) => {
247
- const missingRequiredFields = requiredHeaders.filter((field) => !row[field]);
247
+ const missingRequiredFields = requiredHeaders.filter(field => !row[field]);
248
248
  if (missingRequiredFields.length) {
249
249
  if (missingRequiredFields.length === 1) {
250
250
  return `Row ${index + 1} is missing the required field "${
@@ -258,11 +258,11 @@ export const validateCSVRow = (row, requiredHeaders, index) => {
258
258
  }
259
259
  };
260
260
 
261
- export const cleanCommaSeparatedCell = (cellData) =>
261
+ export const cleanCommaSeparatedCell = cellData =>
262
262
  (cellData || "")
263
263
  .split(",")
264
- .map((n) => n.trim())
265
- .filter((n) => n);
264
+ .map(n => n.trim())
265
+ .filter(n => n);
266
266
 
267
267
  /**
268
268
  * Because the csv rows might not have the same header keys in some cases (extended properties)
@@ -270,17 +270,17 @@ export const cleanCommaSeparatedCell = (cellData) =>
270
270
  * does not drop fields
271
271
  * @param {*} rows
272
272
  */
273
- export const cleanCsvExport = (rows) => {
273
+ export const cleanCsvExport = rows => {
274
274
  const allHeaders = [];
275
- rows.forEach((row) => {
276
- Object.keys(row).forEach((header) => {
275
+ rows.forEach(row => {
276
+ Object.keys(row).forEach(header => {
277
277
  if (!allHeaders.includes(header)) {
278
278
  allHeaders.push(header);
279
279
  }
280
280
  });
281
281
  });
282
- rows.forEach((row) => {
283
- allHeaders.forEach((header) => {
282
+ rows.forEach(row => {
283
+ allHeaders.forEach(header => {
284
284
  row[header] = row[header] || "";
285
285
  });
286
286
  });
@@ -288,10 +288,11 @@ export const cleanCsvExport = (rows) => {
288
288
  };
289
289
 
290
290
  export const filterFilesInZip = async (file, accepted) => {
291
+ if (!file || (Array.isArray(file) && !file.length)) return [];
291
292
  const zipExtracted = await extractZipFiles(file);
292
293
  const acceptedFiles = [];
293
294
  for (const extFile of zipExtracted) {
294
- if (accepted.some((ext) => ext?.replace(".", "") === getExt(extFile))) {
295
+ if (accepted.some(ext => ext?.replace(".", "") === getExt(extFile))) {
295
296
  acceptedFiles.push(extFile);
296
297
  }
297
298
  }
@@ -321,7 +322,7 @@ export async function uploadAndProcessFiles(files = []) {
321
322
 
322
323
  const response = await window.api.post("/user_uploads/", formData);
323
324
 
324
- return response.data.map((d) => ({
325
+ return response.data.map(d => ({
325
326
  encoding: d.encoding,
326
327
  mimetype: d.mimetype,
327
328
  originalname: d.originalname,
@@ -344,8 +345,8 @@ export async function encodeFilesForRequest(files) {
344
345
  return encodedFiles;
345
346
  }
346
347
 
347
- const fileToBase64 = (file) => {
348
- return new Promise((resolve) => {
348
+ const fileToBase64 = file => {
349
+ return new Promise(resolve => {
349
350
  const reader = new FileReader();
350
351
  // Read file content on file loaded event
351
352
  reader.onload = function (event) {
package/src/index.js CHANGED
@@ -1 +1 @@
1
- export * from './file-utils';
1
+ export * from "./file-utils";