@teselagen/file-utils 0.3.8 → 0.3.10

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
@@ -14514,7 +14514,7 @@ const extractZipFiles = /* @__PURE__ */ __name((allFiles) => __async(exports, nu
14514
14514
  const parsedZips = yield Promise$1.map(
14515
14515
  zipFilesArray,
14516
14516
  (file) => jszip_minExports.loadAsync(
14517
- file instanceof (Blob ? Blob : require("buffer").Blob) ? file : file.originFileObj
14517
+ file instanceof (typeof Blob !== "undefined" ? Blob : require("buffer").Blob) ? file : file.originFileObj
14518
14518
  )
14519
14519
  );
14520
14520
  const zippedFiles = lodashExports.flatMap(
package/index.mjs CHANGED
@@ -14512,7 +14512,7 @@ const extractZipFiles = /* @__PURE__ */ __name((allFiles) => __async(void 0, nul
14512
14512
  const parsedZips = yield Promise$1.map(
14513
14513
  zipFilesArray,
14514
14514
  (file) => jszip_minExports.loadAsync(
14515
- file instanceof (Blob ? Blob : require("buffer").Blob) ? file : file.originFileObj
14515
+ file instanceof (typeof Blob !== "undefined" ? Blob : require("buffer").Blob) ? file : file.originFileObj
14516
14516
  )
14517
14517
  );
14518
14518
  const zippedFiles = lodashExports.flatMap(
package/index.umd.js CHANGED
@@ -14516,7 +14516,7 @@ var __async = (__this, __arguments, generator) => {
14516
14516
  const parsedZips = yield Promise$1.map(
14517
14517
  zipFilesArray,
14518
14518
  (file) => jszip_minExports.loadAsync(
14519
- file instanceof (Blob ? Blob : require("buffer").Blob) ? file : file.originFileObj
14519
+ file instanceof (typeof Blob !== "undefined" ? Blob : require("buffer").Blob) ? file : file.originFileObj
14520
14520
  )
14521
14521
  );
14522
14522
  const zippedFiles = lodashExports.flatMap(
package/package.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "name": "@teselagen/file-utils",
3
- "version": "0.3.8",
4
- "type": "commonjs",
3
+ "version": "0.3.10",
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 (Blob ? 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
  });
@@ -291,7 +291,7 @@ export const filterFilesInZip = async (file, accepted) => {
291
291
  const zipExtracted = await extractZipFiles(file);
292
292
  const acceptedFiles = [];
293
293
  for (const extFile of zipExtracted) {
294
- if (accepted.some((ext) => ext?.replace(".", "") === getExt(extFile))) {
294
+ if (accepted.some(ext => ext?.replace(".", "") === getExt(extFile))) {
295
295
  acceptedFiles.push(extFile);
296
296
  }
297
297
  }
@@ -321,7 +321,7 @@ export async function uploadAndProcessFiles(files = []) {
321
321
 
322
322
  const response = await window.api.post("/user_uploads/", formData);
323
323
 
324
- return response.data.map((d) => ({
324
+ return response.data.map(d => ({
325
325
  encoding: d.encoding,
326
326
  mimetype: d.mimetype,
327
327
  originalname: d.originalname,
@@ -344,8 +344,8 @@ export async function encodeFilesForRequest(files) {
344
344
  return encodedFiles;
345
345
  }
346
346
 
347
- const fileToBase64 = (file) => {
348
- return new Promise((resolve) => {
347
+ const fileToBase64 = file => {
348
+ return new Promise(resolve => {
349
349
  const reader = new FileReader();
350
350
  // Read file content on file loaded event
351
351
  reader.onload = function (event) {
package/src/index.js CHANGED
@@ -1 +1 @@
1
- export * from './file-utils';
1
+ export * from "./file-utils";