All files / src/utils jsonUtil.js

0% Statements 0/16
0% Branches 0/2
0% Functions 0/4
0% Lines 0/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22                                           
export const downloadData2File = (data, MIME = { type: 'application/json' }, ext = 'json') => {
    const blob = new Blob([JSON.stringify(data)], MIME),
        fileName = `${new Date().valueOf()}.${ext}`,
        link = document.createElement('a');
 
    link.href = window.URL.createObjectURL(blob);
    link.download = fileName;
    link.click();
    window.URL.revokeObjectURL(link.href);
}
 
export const getJsonData = file => new Promise((resolve, reject) => {
    try {
        const r = new FileReader();
        r.readAsText(file);
        r.onload = function ({ target: { result } }) {
            resolve(JSON.parse(result));
        }
    } catch (error) {
        reject(error);
    }
})