-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-csv.js
More file actions
87 lines (74 loc) · 2.54 KB
/
format-csv.js
File metadata and controls
87 lines (74 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function parse(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.addEventListener('load', (event) => {
const { result } = event.target;
const { errors, data } = Papa.parse(result, { delimiter: ',', header: false });
if(errors && errors.length) {
console.log({errors});
reject(errors.map(error => error.message).join(', '));
} else {
resolve (data);
}
});
reader.readAsText(file);
});
}
function hideImportError() {
const formatErrorMessageEl = document.querySelector('#format-error-message');
formatErrorMessageEl.hidden = true;
}
function showImportError(message) {
const formatErrorsEl = document.querySelector('#format-errors');
formatErrorsEl.innerText = message;
const formatErrorMessageEl = document.querySelector('#format-error-message');
formatErrorMessageEl.hidden = false;
}
function validateRecordFormat(header) {
return header[0] === 'Last Name' && header[1] === 'First Name' && header[2] === 'Username';
}
/**
* "Instructors can add users through one of several formats, which also support user names:
*
* • "A User" buser@example.com
* • buser@example.com B User
* • "User, C" cuser@example.com
*
* Email addresses can be enclosed by less than ( < ) or greater than ( > ) symbols, although it is not required.”
*/
function formatStudentRecord(record) {
const [lname, fname, username] = record;
// "Humphrey, David" <david.humphrey@myseneca.ca>
return `"${lname}, ${fname}" <${username}@myseneca.ca>`;
}
function formatStudentRecords(data) {
if(!(data && data.length)) {
showImportError('No records found in parsed data.');
return;
}
// First row are headers, rest are records
let [ header, ...students ] = data;
if(!validateRecordFormat(header)) {
showImportError('Unexpected format in parsed data.');
return;
}
// Remove empty record(s)
students = students.filter(student => student.length >= 3);
// Format records for import
students = students.map(student => formatStudentRecord(student)).join(', ');
const studentRecordsEl = document.querySelector('#student-records');
studentRecordsEl.value = students;
}
window.onload = function() {
const fileEl = document.getElementById('csv-file-input');
fileEl.addEventListener('change', async (event) => {
hideImportError();
const [ csvFile ] = event.target.files;
try {
const data = await parse(csvFile);
formatStudentRecords(data);
} catch(err) {
showImportError(err);
}
});
};