Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const input = createReadStream('/path/to/input', { encoding: 'utf8' });
const output = createWriteStream('path/to/output', { encoding: 'utf8' });
asyncParser.fromInput(input).promise()
.then(csv => console.log(csv))
.catch(err => console.error(err));
asyncParser.fromInput(input).toOutput(output);
// Test convenience method "parseAsync" with object input
parseAsync(data, opts)
.then(csv => console.log(csv))
.catch(err => console.error(err));
// Test convenience method "parseAsync" with stream input
parseAsync(input, opts)
.then(csv => console.log(csv))
.catch(err => console.error(err));
/********************
* Internal Methods *
********************/
class ParserExt extends Parser {
constructor() {
super();
// Parser methods
obj = this.preprocessData({});
obj = this.preprocessData({ str: '', num: 1, obj: {} });
obj = this.preprocessData([]);
s = this.processData([]);
// JSON2CSVBase methods
.on('error', err => console.log(err));
asyncParser.input.push(data); // This data might come from an HTTP request, etc.
asyncParser.input.push(null); // Sending `null` to a stream signal that no more data is expected and ends it.
const input = createReadStream('/path/to/input', { encoding: 'utf8' });
const output = createWriteStream('path/to/output', { encoding: 'utf8' });
asyncParser.fromInput(input).promise()
.then(csv => console.log(csv))
.catch(err => console.error(err));
asyncParser.fromInput(input).toOutput(output);
// Test convenience method "parseAsync" with object input
parseAsync(data, opts)
.then(csv => console.log(csv))
.catch(err => console.error(err));
// Test convenience method "parseAsync" with stream input
parseAsync(input, opts)
.then(csv => console.log(csv))
.catch(err => console.error(err));
/********************
* Internal Methods *
********************/
class ParserExt extends Parser {
constructor() {
super();
// Parser methods
obj = this.preprocessData({});
// Build rows.
for (let i = 0; i < submissions.length; i++) {
let submissionData = submissions[i].data;
const row = {};
Object.keys(fields).map(fieldId => {
if (fieldId in submissionData) {
row[fields[fieldId]] = submissionData[fieldId];
} else {
row[fields[fieldId]] = "N/A";
}
});
rows.push(row);
}
// Save CSV file and return its URL to the client.
const csv = await parseAsync(rows, { fields: Object.values(fields) });
const buffer = Buffer.from(csv);
const result = await uploadFile({
context,
buffer,
file: {
size: buffer.length,
name: "form_submissions_export.csv",
type: "text/csv"
}
});
return new Response(result);
};
const courseId = ctx.params.courseId;
const students = await getStudentsScore(courseId);
const courseTasks = await getCourseTasks(courseId);
const result = students.map(student => {
return {
githubId: student.githubId,
name: student.name,
locationName: student.locationName,
countryName: countriesMap[citiesMap[student.locationName]] || 'Other',
mentorGithubId: student.mentor ? (student.mentor as any).githubId : '',
totalScore: student.totalScore,
...getTasksResults(student.taskResults, courseTasks),
};
});
const csv = await parseAsync(result);
setCsvResponse(ctx, OK, csv, 'score');
};