Skip to content

Commit 42c58d8

Browse files
authored
Add 'useLocaleFormat' option. (#147)
* Add 'useLocaleFormat' option. Fixes #146, #64 * Force test timezone to EST.
1 parent 728faa6 commit 42c58d8

File tree

9 files changed

+79
-35
lines changed

9 files changed

+79
-35
lines changed

README.md

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -84,36 +84,6 @@ Looking for examples? Check out the Wiki: [json-2-csv Wiki](https://github.com/m
8484
* `false` uses the following keys:
8585
* `['specifications']`
8686
* Note: This may result in CSV output that does not map back exactly to the original JSON. See #102 for more information.
87-
* `unwindArrays` - Boolean - Should array values be "unwound" such that there is one line per value in the array?
88-
* Default: `false`
89-
* Example:
90-
```json
91-
[
92-
{
93-
"_id": {"$oid": "5cf7ca3616c91100018844af"},
94-
"data": {"category": "Computers", "options": [{"name": "MacBook Pro 15"}, {"name": "MacBook Air 13"}]}
95-
},
96-
{
97-
"_id": {"$oid": "5cf7ca3616c91100018844bf"},
98-
"data": {"category": "Cars", "options": [{"name": "Supercharger"}, {"name": "Turbocharger"}]}
99-
}
100-
]
101-
```
102-
* `true` will unwind the JSON to four objects, and therefore four lines of CSV values:
103-
```csv
104-
_id.$oid,data.category,data.options.name
105-
5cf7ca3616c91100018844af,Computers,MacBook Pro 15
106-
5cf7ca3616c91100018844af,Computers,MacBook Air 13
107-
5cf7ca3616c91100018844bf,Cars,Supercharger
108-
5cf7ca3616c91100018844bf,Cars,Turbocharger
109-
```
110-
* `false` will leave the values unwound and will convert the array as-is (when this option is used without expandArrayObjects):
111-
```csv
112-
_id.$oid,data.category,data.options
113-
5cf7ca3616c91100018844af,Computers,"[{""name"":""MacBook Pro 15""},{""name"":""MacBook Air 13""}]"
114-
5cf7ca3616c91100018844bf,Cars,"[{""name"":""Supercharger""},{""name"":""Turbocharger""}]"
115-
```
116-
* Note: This may result in CSV output that does not map back exactly to the original JSON.
11787
* `keys` - Array - Specify the keys (as strings) that should be converted.
11888
* Default: `null`
11989
* If you have a nested object (ie. {info : {name: 'Mike'}}), then set this to ['info.name']
@@ -122,10 +92,43 @@ Looking for examples? Check out the Wiki: [json-2-csv Wiki](https://github.com/m
12292
* Default: `true`
12393
* `sortHeader` - Boolean - Should the header keys be sorted in alphabetical order?
12494
* Default: `false`
95+
* `trimFieldValues` - Boolean - Should the field values be trimmed?
96+
* Default: `false`
12597
* `trimHeaderFields` - Boolean - Should the header fields be trimmed?
12698
* Default: `false`
127-
* `trimFieldValues` - Boolean - Should the field values be trimmed? (*in development*)
99+
* `unwindArrays` - Boolean - Should array values be "unwound" such that there is one line per value in the array?
100+
* Default: `false`
101+
* Example:
102+
```json
103+
[
104+
{
105+
"_id": {"$oid": "5cf7ca3616c91100018844af"},
106+
"data": {"category": "Computers", "options": [{"name": "MacBook Pro 15"}, {"name": "MacBook Air 13"}]}
107+
},
108+
{
109+
"_id": {"$oid": "5cf7ca3616c91100018844bf"},
110+
"data": {"category": "Cars", "options": [{"name": "Supercharger"}, {"name": "Turbocharger"}]}
111+
}
112+
]
113+
```
114+
* `true` will unwind the JSON to four objects, and therefore four lines of CSV values:
115+
```csv
116+
_id.$oid,data.category,data.options.name
117+
5cf7ca3616c91100018844af,Computers,MacBook Pro 15
118+
5cf7ca3616c91100018844af,Computers,MacBook Air 13
119+
5cf7ca3616c91100018844bf,Cars,Supercharger
120+
5cf7ca3616c91100018844bf,Cars,Turbocharger
121+
```
122+
* `false` will leave the values unwound and will convert the array as-is (when this option is used without expandArrayObjects):
123+
```csv
124+
_id.$oid,data.category,data.options
125+
5cf7ca3616c91100018844af,Computers,"[{""name"":""MacBook Pro 15""},{""name"":""MacBook Air 13""}]"
126+
5cf7ca3616c91100018844bf,Cars,"[{""name"":""Supercharger""},{""name"":""Turbocharger""}]"
127+
```
128+
* Note: This may result in CSV output that does not map back exactly to the original JSON.
129+
* `useLocaleFormat` - Boolean - Should values be converted to a locale specific string?
128130
* Default: `false`
131+
* Note: If selected, values will be converted using `toLocaleString()` rather than `toString()`
129132

130133

131134
For examples, please refer to the [json2csv API Documentation (Link)](https://github.com/mrodrig/json-2-csv/wiki/json2csv-Documentation)

src/constants.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"keys" : null,
3333
"checkSchemaDifferences": false,
3434
"expandArrayObjects": false,
35-
"unwindArrays": false
35+
"unwindArrays": false,
36+
"useLocaleFormat": false
3637
},
3738

3839
"values" : {

src/json2csv.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ const Json2Csv = function(options) {
279279
} else if (utils.isNull(fieldValue)) {
280280
return 'null';
281281
} else {
282-
return fieldValue.toString();
282+
return !options.useLocaleFormat ? fieldValue.toString() : fieldValue.toLocaleString();
283283
}
284284
}
285285

test/config/testCsvFilesList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const fs = require('fs'),
3030
{key: 'quotedEmptyFieldValue', file: '../data/csv/quotedEmptyFieldValue.csv'},
3131
{key: 'csvEmptyLastValue', file: '../data/csv/csvEmptyLastValue.csv'},
3232
{key: 'unwind', file: '../data/csv/unwind.csv'},
33-
{key: 'unwindWithSpecifiedKeys', file: '../data/csv/unwindWithSpecifiedKeys.csv'}
33+
{key: 'unwindWithSpecifiedKeys', file: '../data/csv/unwindWithSpecifiedKeys.csv'},
34+
{key: 'localeFormat', file: '../data/csv/localeFormat.csv'}
3435
];
3536

3637
function readCsvFile(filePath) {

test/config/testJsonFilesList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ module.exports = {
2222
emptyFieldValues: require('../data/json/emptyFieldValues'),
2323
quotedEmptyFieldValue: require('../data/json/quotedEmptyFieldValue'),
2424
csvEmptyLastValue: require('../data/json/csvEmptyLastValue'),
25-
unwind: require('../data/json/unwind')
25+
unwind: require('../data/json/unwind'),
26+
localeFormat: require('../data/json/localeFormat')
2627
};

test/data/csv/localeFormat.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name,features.name,price,createdAt,features.pros
2+
test1,modules,"12,345",2021-01-01T04:59:00.000Z,undefined
3+
test1,undefined,"12,345",2021-01-01T04:59:00.000Z,efficiency
4+
test2,testing,"35,000",2000-01-01T05:00:00.000Z,undefined

test/data/json/localeFormat.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = [
2+
{
3+
name: 'test1',
4+
features: [
5+
{
6+
name: 'modules'
7+
},
8+
{
9+
pros: 'efficiency'
10+
}
11+
],
12+
price: 12345,
13+
createdAt: new Date('2021-01-01T04:59:00.000Z')
14+
},
15+
{
16+
name: 'test2',
17+
features: [
18+
{
19+
name: 'testing'
20+
}
21+
],
22+
price: 35000,
23+
createdAt: new Date('2000-01-01T05:00:00.000Z')
24+
}
25+
];

test/json2csv.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ function runTests(jsonTestData, csvTestData) {
435435
keys: ['data.category', 'data.options.name']
436436
});
437437
});
438+
439+
it('should use the locale format when specified', (done) => {
440+
converter.json2csv(jsonTestData.localeFormat, (err, csv) => {
441+
if (err) done(err);
442+
csv.should.equal(csvTestData.localeFormat);
443+
done();
444+
}, { useLocaleFormat: true, unwindArrays: true });
445+
});
438446
});
439447
});
440448

test/tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ let json2csvTests = require('./json2csv'),
44
csvTestData = require('./config/testCsvFilesList');
55

66
describe('json-2-csv Node Module', function() {
7+
process.env.TZ = 'America/New_York';
78
// Run JSON to CSV tests
89
json2csvTests.runTests(jsonTestData, csvTestData);
910

0 commit comments

Comments
 (0)