@cloudflare/util-http-file
Advanced tools
Comparing version
@@ -6,2 +6,24 @@ # Change Log | ||
# [1.2.0](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-http-file@1.1.10...@cloudflare/util-http-file@1.2.0) (2022-03-24) | ||
### Bug Fixes | ||
* **stratus:** A11Y-106 build fix ([73dc31c](http://stash.cfops.it:7999/fe/stratus/commits/73dc31c)) | ||
* **stratus:** A11Y-106 conflicting files ([19209dc](http://stash.cfops.it:7999/fe/stratus/commits/19209dc)) | ||
* **stratus:** A11Y-106 merge conflicting file ([940a252](http://stash.cfops.it:7999/fe/stratus/commits/940a252)) | ||
* **stratus:** A11Y-106 PR comments ([c9bc22a](http://stash.cfops.it:7999/fe/stratus/commits/c9bc22a)) | ||
* **stratus:** A11Y-109 build fix ([cee6c7f](http://stash.cfops.it:7999/fe/stratus/commits/cee6c7f)) | ||
### Features | ||
* **stratus:** A11Y-106 meaningful chartIds for A11Y Metadata ([e7e606c](http://stash.cfops.it:7999/fe/stratus/commits/e7e606c)) | ||
* **stratus:** A11Y-109 chart3 and chart4 ([69bfdd0](http://stash.cfops.it:7999/fe/stratus/commits/69bfdd0)) | ||
* **stratus:** A11Y-109 improved download csv ([586c7f2](http://stash.cfops.it:7999/fe/stratus/commits/586c7f2)) | ||
## [1.1.10](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-http-file@1.1.9...@cloudflare/util-http-file@1.1.10) (2022-03-23) | ||
@@ -8,0 +30,0 @@ |
@@ -32,5 +32,5 @@ import saveFile from './saveFile'; | ||
export const convertTitleToFilename = (title = 'chart_download') => { | ||
export const convertTitleToFilename = (title = 'file_download') => { | ||
if (typeof title !== 'string') { | ||
title = 'chart_download'; | ||
title = 'file_download'; | ||
} else { | ||
@@ -43,11 +43,50 @@ title = title.trim(); | ||
/** | ||
* Lookup for column titles in a data object to replace non-friendly flattened column names for the downloaded file | ||
* @param {*} data | ||
* @param {*} columnsDictionary | ||
* @returns | ||
*/ | ||
const replaceCSVColumnsToFriendlyName = (data, columnsDictionary) => { | ||
const newData = []; | ||
data.forEach(row => { | ||
const newRow = {}; | ||
Object.keys(row).forEach(key => { | ||
if (columnsDictionary[key]) { | ||
newRow[columnsDictionary[key]] = row[key]; | ||
} else { | ||
newRow[key] = row[key]; | ||
} | ||
}); | ||
newData.push(newRow); | ||
}); | ||
return newData; | ||
}; | ||
/** | ||
* Downloads a CSV file from a given table data object | ||
* @param {*} tableData | ||
* @param {*} filename | ||
* @param {*} fileTitle | ||
* @param {*} columnsDictionary | ||
*/ | ||
export const downloadCSV = (tableData, filename) => { | ||
const flattenedTableData = tableData.map(row => flatten(row)); | ||
const data = Papa.unparse(flattenedTableData); | ||
saveFile(`${filename}.csv`, data, DOWNLOAD_DATA_TYPES.CSV); | ||
export const downloadCSV = (tableData, fileTitle, columnsDictionary = {}) => { | ||
const filename = convertTitleToFilename(fileTitle); | ||
let transformedData = tableData; | ||
if (tableData[0] && typeof tableData[0] === 'object') { | ||
// Assumption this is an Array of Objects | ||
transformedData = tableData.map(row => flatten(row)); | ||
} else if (tableData[0] && typeof tableData[0] === 'number') { | ||
// Assumption this is an Array of numbers | ||
transformedData = tableData.map(val => { | ||
return { | ||
value: val | ||
}; | ||
}); | ||
} | ||
const data = replaceCSVColumnsToFriendlyName(transformedData, columnsDictionary); | ||
const csvData = Papa.unparse(data); | ||
saveFile(`${filename}.csv`, csvData, DOWNLOAD_DATA_TYPES.CSV); | ||
}; | ||
@@ -54,0 +93,0 @@ /** |
@@ -27,2 +27,4 @@ "use strict"; | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function downloadFile(url, filename, callback) { | ||
@@ -58,6 +60,6 @@ return (0, _utilHttp.get)(url, null, function (err, res) { | ||
var convertTitleToFilename = function convertTitleToFilename() { | ||
var title = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'chart_download'; | ||
var title = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'file_download'; | ||
if (typeof title !== 'string') { | ||
title = 'chart_download'; | ||
title = 'file_download'; | ||
} else { | ||
@@ -70,5 +72,6 @@ title = title.trim(); | ||
/** | ||
* Downloads a CSV file from a given table data object | ||
* @param {*} tableData | ||
* @param {*} filename | ||
* Lookup for column titles in a data object to replace non-friendly flattened column names for the downloaded file | ||
* @param {*} data | ||
* @param {*} columnsDictionary | ||
* @returns | ||
*/ | ||
@@ -79,10 +82,49 @@ | ||
var downloadCSV = function downloadCSV(tableData, filename) { | ||
var flattenedTableData = tableData.map(function (row) { | ||
return (0, _flat.default)(row); | ||
var replaceCSVColumnsToFriendlyName = function replaceCSVColumnsToFriendlyName(data, columnsDictionary) { | ||
var newData = []; | ||
data.forEach(function (row) { | ||
var newRow = {}; | ||
Object.keys(row).forEach(function (key) { | ||
if (columnsDictionary[key]) { | ||
newRow[columnsDictionary[key]] = row[key]; | ||
} else { | ||
newRow[key] = row[key]; | ||
} | ||
}); | ||
newData.push(newRow); | ||
}); | ||
return newData; | ||
}; | ||
/** | ||
* Downloads a CSV file from a given table data object | ||
* @param {*} tableData | ||
* @param {*} fileTitle | ||
* @param {*} columnsDictionary | ||
*/ | ||
var data = _papaparse.default.unparse(flattenedTableData); | ||
(0, _saveFile.default)("".concat(filename, ".csv"), data, DOWNLOAD_DATA_TYPES.CSV); | ||
var downloadCSV = function downloadCSV(tableData, fileTitle) { | ||
var columnsDictionary = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var filename = convertTitleToFilename(fileTitle); | ||
var transformedData = tableData; | ||
if (tableData[0] && _typeof(tableData[0]) === 'object') { | ||
// Assumption this is an Array of Objects | ||
transformedData = tableData.map(function (row) { | ||
return (0, _flat.default)(row); | ||
}); | ||
} else if (tableData[0] && typeof tableData[0] === 'number') { | ||
// Assumption this is an Array of numbers | ||
transformedData = tableData.map(function (val) { | ||
return { | ||
value: val | ||
}; | ||
}); | ||
} | ||
var data = replaceCSVColumnsToFriendlyName(transformedData, columnsDictionary); | ||
var csvData = _papaparse.default.unparse(data); | ||
(0, _saveFile.default)("".concat(filename, ".csv"), csvData, DOWNLOAD_DATA_TYPES.CSV); | ||
}; | ||
@@ -89,0 +131,0 @@ /** |
{ | ||
"name": "@cloudflare/util-http-file", | ||
"description": "Cloudflare HTTP File Util", | ||
"version": "1.1.10", | ||
"version": "1.2.0", | ||
"main": "lib/index.js", | ||
@@ -30,3 +30,3 @@ "module": "es/index.js", | ||
}, | ||
"gitHead": "fdb7772c900a48db5c347967f0ffd0ddd8c88451" | ||
"gitHead": "f8b1efedfe36a620b3ea17dc59c6168cf2a2ef13" | ||
} |
33199
12.81%371
24.5%