Skip to content

Instantly share code, notes, and snippets.

@kennyrowe
Created July 16, 2020 15:58
Show Gist options
  • Save kennyrowe/15cfd26089840eb1c2b4cfcbe2ee414c to your computer and use it in GitHub Desktop.
Save kennyrowe/15cfd26089840eb1c2b4cfcbe2ee414c to your computer and use it in GitHub Desktop.
function run(chat){
function format(object){
items = [];
for (let each of object){
time = new Date(each.when);
let obj = {
when: each.when,
date: time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate() ,
author: each.author,
uid: each.uid
};
items.push(obj)
};
return items;
}
function convertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ',';
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}
function process(array){
headers = {time: 'time', date: 'date', author : 'author', uid: 'uid'};
array.unshift(headers);
var jsonObject = JSON.stringify(items);
csv = convertToCSV(jsonObject);
console.log(csv);
}
return process(format(chat));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment