Browse Source

Significant cleanup and fix for empty first field; closes #4.

pull/8/head 0.5.7
Matthew Holt 11 years ago
parent
commit
c47809e920
  1. 63
      jquery.parse.js

63
jquery.parse.js

@ -49,6 +49,10 @@
var _input = input; var _input = input;
var _config = config; var _config = config;
var _errors = []; var _errors = [];
var _regex = {
floats: /^-?\d+(\.\d+)?$/,
empty: /^\s*$/
}
var _state = emptyState(); var _state = emptyState();
this.parse = function(arg) this.parse = function(arg)
@ -64,6 +68,7 @@
for (_state.i = 0; _state.i < _input.length; _state.i++) for (_state.i = 0; _state.i < _input.length; _state.i++)
{ {
_state.ch = _input[_state.i]; _state.ch = _input[_state.i];
_state.line += _state.ch;
if (_state.ch == '"') if (_state.ch == '"')
handleQuote(); handleQuote();
@ -73,10 +78,8 @@
notInQuotes(); notInQuotes();
} }
// Treat the last line and its last field // End of input is also end of the last row
saveField(); endRow();
trimEmptyLastLine();
inspectFieldCount();
if (_state.inQuotes) if (_state.inQuotes)
addError("Unescaped or mismatched quotes"); addError("Unescaped or mismatched quotes");
@ -141,9 +144,10 @@
{ {
return { return {
i: 0, i: 0,
line: 1, lineNum: 1,
field: 0, field: 0,
fieldVal: "", fieldVal: "",
line: "",
ch: "", ch: "",
inQuotes: false, inQuotes: false,
parsed: emptyParsed(config.header) parsed: emptyParsed(config.header)
@ -192,7 +196,6 @@
} }
else if (_state.ch == "\n") else if (_state.ch == "\n")
{ {
saveField();
newRow(); newRow();
} }
else else
@ -210,7 +213,7 @@
{ {
if (_config.header) if (_config.header)
{ {
if (_state.line == 1) if (_state.lineNum == 1)
{ {
_state.parsed.fields.push(_state.fieldVal) _state.parsed.fields.push(_state.fieldVal)
} }
@ -244,55 +247,54 @@
_state.field ++; _state.field ++;
} }
function newRow() function endRow()
{ {
trimEmptyLastLine(); saveField();
var emptyLine = trimEmptyLine();
if (!emptyLine && _config.header)
inspectFieldCount();
}
if (_config.header) function newRow()
{ {
inspectFieldCount(); endRow();
if (_state.line > 0)
if (_config.header && _state.lineNum > 0)
_state.parsed.rows.push({}); _state.parsed.rows.push({});
}
else else
_state.parsed.push([]); _state.parsed.push([]);
_state.line ++; _state.lineNum ++;
_state.line = "";
_state.field = 0; _state.field = 0;
} }
function tryParseFloat(num) function tryParseFloat(num)
{ {
var isNumber = /^-?\d+(\.\d+)?$/.test(num); var isNumber = _regex.floats.test(num);
return isNumber ? parseFloat(num) : num; return isNumber ? parseFloat(num) : num;
} }
function trimEmptyLastLine() function trimEmptyLine()
{ {
if (_config.header) if (_regex.empty.test(_state.line))
{ {
if (_state.line == 1) if (_config.header)
{ {
if (_state.parsed.fields.length == 1 if (_state.lineNum == 1)
&& _state.parsed.fields[0].length == 0)
{ {
_state.parsed.fields = []; _state.parsed.fields = [];
_state.line --; _state.lineNum --;
}
} }
else else
{
var lastRow = _state.parsed.rows[_state.parsed.rows.length - 1];
if (!lastRow[_state.parsed.fields[0]])
_state.parsed.rows.splice(_state.parsed.rows.length - 1, 1); _state.parsed.rows.splice(_state.parsed.rows.length - 1, 1);
} }
}
else else
{
var lastRow = _state.parsed[_state.parsed.length - 1];
if (lastRow.length == 0 || (lastRow[0].length == 0))
_state.parsed.splice(_state.parsed.length - 1, 1); _state.parsed.splice(_state.parsed.length - 1, 1);
return true;
} }
return false;
} }
function inspectFieldCount() function inspectFieldCount()
@ -322,11 +324,10 @@
{ {
_errors.push({ _errors.push({
message: msg, message: msg,
line: _state.line, line: _state.lineNum,
row: _config.header ? _state.parsed.rows.length - 1 : _state.parsed.length - 1, row: _config.header ? _state.parsed.rows.length - 1 : _state.parsed.length - 1,
index: _state.i index: _state.i
}); });
return false; return false;
} }

Loading…
Cancel
Save