๐Ÿ“ฆ robzwolf / freeagent-csv-formatter

๐Ÿ“„ utilities.js ยท 40 lines
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
40export function tidyWhitespace(data) {
    if (typeof data === "undefined" || !data) {
        return "";
    }

    return data.trim().replace(/\s\s+/g, ' ');
}

export const freeagentDateFormat = "DD/MM/YYYY"

export function downloadContentsAsCsvFile(csvExport) {
    window.URL = window.webkitURL || window.URL;

    const contentType = 'text/csv';
    const csvFile = new Blob([csvExport], {type: contentType});
    const a = document.createElement('a');
    a.download = 'statement.csv';
    a.href = window.URL.createObjectURL(csvFile);
    a.dataset.downloadurl = [contentType, a.download, a.href].join(':');
    a.click();
}

export function negateAmount(amount) {
    return `${-amount}`;
}

export function MissingHeaderException(header, row) {
    this.header = header;
    this.row = row;
    this.message = `Row does not have '${header}' key`;
}

export function checkForHeaders(headers, row) {
    headers.forEach(header => {
        if (!row.hasOwnProperty(header)) {
            throw new MissingHeaderException(header, row);
        }
    })
}