aboutsummaryrefslogtreecommitdiff
path: root/download-utils.js
blob: 17b59a0ea4fb2598bcff141ef3b5e27d66cd5950 (plain)
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
/*
    Author: sotech117
    License: Educational under Brown University
    Date: 1/22/24
 */

// 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');
}