forked from GEOLYTIX/xyz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request GEOLYTIX#1021 from dbauszus-glx/csv-download
csvDownload params; toLocaleString
- Loading branch information
Showing
3 changed files
with
69 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,71 @@ | ||
export default function csvDownload(arr, params = {}) { | ||
export default function csvDownload(data, params = {}) { | ||
|
||
if (!Array.isArray(arr)) { | ||
console.warn('Array argument for csvDownload must be an array') | ||
return; | ||
if (!Array.isArray(data)) { | ||
console.warn('Array argument for csvDownload must be an array') | ||
return; | ||
} | ||
|
||
// Set comma as default separator. | ||
params.separator ??= ',' | ||
|
||
const rows = data.map(record => { | ||
|
||
let row = Array.isArray(params.fields) | ||
? fieldsFunction(record, params.fields) | ||
: Object.values(record) | ||
|
||
return row.join(params.separator) | ||
}) | ||
|
||
// Set default type for the blob. | ||
params.type ??= 'text/csv;charset=utf-8;' | ||
|
||
params.join ??= '\r\n' | ||
|
||
params.title ??= 'file' | ||
|
||
if (params.header && Array.isArray(params.fields)) { | ||
|
||
// Unshift the header row with either the title or field names. | ||
rows.unshift(params.fields.map(field => field.title || field.field)) | ||
} | ||
|
||
const blob = new Blob([rows.join(params.join)], { type: params.type }) | ||
|
||
const link = document.createElement('a') | ||
|
||
link.download = `${params.title}.csv` | ||
link.href = URL.createObjectURL(blob) | ||
link.dataset.downloadurl = ['csv', link.download, link.href].join(':') | ||
link.style.display = 'none' | ||
document.body.append(link) | ||
link.click() | ||
link.remove() | ||
} | ||
|
||
function fieldsFunction(record, fields) { | ||
|
||
return fields.map(field => { | ||
|
||
if (!field.field) return; | ||
|
||
// Escape quotes in string value. | ||
if (typeof record[field.field] === 'string' && field.string) { | ||
|
||
return `"${record[field.field].replaceAll('"', '\\"')}"` | ||
} | ||
|
||
const rows = arr.map(record => { | ||
// Format number toLocaleString | ||
if (field.formatter === 'toLocaleString') { | ||
|
||
return Object.values(record).join(',') | ||
}) | ||
let val = parseFloat(record[field.field]) | ||
|
||
const blob = new Blob([rows.join('\r\n')], { type: 'text/csv;charset=utf-8;' }) | ||
if (isNaN(val)) return; | ||
|
||
return `"${val.toLocaleString(field.locale || navigator.language, field.options)}"` | ||
} | ||
|
||
const link = document.createElement('a') | ||
return record[field.field] | ||
|
||
link.download = `${params.title || 'file'}.csv` | ||
link.href = URL.createObjectURL(blob) | ||
link.dataset.downloadurl = ['csv', link.download, link.href].join(':') | ||
link.style.display = 'none' | ||
document.body.append(link) | ||
link.click() | ||
link.remove() | ||
}) | ||
} |