Skip to content
Browse files

Merge branch 'jasondavies-csv_newlines'

  • Loading branch information...
2 parents 4fcff60 + 87a9b00 commit 39a32e17d1023e878ac39976c2a363645e91fe0e @mbostock mbostock committed
Showing with 52 additions and 11 deletions.
  1. +1 −0 Makefile
  2. +9 −3 d3.csv.js
  3. +3 −2 d3.csv.min.js
  4. +1 −1 d3.js
  5. +1 −1 d3.min.js
  6. +1 −1 src/core/core.js
  7. +9 −3 src/csv/parse.js
  8. +18 −0 tests/test-csv-parse.js
  9. +9 −0 tests/test-csv-parse.out
View
1 Makefile
@@ -139,6 +139,7 @@ tests: \
tests/test-append.test \
tests/test-attr.test \
tests/test-call.test \
+ tests/test-csv-parse.test \
tests/test-format.test \
tests/test-time-format.test \
tests/test-time-parse.test \
View
12 d3.csv.js
@@ -21,7 +21,7 @@ d3.csv.parseRows = function(text, f) {
var EOL = {}, // sentinel value for end-of-line
EOF = {}, // sentinel value for end-of-file
rows = [], // output rows
- re = /[,\n]/g, // field separator regex
+ re = /\r\n|[,\r\n]/g, // field separator regex
n = 0, // the current line number
t, // the current token
eol; // is the current token followed by EOL?
@@ -41,15 +41,21 @@ d3.csv.parseRows = function(text, f) {
i++;
}
}
- if (text.charCodeAt(i + 1) == 10) eol = true;
re.lastIndex = i + 2;
+ var c = text.charCodeAt(i + 1);
+ if (c == 13) {
+ eol = true;
+ if (text.charCodeAt(i + 2) == 10) re.lastIndex++;
+ } else if (c == 10) {
+ eol = true;
+ }
return text.substring(j + 1, i).replace(/""/g, "\"");
}
// common case
var m = re.exec(text);
if (m) {
- if (m[0] == "\n") eol = true;
+ eol = m[0].charCodeAt(0) != 44;
return text.substring(j, m.index);
}
re.lastIndex = text.length;
View
5 d3.csv.min.js
@@ -1,2 +1,3 @@
-(function(){function m(a){return a.map(n).join(",")}function n(a){return/[",\n]/.test(a)?'"'+a.replace(/\"/g,'""')+'"':a}d3.csv=function(a,c){d3.text(a,"text/csv",function(d){c(d&&d3.csv.parse(d))})};d3.csv.parse=function(a){var c;return d3.csv.parseRows(a,function(d,j){if(j){for(var f={},g=-1,e=c.length;++g<e;)f[c[g]]=d[g];return f}else{c=d;return null}})};d3.csv.parseRows=function(a,c){function d(){if(e.lastIndex==a.length)return f;if(k){k=false;return j}var h=e.lastIndex;if(a.charCodeAt(h)==34){for(var b=
-h;b++<a.length;)if(a.charCodeAt(b)==34){if(a.charCodeAt(b+1)!=34)break;b++}if(a.charCodeAt(b+1)==10)k=true;e.lastIndex=b+2;return a.substring(h+1,b).replace(/""/g,'"')}if(b=e.exec(a)){if(b[0]=="\n")k=true;return a.substring(h,b.index)}e.lastIndex=a.length;return a.substring(h)}for(var j={},f={},g=[],e=/[,\n]/g,o=0,i,k;(i=d())!==f;){for(var l=[];i!==j&&i!==f;){l.push(i);i=d()}c&&!(l=c(l,o++))||g.push(l)}return g};d3.csv.format=function(a){return a.map(m).join("\n")}})();
+(function(){function n(a){return a.map(o).join(",")}function o(a){return/[",\n]/.test(a)?'"'+a.replace(/\"/g,'""')+'"':a}d3.csv=function(a,c){d3.text(a,"text/csv",function(d){c(d&&d3.csv.parse(d))})};d3.csv.parse=function(a){var c;return d3.csv.parseRows(a,function(d,k){if(k){for(var f={},g=-1,e=c.length;++g<e;)f[c[g]]=d[g];return f}else{c=d;return null}})};d3.csv.parseRows=function(a,c){function d(){if(e.lastIndex==a.length)return f;if(h){h=false;return k}var i=e.lastIndex;if(a.charCodeAt(i)==34){for(var b=
+i;b++<a.length;)if(a.charCodeAt(b)==34){if(a.charCodeAt(b+1)!=34)break;b++}e.lastIndex=b+2;var m=a.charCodeAt(b+1);if(m==13){h=true;a.charCodeAt(b+2)==10&&e.lastIndex++}else if(m==10)h=true;return a.substring(i+1,b).replace(/""/g,'"')}if(b=e.exec(a)){h=b[0].charCodeAt(0)!=44;return a.substring(i,b.index)}e.lastIndex=a.length;return a.substring(i)}for(var k={},f={},g=[],e=/\r\n|[,\r\n]/g,p=0,j,h;(j=d())!==f;){for(var l=[];j!==k&&j!==f;){l.push(j);j=d()}c&&!(l=c(l,p++))||g.push(l)}return g};d3.csv.format=
+function(a){return a.map(n).join("\n")}})();
View
2 d3.js
@@ -1,4 +1,4 @@
-(function(){d3 = {version: "1.8.2"}; // semver
+(function(){d3 = {version: "1.8.3"}; // semver
if (!Date.now) Date.now = function() {
return +new Date();
};
View
2 d3.min.js
@@ -25,7 +25,7 @@ a)})}function ka(a,b){var c=Date.now(),g=false,e=c+b,d=F;if(isFinite(b)){for(;d;
Math.LN10}function oa(a){return-Math.log(-a)/Math.LN10}function Oa(a){return function(b){return Math.pow(b,a)}}function Pa(a){return function(b){return-Math.pow(-b,a)}}function Qa(a){return a.innerRadius}function Ra(a){return a.outerRadius}function pa(a){return a.startAngle}function qa(a){return a.endAngle}function aa(a,b,c,g){var e=[],d=-1,f=b.length,h=typeof c=="function",i=typeof g=="function",k;if(h&&i)for(;++d<f;)e.push([c.call(a,k=b[d],d),g.call(a,k,d)]);else if(h)for(;++d<f;)e.push([c.call(a,
b[d],d),g]);else if(i)for(;++d<f;)e.push([c,g.call(a,b[d],d)]);else for(;++d<f;)e.push([c,g]);return e}function ra(a){return a[0]}function sa(a){return a[1]}function H(a){var b=[],c=0,g=a.length,e=a[0];for(b.push(e[0],",",e[1]);++c<g;)b.push("L",(e=a[c])[0],",",e[1]);return b.join("")}function ta(a,b){if(b.length<1||a.length!=b.length&&a.length!=b.length+2)return H(a);var c=a.length!=b.length,g="",e=a[0],d=a[1],f=b[0],h=f,i=1;if(c){g+="Q"+(d[0]-f[0]*2/3)+","+(d[1]-f[1]*2/3)+","+d[0]+","+d[1];e=a[1];
i=2}if(b.length>1){h=b[1];d=a[i];i++;g+="C"+(e[0]+f[0])+","+(e[1]+f[1])+","+(d[0]-h[0])+","+(d[1]-h[1])+","+d[0]+","+d[1];for(e=2;e<b.length;e++,i++){d=a[i];h=b[e];g+="S"+(d[0]-h[0])+","+(d[1]-h[1])+","+d[0]+","+d[1]}}if(c){c=a[i];g+="Q"+(d[0]+h[0]*2/3)+","+(d[1]+h[1]*2/3)+","+c[0]+","+c[1]}return g}function ua(a,b){for(var c=[],g=(1-b)/2,e=a[0],d=a[1],f=a[2],h=2,i=a.length;++h<i;){c.push([g*(f[0]-e[0]),g*(f[1]-e[1])]);e=d;d=f;f=a[h]}c.push([g*(f[0]-e[0]),g*(f[1]-e[1])]);return c}function C(a,b){return a[0]*
-b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3]}function L(a,b,c){a.push("C",C(va,b),",",C(va,c),",",C(wa,b),",",C(wa,c),",",C(M,b),",",C(M,c))}function Sa(){return 0}function Ta(a){return a.source}function Ua(a){return a.target}function Va(a){return a.radius}function Wa(){return 64}function Xa(){return"circle"}d3={version:"1.8.2"};if(!Date.now)Date.now=function(){return+new Date};if(!Object.create)Object.create=function(a){function b(){}b.prototype=a;return new b};var N=function(a){return Array.prototype.slice.call(a)};
+b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3]}function L(a,b,c){a.push("C",C(va,b),",",C(va,c),",",C(wa,b),",",C(wa,c),",",C(M,b),",",C(M,c))}function Sa(){return 0}function Ta(a){return a.source}function Ua(a){return a.target}function Va(a){return a.radius}function Wa(){return 64}function Xa(){return"circle"}d3={version:"1.8.3"};if(!Date.now)Date.now=function(){return+new Date};if(!Object.create)Object.create=function(a){function b(){}b.prototype=a;return new b};var N=function(a){return Array.prototype.slice.call(a)};
try{N(document.documentElement.childNodes)}catch(jb){N=za}d3.rebind=function(a,b){return function(){var c=b.apply(a,arguments);return arguments.length?a:c}};d3.ascending=function(a,b){return a<b?-1:a>b?1:0};d3.descending=function(a,b){return b<a?-1:b>a?1:0};d3.min=function(a,b){var c=0,g=a.length,e=a[0],d;if(arguments.length==1)for(;++c<g;){if(e>(d=a[c]))e=d}else for(e=b(a[0]);++c<g;)if(e>(d=b(a[c])))e=d;return e};d3.max=function(a,b){var c=0,g=a.length,e=a[0],d;if(arguments.length==1)for(;++c<g;){if(e<
(d=a[c]))e=d}else for(e=b(e);++c<g;)if(e<(d=b(a[c])))e=d;return e};d3.nest=function(){function a(h,i){if(i>=g.length)return f?f.call(c,h):d?h.sort(d):h;for(var k=-1,j=h.length,p=g[i++],q,m,o={};++k<j;)if((q=p(m=h[k]))in o)o[q].push(m);else o[q]=[m];for(q in o)o[q]=a(o[q],i);return o}function b(h,i){if(i>=g.length)return h;var k=[],j=e[i++],p;for(p in h)k.push({key:p,values:b(h[p],i)});j&&k.sort(function(q,m){return j(q.key,m.key)});return k}var c={},g=[],e=[],d,f;c.map=function(h){return a(h,0)};
c.entries=function(h){return b(a(h,0),0)};c.key=function(h){g.push(h);return c};c.sortKeys=function(h){e[g.length-1]=h;return c};c.sortValues=function(h){d=h;return c};c.rollup=function(h){f=h;return c};return c};d3.keys=function(a){var b=[],c;for(c in a)b.push(c);return b};d3.values=function(a){var b=[],c;for(c in a)b.push(a[c]);return b};d3.entries=function(a){var b=[],c;for(c in a)b.push({key:c,value:a[c]});return b};d3.merge=function(a){return Array.prototype.concat.apply([],a)};d3.split=function(a,
View
2 src/core/core.js
@@ -1 +1 @@
-d3 = {version: "1.8.2"}; // semver
+d3 = {version: "1.8.3"}; // semver
View
12 src/csv/parse.js
@@ -16,7 +16,7 @@ d3.csv.parseRows = function(text, f) {
var EOL = {}, // sentinel value for end-of-line
EOF = {}, // sentinel value for end-of-file
rows = [], // output rows
- re = /[,\n]/g, // field separator regex
+ re = /\r\n|[,\r\n]/g, // field separator regex
n = 0, // the current line number
t, // the current token
eol; // is the current token followed by EOL?
@@ -36,15 +36,21 @@ d3.csv.parseRows = function(text, f) {
i++;
}
}
- if (text.charCodeAt(i + 1) == 10) eol = true;
re.lastIndex = i + 2;
+ var c = text.charCodeAt(i + 1);
+ if (c == 13) {
+ eol = true;
+ if (text.charCodeAt(i + 2) == 10) re.lastIndex++;
+ } else if (c == 10) {
+ eol = true;
+ }
return text.substring(j + 1, i).replace(/""/g, "\"");
}
// common case
var m = re.exec(text);
if (m) {
- if (m[0] == "\n") eol = true;
+ eol = m[0].charCodeAt(0) != 44;
return text.substring(j, m.index);
}
re.lastIndex = text.length;
View
18 tests/test-csv-parse.js
@@ -0,0 +1,18 @@
+require("./../lib/env-js/envjs/node");
+require("./../d3");
+require("./../d3.csv");
+
+var newlines = {
+ UNIX: "\n",
+ DOS: "\r\n",
+ Mac: "\r"
+};
+
+for (os in newlines) {
+ console.log("parse " + os + " newlines:");
+ var rows = d3.csv.parse(["a,b,c", "1,2,3", "4,5,\"6\"", "7,8,9"].join(newlines[os]));
+ console.log(" " + rows.map(function(row) {
+ return row.a + ":" + row.b + ":" + row.c;
+ }).join("|"));
+ console.log("");
+}
View
9 tests/test-csv-parse.out
@@ -0,0 +1,9 @@
+parse UNIX newlines:
+ 1:2:3|4:5:6|7:8:9
+
+parse DOS newlines:
+ 1:2:3|4:5:6|7:8:9
+
+parse Mac newlines:
+ 1:2:3|4:5:6|7:8:9
+

0 comments on commit 39a32e1

Please sign in to comment.
Something went wrong with that request. Please try again.