Comments (9)
Part of the problem with using CSV as a data type is, there's no way to specify
whether the data should be cast to scalar values or if they should remain in
string form.
There are two clear reasons why I don't want to make auto-casting a default
action of the parser.
First, it's not a good idea to assume that all data needs to be cast to scalar
values. To maximize performance, it's best to avoid any unnecessary processing
by default. For instance, in cases where all of the data is strings then taking
the time to type-check every single value is just a waste.
Second, I want to keep the API as slim and predictable as possible. Doing too
much magic under the covers by default may introduce unwanted side-effects (ex
casting automatically where the user wants the value as a string). When that
happens the abstraction begins to leak and the user is pained with figuring out
a way to disable the internal implementation.
I would like to avoid leaky abstractions and side-effects as much as physically
possible while providing a stable and performant parser to the users.
To strike a healthy balance I want to leave this functionality out by default
but make it possible to add it in through a different approach. Basically, by
adding a hook (ie event callback) within the parser stage (ie just before
output.push) the user will be able to insert an arbitrary function to enable
further processing.
In your case you'd want to add auto-casting. In other cases the user may want
to provide a string formatting function that outputs float values as strings
with only two digits of precision with a dollar sign and parentheses around
negative values (ie as is done in accounting). I could probably think of
numerous use cases but the point is, it's more useful to empower the users to
extend the parsing capabilities than to try and strike the perfect balance of
functionality among a large diverse user base.
Fortunately, because javascript is a 'functional' language, the default
function can be easily overridden by simply assigning a new function value to
the hook parameter.
As soon as I have a working implementation I will incorporate your code to add
auto-casting as the first use case...
If you have any more feedback, I'm all ears. If not, a working implementation
will be made available as soon as I have it ready.
Thanks,
Evan
Original comment by [email protected]
on 1 Oct 2012 at 9:54
from jquery-csv.
Thanks. The hook is better as you said, leaving the parser ... be just the
parser :)
Original comment by [email protected]
on 2 Oct 2012 at 5:55
from jquery-csv.
OK, it's done. Released with 0.64.
I'm on a roll.
The feature isn't documented yet but there's a new parameter called
onParseValue.
You set it with a function-as-a-value that works as a callback. It takes one
parameter called 'value' the parser sets that you can manipulate. I also
created $.csv.hooks to contain convenient hook functions which includes one
called castToScalar (loosely based on your code.
Here's an example of how you can call it:
var output = $.csv2Array(testHooks, {
onParseValue: $.csv.hooks.castToScalar
});
Here's the code for castToScalar:
$.csv.hooks.castToScalar: function(value) {
var isNumber = /^[\d\.]+$/;
var hasDot = /\./;
if (value.length) {
if (isNaN(value)) {
return value;
} else if (isNumber.test(value)){
if (hasDot.test(value)) {
return parseFloat(value);
} else {
return parseInt(value);
}
} else {
return undefined;
}
}
}
I replaced your not_a_number function with the one built into JavaScript, and
changed the naming conventions but it should function the same way.
There's also a test in the test runner that checks its accuracy and
demonstrates its usage because I'm thorough like that.
The best part is, if you want to incorporate a different type of inline
post-processing the capability is already there. I'm not sure what other hooks
I'm going to add yet, maybe onParseEntry, onPreParse, onPostParse.
Have fun. Test it out. Let me know if it breaks.
Original comment by [email protected]
on 7 Oct 2012 at 5:21
from jquery-csv.
Original comment by [email protected]
on 7 Oct 2012 at 6:22
- Changed state: Fixed
from jquery-csv.
Thanks, will try this and let you know.
http://techdiary-viki.blogspot.in
Original comment by [email protected]
on 8 Oct 2012 at 6:42
from jquery-csv.
Original comment by [email protected]
on 11 Oct 2012 at 4:07
from jquery-csv.
[deleted comment]
from jquery-csv.
I have written about how I used this package:
http://techdiary-viki.blogspot.in/2012/10/javascript-using-ajax-jquery-csv-to.ht
ml
Original comment by [email protected]
on 16 Oct 2012 at 8:25
from jquery-csv.
Nice,
Original comment by [email protected]
on 16 Oct 2012 at 9:34
- Changed state: Verified
from jquery-csv.
Related Issues (20)
- doesn't seem to recoginse end of line properly in some cases. HOT 1
- CR line ending are not well treated HOT 1
- Typo in basic-usage.html (fixed in 0.8.0) HOT 2
- Enhancement: Parse CSV in a thread using settimeout HOT 3
- 배열의 기본 속성값(remove)이 포함 됩니다. HOT 1
- Karma test fails - HOT 2
- Failures on test.html file HOT 2
- colNum not reset when onParseValue callback called
- Export $ globally in browser when not already set
- CSVDataError: Illegal State - Only in IE HOT 3
- Using Zurb Foundation 3 causing an error HOT 3
- $.csv.toObjects return garbage value HOT 3
- Parse doesn't handle quotes in the CSV, even if escaped HOT 3
- Hooks relying on a return value of false to skip entries makes parsing values that resolve to false impossible
- CSV file created with Excel for Mac fails HOT 3
- Uncaught ReferenceError: require is not defined
- Generate Motion chart HOT 1
- Options arg not really optional? HOT 2
- TypeError: csv.replace is not a function
- Data Format
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jquery-csv.