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

Only skip first n lines in the first chunk and don't take the first line as header (#1045) #1046

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 37 additions & 28 deletions docs/resources/js/papaparse.js
Expand Up @@ -491,6 +491,16 @@ License: MIT
this.parseChunk = function(chunk, isFakeChunk)
{
// First chunk pre-processing
const skipFirstNLines = parseInt(this._config.skipFirstNLines) || 0;
if (this.isFirstChunk && skipFirstNLines > 0) {
let _newline = this._config.newline;
if (!_newline) {
const quoteChar = this._config.quoteChar || '"';
_newline = this._handle.guessLineEndings(chunk, quoteChar);
}
const splitChunk = chunk.split(_newline);
chunk = [...splitChunk.slice(skipFirstNLines)].join(_newline);
}
if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk))
{
var modifiedChunk = this._config.beforeFirstChunk(chunk);
Expand All @@ -503,7 +513,6 @@ License: MIT
// Rejoin the line we likely just split in two by chunking the file
var aggregate = this._partialLine + chunk;
this._partialLine = '';

var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);

if (this._handle.paused() || this._handle.aborted()) {
Expand Down Expand Up @@ -1048,7 +1057,7 @@ License: MIT
{
var quoteChar = _config.quoteChar || '"';
if (!_config.newline)
_config.newline = guessLineEndings(input, quoteChar);
_config.newline = this.guessLineEndings(input, quoteChar);

_delimiterError = false;
if (!_config.delimiter)
Expand Down Expand Up @@ -1119,6 +1128,32 @@ License: MIT
_input = '';
};

this.guessLineEndings = function(input, quoteChar)
{
input = input.substr(0, 1024 * 1024); // max length 1 MB
// Replace all the text inside quotes
var re = new RegExp(escapeRegExp(quoteChar) + '([^]*?)' + escapeRegExp(quoteChar), 'gm');
input = input.replace(re, '');

var r = input.split('\r');

var n = input.split('\n');

var nAppearsFirst = (n.length > 1 && n[0].length < r[0].length);

if (r.length === 1 || nAppearsFirst)
return '\n';

var numWithN = 0;
for (var i = 0; i < r.length; i++)
{
if (r[i][0] === '\n')
numWithN++;
}

return numWithN >= r.length / 2 ? '\r\n' : '\r';
};

function testEmptyLine(s) {
return _config.skipEmptyLines === 'greedy' ? s.join('').trim() === '' : s.length === 1 && s[0].length === 0;
}
Expand Down Expand Up @@ -1321,32 +1356,6 @@ License: MIT
};
}

function guessLineEndings(input, quoteChar)
{
input = input.substr(0, 1024 * 1024); // max length 1 MB
// Replace all the text inside quotes
var re = new RegExp(escapeRegExp(quoteChar) + '([^]*?)' + escapeRegExp(quoteChar), 'gm');
input = input.replace(re, '');

var r = input.split('\r');

var n = input.split('\n');

var nAppearsFirst = (n.length > 1 && n[0].length < r[0].length);

if (r.length === 1 || nAppearsFirst)
return '\n';

var numWithN = 0;
for (var i = 0; i < r.length; i++)
{
if (r[i][0] === '\n')
numWithN++;
}

return numWithN >= r.length / 2 ? '\r\n' : '\r';
}

function addError(type, code, msg, row)
{
_results.errors.push({
Expand Down
80 changes: 37 additions & 43 deletions papaparse.js
Expand Up @@ -511,6 +511,16 @@ License: MIT
this.parseChunk = function(chunk, isFakeChunk)
{
// First chunk pre-processing
const skipFirstNLines = parseInt(this._config.skipFirstNLines) || 0;
if (this.isFirstChunk && skipFirstNLines > 0) {
let _newline = this._config.newline;
if (!_newline) {
const quoteChar = this._config.quoteChar || '"';
_newline = this._handle.guessLineEndings(chunk, quoteChar);
}
const splitChunk = chunk.split(_newline);
chunk = [...splitChunk.slice(skipFirstNLines)].join(_newline);
}
if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk))
{
var modifiedChunk = this._config.beforeFirstChunk(chunk);
Expand All @@ -522,22 +532,6 @@ License: MIT

// Rejoin the line we likely just split in two by chunking the file
var aggregate = this._partialLine + chunk;
this._pendingSkip = parseInt(this._config.skipFirstNLines) || 0;
this._skipHeader = 0;
if (this._config.header) {
this._skipHeader++;
}
if (this._pendingSkip > 0) {
var splitChunk = aggregate.split('\n');
var currentChunkLength = splitChunk.length;
if (currentChunkLength <= this._pendingSkip) {
aggregate = this._partialLine;
}
else{
aggregate = this._partialLine + [...splitChunk.slice(0, this._skipHeader), ...splitChunk.slice(this._skipHeader + this._pendingSkip)].join('\n');
}
this._pendingSkip -= currentChunkLength;
}
this._partialLine = '';
var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);

Expand Down Expand Up @@ -1093,7 +1087,7 @@ License: MIT
{
var quoteChar = _config.quoteChar || '"';
if (!_config.newline)
_config.newline = guessLineEndings(input, quoteChar);
_config.newline = this.guessLineEndings(input, quoteChar);

_delimiterError = false;
if (!_config.delimiter)
Expand Down Expand Up @@ -1167,6 +1161,32 @@ License: MIT
_input = '';
};

this.guessLineEndings = function(input, quoteChar)
{
input = input.substring(0, 1024 * 1024); // max length 1 MB
// Replace all the text inside quotes
var re = new RegExp(escapeRegExp(quoteChar) + '([^]*?)' + escapeRegExp(quoteChar), 'gm');
input = input.replace(re, '');

var r = input.split('\r');

var n = input.split('\n');

var nAppearsFirst = (n.length > 1 && n[0].length < r[0].length);

if (r.length === 1 || nAppearsFirst)
return '\n';

var numWithN = 0;
for (var i = 0; i < r.length; i++)
{
if (r[i][0] === '\n')
numWithN++;
}

return numWithN >= r.length / 2 ? '\r\n' : '\r';
};

function testEmptyLine(s) {
return _config.skipEmptyLines === 'greedy' ? s.join('').trim() === '' : s.length === 1 && s[0].length === 0;
}
Expand Down Expand Up @@ -1373,32 +1393,6 @@ License: MIT
};
}

function guessLineEndings(input, quoteChar)
{
input = input.substring(0, 1024 * 1024); // max length 1 MB
// Replace all the text inside quotes
var re = new RegExp(escapeRegExp(quoteChar) + '([^]*?)' + escapeRegExp(quoteChar), 'gm');
input = input.replace(re, '');

var r = input.split('\r');

var n = input.split('\n');

var nAppearsFirst = (n.length > 1 && n[0].length < r[0].length);

if (r.length === 1 || nAppearsFirst)
return '\n';

var numWithN = 0;
for (var i = 0; i < r.length; i++)
{
if (r[i][0] === '\n')
numWithN++;
}

return numWithN >= r.length / 2 ? '\r\n' : '\r';
}

function addError(type, code, msg, row)
{
var error = {
Expand Down
15 changes: 12 additions & 3 deletions tests/test-cases.js
Expand Up @@ -1585,11 +1585,11 @@ var PARSE_TESTS = [
}
},
{
description: "Skip First N number of lines , with header and 3 rows",
jkruke marked this conversation as resolved.
Show resolved Hide resolved
input: 'a,b,c,d\n1,2,3,4\n4,5,6,7',
description: "Skip First N number of lines , with header and 2 rows",
input: 'to-be-ignored\na,b,c,d\n1,2,3,4',
config: { header: true, skipFirstNLines: 1 },
expected: {
data: [{a: '4', b: '5', c: '6', d: '7'}],
data: [{a: '1', b: '2', c: '3', d: '4'}],
errors: []
}
},
Expand All @@ -1610,6 +1610,15 @@ var PARSE_TESTS = [
data: [['a','b','c','d'],['1','2','3','4'],['4','5','6','7']],
errors: []
}
},
{
description: "Skip first 2 lines , with custom newline character",
input: 'skip-this\rskip-this\r1,2,3,4',
config: { header: false, skipFirstNLines: 2, newline: '\r' },
expected: {
data: [['1','2','3','4']],
errors: []
}
}
];

Expand Down