jijopaulose / jquery-csv

Automatically exported from code.google.com/p/jquery-csv
MIT License
0 stars 0 forks source link

Hooks relying on a return value of false to skip entries makes parsing values that resolve to false impossible #43

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The onParseEntry callback function provides plenty of scope to parse entries to 
numerical values, Booleans, convert special cases to other values, etc, but 
since it always skips a value when a hook to boolean false, you cannot use it 
to handle values that parse to boolean false. 

Repro example:

var ParseBool = function (value)
{
  if (value == "true")
  {
    return true;
  }
  else if (value == "false")
  {
    return false;
  }
  else
  {
    return value;
  }
}

var contentCSV = "index,isAwesome\n0,true\n,1,false";
var data = $csv.toObjects (contentCSV, {onParseValue : ParseBool});

This will produce a JSON array with two objects of structure { index : 
someIntString, isAwesome : someBool }, one which looks like {index : "0", 
isAwesome : true } and one that looks like {index : "1", isAwesome : 0 }.

It would be better is the library used undefined to decide when to skip a 
value, or simply didn't have a skip-value check when parsing values, as it 
makes less sense to skip parsing specific values than it does to skip entire 
entries, for example. I've commented out the if hook !=== false line at 474 of 
the source (in the endOfValue function of parseEntry) and it gives no 
detrimental effects.

Original issue reported on code.google.com by whitesid...@gmail.com on 11 Aug 2014 at 2:06