Skip to content

Instantly share code, notes, and snippets.

@olanipekunife
Created October 5, 2020 06:54
Show Gist options
  • Save olanipekunife/29f5472b42612910da0204293b992d93 to your computer and use it in GitHub Desktop.
Save olanipekunife/29f5472b42612910da0204293b992d93 to your computer and use it in GitHub Desktop.
convert csv string to array
const detectNewline = string => {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
const newlines = string.match(/(?:\r?\n)/g) || [];
if (newlines.length === 0) {
return;
}
const crlf = newlines.filter(newline => newline === '\r\n').length;
const lf = newlines.length - crlf;
return crlf > lf ? '\r\n' : '\n';
};
const result = await request(options)
let ress = result.split(detectNewline(result))
//remove header
ress.shift()
let newCsvARR = []
ress.map(row=>{
result = row
if(row){
result = result.replace(/"[^"]+"/g, function (match) {
return match.replace(/,/g, '');
});
result = result.replace(/"/g, "")
}
newCsvARR.push(result)
//teaching you hoisting in javasript, :) use only in vars
var result
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment