Coder Social home page Coder Social logo

Comments (10)

mrodrig avatar mrodrig commented on July 18, 2024 1

Per #66 this was causing an issue that I didn't see in my dev environment that was causing the number 1000 to be converted to 1,000 which was not of the proper form. I reverted the changes and will look to see if there is a better way to implement this without breaking the functionality.

Edit: Reverted in 2.0.18; Unpublished 2.0.13 through 2.0.17 which had this change.

from json-2-csv.

mrodrig avatar mrodrig commented on July 18, 2024

I hadn't thought about that, but it definitely makes more sense to use value.toLocaleString() than value.toString() like you mentioned. I'll definitely take a look at implementing this. Thanks!

from json-2-csv.

mrodrig avatar mrodrig commented on July 18, 2024

I was working on implementing this, but noticed that it might cause the csv2json function to incorrectly convert data back to JSON. In particular, if json2csv converts a float (ie. 3.14) to 3,14, then it won't be as easy to convert back to a float: parseFloat('3,14'); // returns 3 since it doesn't take a locale.

I suppose this is fine since all values are treated as strings in csv2json though.

from json-2-csv.

mrodrig avatar mrodrig commented on July 18, 2024

I believe this should be all set in version 2.0.13. The option to set is: option.LOCALE. I tried testing it out and despite setting the locale to a variety of the values on MDN .toLocaleString() documentation page, I didn't see any difference but that could just be the environment configuration that I have.

Please let me know if you have any issues with it. Thanks again!

from json-2-csv.

datuary-jmartinez avatar datuary-jmartinez commented on July 18, 2024

Hello Mike:

I was working on implementing this, but noticed that it might cause the csv2json function to incorrectly convert data back to JSON. In particular, if json2csv converts a float (ie. 3.14) to 3,14, then it won't be as easy to convert back to a float: parseFloat('3,14'); // returns 3 since it doesn't take a locale.

I suppose this is fine since all values are treated as strings in csv2json though.

I think that before converting from CSV (string) to JSON (float) you should check option.LOCALE. If no option provided or it is similar to US format, nothing has to be done and parseFloat('3.14') must work properly. Otherwise, parseFloat(value.replace(',','.')) should be execute to replace string for a valid format.

Going further, I think you could always replace commas with points before parsing because if you have '3.14' you do nothing and if you have '3,14' you convert it to proper format.

You can also ignore .toLocaleString and allow user to declare his own decimal separator and thousand separator. User has now the responsibility and flexibility to choose whatever he needs.

Regards

from json-2-csv.

datuary-jmartinez avatar datuary-jmartinez commented on July 18, 2024

Hello again:

I have just executed this code in Javascript console in Chrome:

var pi = 3.14;
undefined
console.log(pi.toString());
3.14
undefined
console.log(pi.toLocaleString('es-ES'));
3,14
undefined 

It works as expected. I don't think it has something to do with your environment because you explicitly indicate what locale you want.

Regards

from json-2-csv.

mrodrig avatar mrodrig commented on July 18, 2024

Thanks @jmartinez-emagina! I will take a look at configuring csv2json to perform those checks. I see the same behavior in the Chrome console, but when I tried it in Node on my Cloud9 development machine, it completely ignores the locale provided. I believe that is due to Cloud9's environment configuration, but I would imagine that the option (now available in version 2.0.13 with options.locale) should work as expected. Thanks!

Output from Node on Cloud9 for reference:

> var pi = 3.14
undefined
> console.log(pi.toString())
3.14
undefined
> console.log(pi.toLocaleString('es-ES'))
3.14
undefined

from json-2-csv.

mrodrig avatar mrodrig commented on July 18, 2024

Going to close this out until I have a chance to convert the module to force values to be wrapped in quotes.

from json-2-csv.

meelash avatar meelash commented on July 18, 2024

I have another use case for this, wondering if this is still a possibility to get implemented, and/or if there is a better way currently to accomplish what I'm seeking.
By default datetimes will be converted to UTC when creating a csv, since that is what Date.prototype.toString() does. Generally, I'm guessing this would not be desired behavior.

Example:

{
  startTime: new Date('2020-03-05 16:34:39')
}

If my local timezone is EST, json2csv output will be:

startTime
'2020-03-05 21:34:39'

Is there a way I can (even a temporary fix) get json2csv to use toLocaleString or any other way to get dates to convert to a string in the local timezone?

from json-2-csv.

mrodrig avatar mrodrig commented on July 18, 2024

Added the useLocaleFormat option, which when set to true will cause the module to use toLocaleString() instead of toString(). This new option is published in 3.7.0 and should be available on NPM.

from json-2-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.