pablocortez / jquery-csv

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

Identify data type and then push into array (in case of csv2array) #9

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Basically when using this csv2array function, the returned array elements are 
all of string type. If i want to use this utility in a situation where the 
values in the array are to be interpreted as number/integers/floats/strings. 
Then the code has to identify the data type and accordingly push the elements 
into array. I have modified the source code to put my own code for this. The 
added this code only to the portion which is relevant for me, and not very well 
tested either. May be this can be a starting point for including this be 
default. Attached is the code i modified.
Thanks,
Vikas

Original issue reported on code.google.com by vikas...@gmail.com on 26 Sep 2012 at 8:24

Attachments:

GoogleCodeExporter commented 9 years ago
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 evanpla...@gmail.com on 1 Oct 2012 at 9:54

GoogleCodeExporter commented 9 years ago
Thanks. The hook is better as you said, leaving the parser ... be just the
parser :)

Original comment by vikas...@gmail.com on 2 Oct 2012 at 5:55

GoogleCodeExporter commented 9 years ago
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 evanpla...@gmail.com on 7 Oct 2012 at 5:21

GoogleCodeExporter commented 9 years ago

Original comment by evanpla...@gmail.com on 7 Oct 2012 at 6:22

GoogleCodeExporter commented 9 years ago
Thanks, will try this and let you know.

http://techdiary-viki.blogspot.in

Original comment by vikas...@gmail.com on 8 Oct 2012 at 6:42

GoogleCodeExporter commented 9 years ago

Original comment by evanpla...@gmail.com on 11 Oct 2012 at 4:07

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
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 vikas...@gmail.com on 16 Oct 2012 at 8:25

GoogleCodeExporter commented 9 years ago
Nice,

Original comment by evanpla...@gmail.com on 16 Oct 2012 at 9:34