Skip to content

Commit d89d3cb

Browse files
committed
Add test for 'useDateIso8601Format' option, version 3.7.11
1 parent 0e821ea commit d89d3cb

File tree

6 files changed

+22
-10
lines changed

6 files changed

+22
-10
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ Looking for examples? Check out the Wiki: [json-2-csv Wiki](https://github.com/m
124124
5cf7ca3616c91100018844bf,Cars,"[{""name"":""Supercharger""},{""name"":""Turbocharger""}]"
125125
```
126126
* Note: This may result in CSV output that does not map back exactly to the original JSON.
127+
* `useDateIso8601Format` - Boolean - Should date values be converted to an ISO8601 date string?
128+
* Default: `false`
129+
* Note: If selected, values will be converted using `toISOString()` rather than `toString()` or `toLocaleString()` depending on the other options provided.
127130
* `useLocaleFormat` - Boolean - Should values be converted to a locale specific string?
128131
* Default: `false`
129132
* Note: If selected, values will be converted using `toLocaleString()` rather than `toString()`

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"name": "json-2-csv",
77
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
8-
"version": "3.7.10",
8+
"version": "3.7.11",
99
"repository": {
1010
"type": "git",
1111
"url": "https://github.com/mrodrig/json-2-csv.git"

src/converter.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ export interface IFullOptions extends ISharedOptions {
8686
* Should dates be output in ISO 8601 "Z" format:
8787
* @default false
8888
*/
89-
useDateIso8601Format?: boolean;
90-
89+
useDateIso8601Format?: boolean;
9190
}
9291

9392
export function json2csv(data: object[],

src/json2csv.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,14 @@ const Json2Csv = function(options) {
272272
* @returns {*}
273273
*/
274274
function recordFieldValueToString(fieldValue) {
275-
const isD = utils.isDate(fieldValue); // store to avoid checking twice
276-
if (Array.isArray(fieldValue) || utils.isObject(fieldValue) && !isD) {
275+
const isDate = utils.isDate(fieldValue); // store to avoid checking twice
276+
277+
if (utils.isNull(fieldValue) || Array.isArray(fieldValue) || utils.isObject(fieldValue) && !isDate) {
277278
return JSON.stringify(fieldValue);
278279
} else if (utils.isUndefined(fieldValue)) {
279280
return 'undefined';
280-
} else if (utils.isNull(fieldValue)) {
281-
return 'null';
282-
} else if (isD && options.useDateIso8601Format) {
283-
return new Date(fieldValue).toISOString();
281+
} else if (isDate && options.useDateIso8601Format) {
282+
return fieldValue.toISOString();
284283
} else {
285284
return !options.useLocaleFormat ? fieldValue.toString() : fieldValue.toLocaleString();
286285
}

test/json2csv.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,17 @@ function runTests(jsonTestData, csvTestData) {
453453
done();
454454
}, { useLocaleFormat: true, unwindArrays: true });
455455
});
456+
457+
it('should convert objects with dates to iso8601 format correctly', (done) => {
458+
// Convert to a date since the `dob` value is imported as a string by default for some reason
459+
jsonTestData.date.dob = new Date('1990-01-01T05:00:00.000Z');
460+
461+
converter.json2csv(jsonTestData.date, (err, csv) => {
462+
if (err) done(err);
463+
csv.should.equal(csvTestData.date);
464+
done();
465+
}, { useDateIso8601Format: true });
466+
});
456467
});
457468
});
458469

0 commit comments

Comments
 (0)