Created
January 17, 2023 19:31
-
-
Save roryc89/d5c4fd34dec140e9fa4d7b17fdd6a485 to your computer and use it in GitHub Desktop.
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
function flattenResult (target, opts) { | |
opts = opts || {} | |
var delimiter = opts.delimiter || '.' | |
var maxDepth = opts.maxDepth | |
var output = {} | |
function step (object, prev, currentDepth) { | |
currentDepth = currentDepth || 1 | |
Object.keys(object).forEach(function (key) { | |
var value = object[key] | |
var isarray = opts.safe && Array.isArray(value) | |
var type = Object.prototype.toString.call(value) | |
var isobject = ( | |
type === '[object Object]' || | |
type === '[object Array]' | |
) | |
var newKey = prev | |
? prev + delimiter + key | |
: key | |
if (!isarray && isobject && Object.keys(value).length && | |
(!opts.maxDepth || currentDepth < maxDepth)) { | |
return step(value, newKey, currentDepth + 1) | |
} | |
output[newKey] = value | |
}) | |
} | |
step(target) | |
return output | |
} | |
function exportToCsv(filename, rows) { | |
if(!rows.length){ | |
console.log('no data') | |
return; | |
} | |
const keys = Object.keys(rows[0]); | |
var processRow = function (row) { | |
var finalVal = ''; | |
for (var j = 0; j < keys.length; j++) { | |
const key = keys[j]; | |
var innerValue = (row[key] === null || row[key] === undefined) ? '' : row[key].toString(); | |
if (row[key] instanceof Date) { | |
innerValue = row[key].toLocaleString(); | |
}; | |
var result = innerValue.replace(/"/g, '""'); | |
if (result.search(/("|,|\n)/g) >= 0) | |
result = '"' + result + '"'; | |
if (j > 0) | |
finalVal += ','; | |
finalVal += result; | |
} | |
return finalVal + '\n'; | |
}; | |
var csvFile = ''; | |
const headers = {} | |
keys.forEach(key => headers[key] = key) | |
csvFile += processRow(headers) | |
for (var i = 0; i < rows.length; i++) { | |
csvFile += processRow(rows[i]); | |
} | |
var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' }); | |
if (navigator.msSaveBlob) { // IE 10+ | |
navigator.msSaveBlob(blob, filename); | |
} else { | |
var link = document.createElement("a"); | |
if (link.download !== undefined) { // feature detection | |
// Browsers that support HTML5 download attribute | |
var url = URL.createObjectURL(blob); | |
link.setAttribute("href", url); | |
link.setAttribute("download", filename); | |
link.style.visibility = 'hidden'; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} | |
} | |
} | |
function saveResult (name, key){ | |
name = name || document.querySelector('.cm-def').textContent; | |
var result = JSON.parse(document.querySelector('.result-window .CodeMirror').CodeMirror.getDoc().getValue()).data; | |
key = key || Object.keys(result)[0]; | |
var flattenedResult = result[key].map(flattenResult); | |
exportToCsv(name, flattenedResult) | |
} | |
saveResult() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment