Skip to content

Commit

Permalink
Escape double quotes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
asafdav committed Mar 5, 2014
1 parent 28656c8 commit fbd9d58
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
19 changes: 15 additions & 4 deletions build/ng-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ angular.module('ngCsv.directives', []).
'$attrs',
'$transclude',
function ($scope, $element, $attrs, $transclude) {
var stringifyCell = function(data) {
if (typeof data === 'string') {
data = data.replace(/"/g, '""'); // Escape double qoutes
if ($scope.txtDelim) data = $scope.txtDelim + data + $scope.txtDelim;
return data;
}

if (typeof data === 'boolean') {
return data ? 'TRUE' : 'FALSE';
}

return data;
};

$scope.csv = '';

$scope.$watch(function (newValue) {
Expand All @@ -77,10 +91,7 @@ angular.module('ngCsv.directives', []).
angular.forEach(data, function (row, index) {
var infoArray = [];
angular.forEach(row, function (field) {
if (typeof field === 'string' && $scope.txtDelim) {
field = $scope.txtDelim + field + $scope.txtDelim;
}
this.push(field);
this.push(stringifyCell(field));
}, infoArray);
dataString = infoArray.join($scope.fieldSep || ',');
csvContent += index < data.length ? dataString + '\r\n' : dataString;
Expand Down
4 changes: 2 additions & 2 deletions build/ng-csv.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions src/ng-csv/directives/ng-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ angular.module('ngCsv.directives', []).
'$attrs',
'$transclude',
function ($scope, $element, $attrs, $transclude) {
var stringifyCell = function(data) {
if (typeof data === 'string') {
data = data.replace(/"/g, '""'); // Escape double qoutes
if ($scope.txtDelim) data = $scope.txtDelim + data + $scope.txtDelim;
return data;
}

if (typeof data === 'boolean') {
return data ? 'TRUE' : 'FALSE';
}

return data;
};

$scope.csv = '';

$scope.$watch(function (newValue) {
Expand All @@ -50,10 +64,7 @@ angular.module('ngCsv.directives', []).
angular.forEach(data, function (row, index) {
var infoArray = [];
angular.forEach(row, function (field) {
if (typeof field === 'string' && $scope.txtDelim) {
field = $scope.txtDelim + field + $scope.txtDelim;
}
this.push(field);
this.push(stringifyCell(field));
}, infoArray);
dataString = infoArray.join($scope.fieldSep || ',');
csvContent += index < data.length ? dataString + '\r\n' : dataString;
Expand Down

0 comments on commit fbd9d58

Please sign in to comment.