Browse Source

Support for comments in CSV input. Configurable by setting config.commentChar to either a string, true (in which case it defaults to using "#") or false.

pull/51/head
Rune Juhl Jacobsen 11 years ago
parent
commit
95d4fda830
  1. 30
      jquery.parse.js

30
jquery.parse.js

@ -219,7 +219,8 @@
delimiter: "", delimiter: "",
header: true, header: true,
dynamicTyping: true, dynamicTyping: true,
preview: 0 preview: 0,
commentChar: false
}; };
var _regex = { var _regex = {
floats: /^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i, floats: /^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i,
@ -232,7 +233,8 @@
header: config.header, header: config.header,
dynamicTyping: config.dynamicTyping, dynamicTyping: config.dynamicTyping,
preview: config.preview, preview: config.preview,
step: config.step step: config.step,
commentChar: config.commentChar
}; };
this.parse = function(input) this.parse = function(input)
@ -254,6 +256,24 @@
break; break;
_state.ch = _input[_state.i]; _state.ch = _input[_state.i];
if (_config.commentChar) {
// Check if line begins with a commentChar
if (_state.line == "" &&_state.ch == _config.commentChar) {
newRow();
// skip to next row
while (true) {
++_state.i
if (_input[_state.i] == "\r" || _input[_state.i] == "\n")
break;
}
}
_state.ch = _input[_state.i];
}
_state.line += _state.ch; _state.line += _state.ch;
if (_state.ch == '"') if (_state.ch == '"')
@ -311,6 +331,12 @@
if (typeof config.step !== 'function') if (typeof config.step !== 'function')
config.step = _defaultConfig.step; config.step = _defaultConfig.step;
if (config.commentChar === true)
config.commentChar = '#';
if (typeof config.commentChar !== 'string' && config.commentChar !== false)
config.commentChar = false;
return config; return config;
} }

Loading…
Cancel
Save