Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special chars escaping improvement #10

Merged
merged 4 commits into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,22 @@ function generateCsv(json, callback) {
else if(_.isObject(json)){
var fileRows = [];
var horizontalRows = [[],[]];
var textDelimiterRegex = new RegExp(options.textDelimiter, 'g');
for(var prop in json){
var parseResult = checkType(json[prop], prop);
parseResult.forEach(function (result) {

var value = result.value || options.undefinedString;
if (value.indexOf(options.rowDelimiter) >= 0) {
// Escape the textDelimiters contained in the field
/*(https://tools.ietf.org/html/rfc4180)
7. If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote.
For example: "aaa","b""bb","ccc"
*/
value = value.replace(textDelimiterRegex, options.textDelimiter+options.textDelimiter);
// Escape the whole field if it contains a rowDelimiter
if (value.indexOf(options.rowDelimiter) >= 0 || value.indexOf('\n') >= 0) {
value = options.textDelimiter + value + options.textDelimiter;
}
//Type header;value
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonexport",
"version": "1.2.1",
"version": "1.2.2",
"description": "Makes easy to convert JSON to CSV",
"main": "./lib",
"scripts": {
Expand Down