diff --git a/build/ng-csv.js b/build/ng-csv.js index eccbfea..c1081fc 100644 --- a/build/ng-csv.js +++ b/build/ng-csv.js @@ -39,13 +39,25 @@ angular.module('ngCsv.services'). /** * Stringify one field * @param data - * @param delimier + * @param options * @returns {*} */ - this.stringifyField = function (data, delimier, quoteText) { + this.stringifyField = function (data, options) { + if (options.decimalSep === 'locale' && this.isFloat(data)) { + return data.toLocaleString(); + } + + if (options.decimalSep !== '.' && this.isFloat(data)) { + return data.toString().replace('.', options.decimalSep); + } + if (typeof data === 'string') { data = data.replace(/"/g, '""'); // Escape double qoutes - if (quoteText || data.indexOf(',') > -1 || data.indexOf('\n') > -1 || data.indexOf('\r') > -1) data = delimier + data + delimier; + + if (options.quoteStrings || data.indexOf(',') > -1 || data.indexOf('\n') > -1 || data.indexOf('\r') > -1) { + data = options.txtDelim + data + options.txtDelim; + } + return data; } @@ -56,6 +68,15 @@ angular.module('ngCsv.services'). return data; }; + /** + * Helper function to check if input is float + * @param input + * @returns {boolean} + */ + this.isFloat = function (input) { + return +input === input && (!isFinite(input) || Boolean(input % 1)); + }; + /** * Creates a csv from a data array * @param data @@ -80,7 +101,7 @@ angular.module('ngCsv.services'). encodingArray = []; angular.forEach(options.header, function (title, key) { - this.push(that.stringifyField(title, options.txtDelim, options.quoteStrings)); + this.push(that.stringifyField(title, options)); }, encodingArray); headerString = encodingArray.join(options.fieldSep ? options.fieldSep : ","); @@ -102,7 +123,7 @@ angular.module('ngCsv.services'). infoArray = []; angular.forEach(row, function (field, key) { - this.push(that.stringifyField(field, options.txtDelim, options.quoteStrings)); + this.push(that.stringifyField(field, options)); }, infoArray); dataString = infoArray.join(options.fieldSep ? options.fieldSep : ","); @@ -143,6 +164,7 @@ angular.module('ngCsv.directives'). filename: '@filename', header: '&csvHeader', txtDelim: '@textDelimiter', + decimalSep: '@decimalSeparator', quoteStrings: '@quoteStrings', fieldSep: '@fieldSeparator', lazyLoad: '@lazyLoad', @@ -173,6 +195,7 @@ angular.module('ngCsv.directives'). function getBuildCsvOptions() { var options = { txtDelim: $scope.txtDelim ? $scope.txtDelim : '"', + decimalSep: $scope.decimalSep ? $scope.decimalSep : '.', quoteStrings: $scope.quoteStrings, addByteOrderMarker: $scope.addByteOrderMarker }; diff --git a/build/ng-csv.min.js b/build/ng-csv.min.js index 6bdb019..8ece3db 100644 --- a/build/ng-csv.min.js +++ b/build/ng-csv.min.js @@ -1,2 +1,2 @@ -/*! ng-csv 10-03-2015 */ -!function(a){angular.module("ngCsv.config",[]).value("ngCsv.config",{debug:!0}).config(["$compileProvider",function(a){angular.isDefined(a.urlSanitizationWhitelist)?a.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/):a.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/)}]),angular.module("ngCsv.directives",["ngCsv.services"]),angular.module("ngCsv.services",[]),angular.module("ngCsv",["ngCsv.config","ngCsv.services","ngCsv.directives","ngSanitize"]),angular.module("ngCsv.services").service("CSV",["$q",function(a){var b="\r\n",c="";this.stringifyField=function(a,b,c){return"string"==typeof a?(a=a.replace(/"/g,'""'),(c||a.indexOf(",")>-1||a.indexOf("\n")>-1||a.indexOf("\r")>-1)&&(a=b+a+b),a):"boolean"==typeof a?a?"TRUE":"FALSE":a},this.stringify=function(d,e){var f=a.defer(),g=this,h="",i="",j=a.when(d).then(function(a){if(a=angular.copy(a),angular.isDefined(e.header)&&e.header){var d,j;d=[],angular.forEach(e.header,function(a){this.push(g.stringifyField(a,e.txtDelim,e.quoteStrings))},d),j=d.join(e.fieldSep?e.fieldSep:","),i+=j+b}var k=[];angular.isArray(a)?k=a:angular.isFunction(a)&&(k=a()),angular.forEach(k,function(a,c){var d,f;f=[],angular.forEach(a,function(a){this.push(g.stringifyField(a,e.txtDelim,e.quoteStrings))},f),d=f.join(e.fieldSep?e.fieldSep:","),i+=c");g.attr("href",a.URL.createObjectURL(d)),g.attr("download",b.getFilename()),g.attr("target","_blank"),e.find("body").append(g),f(function(){g[0].click(),g.remove()},null)}}c.bind("click",function(){b.buildCSV().then(function(){d()}),b.$apply()})}}}])}(window,document); \ No newline at end of file +/*! ng-csv 12-03-2015 */ +!function(a){angular.module("ngCsv.config",[]).value("ngCsv.config",{debug:!0}).config(["$compileProvider",function(a){angular.isDefined(a.urlSanitizationWhitelist)?a.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/):a.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/)}]),angular.module("ngCsv.directives",["ngCsv.services"]),angular.module("ngCsv.services",[]),angular.module("ngCsv",["ngCsv.config","ngCsv.services","ngCsv.directives","ngSanitize"]),angular.module("ngCsv.services").service("CSV",["$q",function(a){var b="\r\n",c="";this.stringifyField=function(a,b){return"locale"===b.decimalSep&&this.isFloat(a)?a.toLocaleString():"."!==b.decimalSep&&this.isFloat(a)?a.toString().replace(".",b.decimalSep):"string"==typeof a?(a=a.replace(/"/g,'""'),(b.quoteStrings||a.indexOf(",")>-1||a.indexOf("\n")>-1||a.indexOf("\r")>-1)&&(a=b.txtDelim+a+b.txtDelim),a):"boolean"==typeof a?a?"TRUE":"FALSE":a},this.isFloat=function(a){return+a===a&&(!isFinite(a)||Boolean(a%1))},this.stringify=function(d,e){var f=a.defer(),g=this,h="",i="",j=a.when(d).then(function(a){if(a=angular.copy(a),angular.isDefined(e.header)&&e.header){var d,j;d=[],angular.forEach(e.header,function(a){this.push(g.stringifyField(a,e))},d),j=d.join(e.fieldSep?e.fieldSep:","),i+=j+b}var k=[];angular.isArray(a)?k=a:angular.isFunction(a)&&(k=a()),angular.forEach(k,function(a,c){var d,f;f=[],angular.forEach(a,function(a){this.push(g.stringifyField(a,e))},f),d=f.join(e.fieldSep?e.fieldSep:","),i+=c");g.attr("href",a.URL.createObjectURL(d)),g.attr("download",b.getFilename()),g.attr("target","_blank"),e.find("body").append(g),f(function(){g[0].click(),g.remove()},null)}}c.bind("click",function(){b.buildCSV().then(function(){d()}),b.$apply()})}}}])}(window,document); \ No newline at end of file