@teselagen/ove 0.7.30-beta.1 → 0.7.30

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/src/fileUtils.js DELETED
@@ -1,103 +0,0 @@
1
- /* Copyright (C) 2018 TeselaGen Biotechnology, Inc. */
2
- import { parse } from "papaparse";
3
-
4
- export const allowedCsvFileTypes = [".csv", ".txt", ".xlsx"];
5
-
6
- export const isZipFile = file => {
7
- const type = file.mimetype || file.type;
8
- return type === "application/zip" || type === "application/x-zip-compressed";
9
- };
10
-
11
- export const getExt = file => file.name.split(".").pop();
12
- export const isExcelFile = file => getExt(file) === "xlsx";
13
- export const isCsvFile = file => getExt(file) === "csv";
14
- export const isTextFile = file => ["text", "txt"].includes(getExt(file));
15
-
16
- const defaultCsvParserOptions = {
17
- header: true,
18
- skipEmptyLines: "greedy",
19
- trimHeaders: true
20
- // delimiter: ","
21
- };
22
-
23
- export const parseCsvFile = (csvFile, parserOptions = {}) => {
24
- return new Promise((resolve, reject) => {
25
- parse(csvFile.originFileObj, {
26
- ...defaultCsvParserOptions,
27
- complete: results => {
28
- if (results && results.errors && results.errors.length) {
29
- return reject("Error in csv: " + JSON.stringify(results.errors));
30
- }
31
- resolve(results);
32
- },
33
- error: error => {
34
- reject(error);
35
- },
36
- ...parserOptions
37
- });
38
- });
39
- };
40
-
41
- export const parseCsvString = (csvString, parserOptions = {}) => {
42
- return parse(csvString, { ...defaultCsvParserOptions, ...parserOptions });
43
- };
44
-
45
- export const cleanCommaSeparatedCell = cellData =>
46
- (cellData || "")
47
- .split(",")
48
- .map(n => n.trim())
49
- .filter(n => n);
50
-
51
- /**
52
- * Because the csv rows might not have the same header keys in some cases (extended properties)
53
- * this function will make sure that each row will have all headers so that the export
54
- * does not drop fields
55
- * @param {*} rows
56
- */
57
- export const cleanCsvExport = rows => {
58
- const allHeaders = [];
59
- rows.forEach(row => {
60
- Object.keys(row).forEach(header => {
61
- if (!allHeaders.includes(header)) {
62
- allHeaders.push(header);
63
- }
64
- });
65
- });
66
- rows.forEach(row => {
67
- allHeaders.forEach(header => {
68
- row[header] = row[header] || "";
69
- });
70
- });
71
- return rows;
72
- };
73
-
74
- export const validateCSVRequiredHeaders = (
75
- fields,
76
- requiredHeaders,
77
- filename
78
- ) => {
79
- const missingRequiredHeaders = requiredHeaders.filter(field => {
80
- return !fields.includes(field);
81
- });
82
- if (missingRequiredHeaders.length) {
83
- const name = filename ? `The file ${filename}` : "CSV file";
84
- return `${name} is missing required headers. (${missingRequiredHeaders.join(
85
- ", "
86
- )})`;
87
- }
88
- };
89
-
90
- export const validateCSVRow = (row, requiredHeaders, index) => {
91
- const missingRequiredFields = requiredHeaders.filter(field => !row[field]);
92
- if (missingRequiredFields.length) {
93
- if (missingRequiredFields.length === 1) {
94
- return `Row ${index + 1} is missing the required field "${
95
- missingRequiredFields[0]
96
- }"`;
97
- } else {
98
- return `Row ${
99
- index + 1
100
- } is missing these required fields: ${missingRequiredFields.join(", ")}`;
101
- }
102
- }
103
- };