Created
October 5, 2020 06:54
-
-
Save olanipekunife/29f5472b42612910da0204293b992d93 to your computer and use it in GitHub Desktop.
convert csv string to array
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
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