Last active
March 22, 2016 21:03
-
-
Save GingerBear/f29b467a026780a6e469 to your computer and use it in GitHub Desktop.
csv stringify sync
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
// from https://github.com/wdavidw/node-csv-stringify/blob/de22cdd8ffd07a02bd916e96a31678581eb33659/lib/index.js#L209 | |
function stringify(line) { | |
var _line, column, columns, containsEscape, containsLinebreak, containsQuote, containsdelimiter, delimiter, escape, field, i, j, l, newLine, quote, ref, ref1, regexp, shouldQuote, value; | |
if (typeof line !== 'object') { | |
return line; | |
} | |
columns = null; | |
if (typeof columns === 'object' && columns !== null && !Array.isArray(columns)) { | |
columns = Object.keys(columns); | |
} | |
delimiter = ','; | |
quote = '"'; | |
escape = '"'; | |
if (!Array.isArray(line)) { | |
_line = []; | |
if (columns) { | |
for (i = j = 0, ref = columns.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) { | |
column = columns[i]; | |
value = get(line, column); | |
_line[i] = typeof value === 'undefined' || value === null ? '' : value; | |
} | |
} else { | |
for (column in line) { | |
_line.push(line[column]); | |
} | |
} | |
line = _line; | |
_line = null; | |
} else if (columns) { | |
line.splice(columns.length); | |
} | |
if (Array.isArray(line)) { | |
newLine = ''; | |
for (i = l = 0, ref1 = line.length; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) { | |
field = line[i]; | |
if (typeof field === 'string') { | |
} else if (typeof field === 'number') { | |
field = '' + field; | |
} else if (typeof field === 'boolean') { | |
field = field ? '1' : ''; | |
} else if (field instanceof Date) { | |
field = '' + field.getTime(); | |
} else if (typeof field === 'object' && field !== null) { | |
field = JSON.stringify(field); | |
} | |
if (field) { | |
containsdelimiter = field.indexOf(delimiter) >= 0; | |
containsQuote = field.indexOf(quote) >= 0; | |
containsEscape = field.indexOf(escape) >= 0 && (escape !== quote); | |
containsLinebreak = field.indexOf('\r') >= 0 || field.indexOf('\n') >= 0; | |
shouldQuote = containsQuote || containsdelimiter || containsLinebreak || false || (false && typeof line[i] === 'string'); | |
if (shouldQuote && containsEscape) { | |
regexp = escape === '\\' ? new RegExp(escape + escape, 'g') : new RegExp(escape, 'g'); | |
field = field.replace(regexp, escape + escape); | |
} | |
if (containsQuote) { | |
regexp = new RegExp(quote, 'g'); | |
field = field.replace(regexp, escape + quote); | |
} | |
if (shouldQuote) { | |
field = quote + field + quote; | |
} | |
newLine += field; | |
} | |
if (i !== line.length - 1) { | |
newLine += delimiter; | |
} | |
} | |
line = newLine; | |
} | |
return line; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment