Comments (10)
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.
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.
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.
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.
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.
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.
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.
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.
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.
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)
- Prevent CSV Injection
- Headerless CSV HOT 3
- Incorrect parsing of wrapped values HOT 5
- Custom sorting of headers HOT 2
- How to properly change mongoDB timestamp to proper date? HOT 1
- TypeError: Cannot convert undefined or null to object HOT 1
- Private Vulnerability Disclosure HOT 1
- csv2json Allow CSV Files Without Headers HOT 3
- NestedDotKeys with false and 0 are returned as undefined HOT 4
- prependHeader: false, is not working HOT 2
- Paginated conversion ? HOT 1
- "expandArrayObjects" option not behaving as expected HOT 3
- sync version HOT 2
- Incorrect parsing of wrapped values when the new line is right after the starting quote HOT 1
- Ignoring an entire array with `excludeKeys` and `expandArrayObjects` HOT 2
- No possibility to use unescaped dots in headers HOT 11
- Feature Request: expandNestedObjects functionality with keys HOT 10
- Providing option.keys[] field with RegExp throws kp.slice() is not a function HOT 2
- Add support for RegExps in excludeKeys
- Field in CSV it turned into an object unexpectedly. HOT 2
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 json-2-csv.