Git Product home page Git Product logo

Comments (9)

GoogleCodeExporter avatar GoogleCodeExporter commented on August 24, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 24, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 24, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 24, 2024

Original comment by [email protected] on 7 Oct 2012 at 6:22

  • Changed state: Fixed

from jquery-csv.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 24, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 24, 2024

Original comment by [email protected] on 11 Oct 2012 at 4:07

from jquery-csv.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 24, 2024
[deleted comment]

from jquery-csv.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 24, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 24, 2024
Nice,

Original comment by [email protected] on 16 Oct 2012 at 9:34

  • Changed state: Verified

from jquery-csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.