Writing a function to read a wide array of CSV datasets. Can parse CSV to arrays-of-arrays-of-strings well enough (see Javascript code to parse CSV data).
Now would like to can each column, and if they're all convertible to numbers, convert them to Number, else leave as String. But if not, leave them as strings.
Of course, numbers can come in many formats, some possibly dependent on locale. I'd like a function like parseFloat that recognizes these:
"2.0" -> 2.0
"2" -> 2.0
"1,532" -> 1532 (in US at least)
"$2.00" -> 2.0
"0.02" -> 0.02
"2%" -> 0.02
"" - null
"NaN" -> NaN
"""NaN""" -> String
"2 years old" -> String
"ABC123" -> String
parseFloat(val)
doesn't quite work for this purpose:
parseFloat("1,523") === 1
parseFloat("2%") === 2 // could live with this
parseFloat("$2.25") === NaN
This obviously too much to expect a native ECMAscript function to handle, but it seems like i shouldn't be inventing regular expressions myself for each of these either.
Might there be a library that does this well? My Google search skills seem to fail here.
Handling common date formats would be even better.