Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const steps = resources.map(async res => {
if (excelFormats.includes(res.descriptor.format)) {
const buffer = await res.buffer
const workbook = XLSX.read(buffer, {type: 'buffer', bookSheets: true})
if (sheets === 'all') {
sheets = Array.from(Array(workbook.SheetNames.length).keys())
// We are using sheet idx starting from 1 so we need to add 1 to each idx:
sheets = sheets.map(value => value + 1)
} else if (sheets) { // When sheets are indexes of sheets separated by ','
sheets = sheets.split(',').map(value => parseInt(value))
} else { // Default case
sheets = [1]
}
for (let i of sheets) {
const rows = await toArray(await xlsxParser(res, false, i-1))
const schema = await infer(rows)
const step = {
input: res.descriptor.name,
output: `${res.descriptor.name}-sheet-${i}`,
tabulator: {
sheet: i
},
schema
}
processingSteps.push(step)
}
}
})
await Promise.all(steps)
async addSchema() {
// Ensure resource is tabular
if (knownTabularFormats.indexOf(this.descriptor.format) === -1) {
throw new Error('File is not in known tabular format.')
}
const rows = await toArray(await this.rows())
this.descriptor.schema = await infer(rows)
}
}
return (0, _asyncToGenerator3.default)(function* () {
// Ensure resource is tabular
if (knownTabularFormats.indexOf(_this2.descriptor.format) === -1) {
throw new Error('File is not in known tabular format.');
}
const rows = yield toArray((yield _this2.rows()));
_this2.descriptor.schema = yield infer(rows);
})();
}
this.descriptor.schema = await infer(this.descriptor.data);
return;
} // Get parserOptions so we can use it when "infering" schema:
const parserOptions = await guessParseOptions(this); // We also need to include parserOptions in "dialect" property of descriptor:
this.descriptor.dialect = {
delimiter: parserOptions.delimiter,
quoteChar: parserOptions.quote // Now let's get a stream from file and infer schema:
};
let thisFileStream = await this.stream({
size: 100
});
this.descriptor.schema = await infer(thisFileStream, parserOptions);
}
async addSchema() {
// Ensure file is tabular
if (knownTabularFormats.indexOf(this.descriptor.format) === -1) {
throw new Error('File is not in known tabular format.')
}
if (this.displayName === 'FileInline') {
this.descriptor.schema = await infer(this.descriptor.data)
return
}
// Get parserOptions so we can use it when "infering" schema:
const parserOptions = await guessParseOptions(this)
// We also need to include parserOptions in "dialect" property of descriptor:
this.descriptor.dialect = {
delimiter: parserOptions.delimiter,
quoteChar: parserOptions.quote
}
// Now let's get a stream from file and infer schema:
let thisFileStream = await this.stream({size: 100})
this.descriptor.schema = await infer(thisFileStream, parserOptions)
}
}
async addSchema() {
// Ensure file is tabular
if (knownTabularFormats.indexOf(this.descriptor.format) === -1) {
throw new Error('File is not in known tabular format.');
}
if (this.displayName === 'FileInline') {
this.descriptor.schema = await infer(this.descriptor.data);
return;
} // Get parserOptions so we can use it when "infering" schema:
const parserOptions = await guessParseOptions(this); // We also need to include parserOptions in "dialect" property of descriptor:
this.descriptor.dialect = {
delimiter: parserOptions.delimiter,
quoteChar: parserOptions.quote // Now let's get a stream from file and infer schema:
};
let thisFileStream = await this.stream({
size: 100
});
this.descriptor.schema = await infer(thisFileStream, parserOptions);
}
throw new Error('File is not in known tabular format.')
}
if (this.displayName === 'FileInline') {
this.descriptor.schema = await infer(this.descriptor.data)
return
}
// Get parserOptions so we can use it when "infering" schema:
const parserOptions = await guessParseOptions(this)
// We also need to include parserOptions in "dialect" property of descriptor:
this.descriptor.dialect = {
delimiter: parserOptions.delimiter,
quoteChar: parserOptions.quote
}
// Now let's get a stream from file and infer schema:
let thisFileStream = await this.stream({size: 100})
this.descriptor.schema = await infer(thisFileStream, parserOptions)
}
}
throw new Error(`sheet index ${sheet} is out of range: please, provide existing sheet index or use sheet name.`)
} else if (sheet.constructor.name === 'String' && !workbook.SheetNames.includes(sheet)) {
throw new Error(`sheet name ${sheet} does not exist in the given file.`)
} else if (sheet.constructor.name === 'String') {
sheets[idx] = workbook.SheetNames.indexOf(sheet) + 1
}
})
} else { // Default case
sheets = [1]
}
for (let i of sheets) {
const rows = await toArray(await xlsxParser(res, false, i-1))
if (rows.length === 0) {
throw new Error('You cannot push an empty sheet. Please, add some data and try again.')
}
const schema = await infer(rows)
// hacky way to handle types of excel numbers (they actually are floats)
schema.fields.forEach(field => {
if (field.type === 'integer') {
field.type = 'number'
}
})
const step = {
input: res.descriptor.name,
output: `${res.descriptor.name}-sheet-${i}`,
tabulator: {
sheet: i
},
schema
}
processingSteps.push(step)
}
fs.readFile(path_, (err, data) => {
if (err) {
reject(err);
return;
}
const values = parse(data);
const schema = infer(values);
resolve(schema);
});
});
fs.readFile(path_, (err, data) => {
if (err) {
reject(err)
return
}
const values = parse(data)
const schema = infer(values)
resolve(schema)
})
})