Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
Papa.parse(file, {
skipEmptyLines: true,
complete: ({data}) => {
if (data.length === 0) {
NylasEnv.showErrorDialog(
`The csv file you are trying to import contains no rows. Please select another file.`
);
resolve(null)
return;
}
// If a cell in the first row contains a valid email address, assume that
// the table has no headers. We need row[0] to be field names, so make some up!
const emailRegexp = RegExpUtils.emailRegex();
const emailInFirstRow = data[0].find((val) => emailRegexp.test(val));
if (emailInFirstRow) {
const headers = data[0].map((val, idx) => {
readCSV(text: string): SeriesData[] {
this.data = [this.series];
const papacfg = {
...this.config,
dynamicTyping: false,
skipEmptyLines: true,
comments: false, // Keep comment lines
step: this.step,
} as ParseConfig;
Papa.parse(text, papacfg);
return this.data;
}
}
export const loadCsvFileWithoutWorker = (path, onComplete) => {
const batchId = Date.now();
// https://www.papaparse.com/docs#config
parse(path, {
delimiter: ',',
download: true,
dynamicTyping: true,
// TODO: header: true lead to large memory consumption
header: true,
newline: '',
quotes: false,
quoteChar: '"',
skipEmptyLines: true,
complete: results => {
const {
data,
meta: {fields},
} = results;
onComplete({data, fields, batchId});
},
const { headers, contacts, errors } = await new Promise((resolve, reject) => {
const onComplete = ({ data = [], errors = [] } = {}) =>
resolve({ headers: data[0], contacts: data.slice(1), errors });
Papa.parse(file, {
header: false,
/*
If true, the first row of parsed data will be interpreted as field names. An array of field names will be returned in meta,
and each row of data will be an object of values keyed by field name instead of a simple array.
Rows with a different number of fields from the header row will produce an error.
*/
dynamicTyping: false, // If true, numeric and Boolean data will be converted to their type instead of remaining strings.
complete: onComplete,
error: reject,
skipEmptyLines: true // If true, lines that are completely empty will be skipped. An empty line is defined to be one which evaluates to empty string.
});
});
value: function parse() {
var _this = this;
var csvMatrixLocal = [];
var count = 0;
var f = this.parseReturn;
Papa.parse(this.csvFile, {
download: true,
dynamicTyping: true,
comments: true,
step: function step(row) {
csvMatrixLocal[count] = row.data[0];
count += 1;
},
complete: function complete() {
_this.callbackForLocalFile(csvMatrixLocal);
}
});
} // parsing string: for remote and csvString import options. Dat is parsed line by line but NOT asynchronously.
fetchCsvRows() {
const { entity } = this.props;
const url = entity.links.csv;
// set chunk size to 100 KB
Papa.RemoteChunkSize = 1024 * 100;
Papa.parse(url, {
download: true,
delimiter: ',',
newline: '\n',
encoding: 'utf-8',
chunk: (results, parser) => {
this.setState({
csvRows: results.data.slice(0, 10),
});
parser.abort();
},
});
}
transform: (val: string) => {
if (val.indexOf('"') >= 0) {
return val.trim().replace(/(^\"|\"$)/g, '');
}
return val;
},
};
if (delimiter != null) {
config.delimiter = delimiter;
}
if (newline != null) {
config.newline = newline;
}
const output = Papa.parse(csvString, config);
const { data, errors } = output;
if (errors.length > 0) {
throw errorMessages.invalidInputCSV();
}
// output.data is an array of arrays, rows and values in each row
return data.reduce(
(acc, row, i) => {
if (i === 0) {
// first row, assume header values
row.forEach((colName: string) =>
acc.columns.push({ name: colName.trim(), type: 'string' })
);
} else {
// any other row is a data row
const convertCSVToObject = csv => {
const data = Papa.parse(csv, {
header: true
});
return data;
};
static createFromCSV(url, callback) {
Papa.parse(url, {
download: true,
header: true,
dynamicTyping: true,
complete: function(results) {
var ds = new Dataset();
for (let i in results.data) {
let dp = results.data[i];
dp._id = i;
ds.add(dp);
}
callback(ds);
}
});
}