Skip to content

Commit 7ad465e

Browse files
committed
Merge pull request #51 from runejuhl/master
Support for comments in CSV input.
2 parents d9a0288 + 95d4fda commit 7ad465e

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

jquery.parse.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@
227227
delimiter: "",
228228
header: true,
229229
dynamicTyping: true,
230-
preview: 0
230+
preview: 0,
231+
commentChar: false
231232
};
232233
var _regex = {
233234
floats: /^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i,
@@ -240,7 +241,8 @@
240241
header: config.header,
241242
dynamicTyping: config.dynamicTyping,
242243
preview: config.preview,
243-
step: config.step
244+
step: config.step,
245+
commentChar: config.commentChar
244246
};
245247

246248
this.parse = function(input)
@@ -262,6 +264,24 @@
262264
break;
263265

264266
_state.ch = _input[_state.i];
267+
268+
if (_config.commentChar) {
269+
// Check if line begins with a commentChar
270+
if (_state.line == "" &&_state.ch == _config.commentChar) {
271+
newRow();
272+
273+
// skip to next row
274+
while (true) {
275+
++_state.i
276+
if (_input[_state.i] == "\r" || _input[_state.i] == "\n")
277+
break;
278+
}
279+
}
280+
281+
_state.ch = _input[_state.i];
282+
283+
}
284+
265285
_state.line += _state.ch;
266286

267287
if (_state.ch == '"')
@@ -319,6 +339,12 @@
319339
if (typeof config.step !== 'function')
320340
config.step = _defaultConfig.step;
321341

342+
if (config.commentChar === true)
343+
config.commentChar = '#';
344+
345+
if (typeof config.commentChar !== 'string' && config.commentChar !== false)
346+
config.commentChar = false;
347+
322348
return config;
323349
}
324350

0 commit comments

Comments
 (0)