Skip to content

Commit

Permalink
Data module: fixed start/end + delimiter deduction. See #7272
Browse files Browse the repository at this point in the history
  • Loading branch information
cvasseng committed Oct 17, 2017
1 parent 8804e75 commit 3f86649
Showing 1 changed file with 72 additions and 46 deletions.
118 changes: 72 additions & 46 deletions js/modules/data.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ Highcharts.extend(Data.prototype, {
*/
parseCSV: function (inOptions) {
var self = this,
options = this.options || inOptions,
options = inOptions || this.options,
csv = options.csv,
columns,
startRow = options.startRow || 0,
Expand All @@ -502,7 +502,7 @@ Highcharts.extend(Data.prototype, {
'\t': 0
};

columns = this.columns = this.columns || [];
columns = this.columns = [];

/*
This implementation is quite verbose. It will be shortened once
Expand Down Expand Up @@ -571,6 +571,7 @@ Highcharts.extend(Data.prototype, {
if (startColumn > actualColumn || actualColumn > endColumn) {
// Skip this column, but increment the column count (#7272)
++actualColumn;
token = '';
return;
}

Expand All @@ -584,12 +585,15 @@ Highcharts.extend(Data.prototype, {
pushType('string');
}


if (columns.length < column + 1) {
columns.push([]);
}

if (!noAdd) {
columns[column].push(token);
// Don't push - if there's a varrying amount of columns
// for each row, pushing will skew everything down n slots
columns[column][rowNumber] = token;
}

token = '';
Expand Down Expand Up @@ -645,66 +649,88 @@ Highcharts.extend(Data.prototype, {

push();

if (column < columns.length) {
// There might be an issue.
// This set is either

// Fill in
if (!noAdd) {
for (var z = column; z < columns.length; z++) {
columns[z].push(0);
}
}
}
}

// Attempt to guess the delimiter
// We do a separate parse pass here because we need
// to count potential delimiters softly without making any assumptions.
function guessDelimiter(lines) {
var points = 0,
commas = 0,
guessed = false,
handler = function (c, token) {
guessed = false;

some(lines, function (columnStr, i) {
var inStr = false,
c,
cn,
cl,
token = ''
;


// We should be able to detect dateformats within 13 rows
if (i > 13) {
return true;
}

for (var j = 0; j < columnStr.length; j++) {
c = columnStr[j];
cn = columnStr[j + 1];
cl = columnStr[j - 1];

if (c === '#') {
// Skip the rest of the line - it's a comment
return;
} else if (c === '"') {
if (inStr) {
if (cl !== '"' && cn !== '"') {
while (cn === ' ' && j < columnStr.length) {
cn = columnStr[++j];
}

// After parsing a string, the next non-blank
// should be a delimiter if the CSV is properly
// formed.

if (typeof potDelimiters[cn] !== 'undefined') {
potDelimiters[cn]++;
}

inStr = false;
}
} else {
inStr = true;
}
} else if (typeof potDelimiters[c] !== 'undefined') {

token = token.trim();

if (!isNaN(Date.parse(token))) {
potDelimiters[c]++;
} else if (isNaN(token) || !isFinite(token)) {
potDelimiters[c]++;
}

token = '';

} else {
token += c;
}

if (c === ',') {
commas++;
}

if (c === '.') {
points++;
}

if (typeof potDelimiters[c] !== 'undefined') {
// Check what we have in token now

if (
// We can't make a deduction when token is a number,
// since the decimal delimiter may interfere.
(isNaN(parseFloat(token)) || !isFinite(token)) &&
(
Highcharts.isString(token) ||
!isNaN(Date.parse(token))
)
) {
potDelimiters[c]++;
return true;
}
}
},
callbacks = {
';': handler,
',': handler,
'\t': handler
};

some(lines, function (columnStr, i) {
// We should be able to detect dateformats within 13 rows
if (i > 13) {
return true;
}
parseRow(columnStr, i, true, callbacks);
});

// Count the potential delimiters.
// This could be improved by checking if the number of delimiters
// equals the number of columns - 1

if (potDelimiters[';'] > potDelimiters[',']) {
guessed = ';';
} else if (potDelimiters[','] > potDelimiters[';']) {
Expand Down Expand Up @@ -870,7 +896,7 @@ Highcharts.extend(Data.prototype, {
}

for (rowIt = startRow; rowIt <= endRow; rowIt++) {
parseRow(lines[rowIt], rowIt);
parseRow(lines[rowIt], rowIt - startRow);
}

// //Make sure that there's header columns for everything
Expand Down

0 comments on commit 3f86649

Please sign in to comment.