aboutsummaryrefslogtreecommitdiff
path: root/download-utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'download-utils.js')
-rw-r--r--download-utils.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/download-utils.js b/download-utils.js
new file mode 100644
index 0000000..0156bdc
--- /dev/null
+++ b/download-utils.js
@@ -0,0 +1,50 @@
+// citation: https://www.geeksforgeeks.org/how-to-create-and-download-csv-file-in-javascript/
+
+// performs a downloadUtils of a csv file, given a string buffer in the format of csv text
+const download = data => {
+ // creating a Blob for having a csv file format
+ // and passing the data with type
+ const blob = new Blob([data], {type: 'text/csv'});
+
+ // Creating an object for downloading url
+ const url = window.URL.createObjectURL(blob);
+
+ // Creating an anchor(a) tag of HTML
+ const a = document.createElement('a');
+
+ // Passing the blob downloading url
+ a.setAttribute('href', url);
+
+ // Setting the anchor tag attribute for downloading
+ // and passing the downloadUtils file name
+ const date = new Date();
+ // stamp in format of `YYYY-MM-DDTHHmmss`
+ const stamp = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}T${date.getHours()}${date.getMinutes()}${date.getSeconds()}`;
+ const fileName = `cs1300-ab-testing-data-${stamp}.csv`;
+ a.setAttribute('download', fileName);
+
+ // Performing a downloadUtils with click
+ a.click()
+}
+
+// given the data as an array of objects, generates the text of it's corresponding csv
+const buildcsv = data => {
+ // Empty array for storing the values
+ let csvRows = [];
+
+ console.log("buildcsv", data);
+ data.forEach(d => {
+ // if the array is empty, append the headers (which are the keys)
+ if (csvRows.length === 0) {
+ const headers = Object.keys(d);
+ csvRows.push(headers.join(','));
+ }
+
+ // append the data
+ const values = Object.values(d).join(',');
+ csvRows.push(values)
+
+ })
+ // Returning the array joining with new line
+ return csvRows.join('\n');
+}