Skip to content

Commit 4f0e01b

Browse files
authored
Pass default parser to custom parse value function (#218)
* Pass builtin parse method to parseValue function * Update README.md
1 parent 0035b49 commit 4f0e01b

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Looking for examples? Check out the Wiki: [json-2-csv Wiki](https://github.com/m
9191
* `[ 'key1', 'key2', ... ]`
9292
* `[ { field: 'key1', title: 'Key 1' }, { field: 'key2' }, 'key3', ... ]`
9393
* Key Paths - If you are converting a nested object (ie. {info : {name: 'Mike'}}), then set this to ['info.name']
94-
* `parseValue` - Function - Specify how values should be converted into CSV format. This function is provided a single field value at a time and must return a `String`.
94+
* `parseValue` - Function - Specify how values should be converted into CSV format. This function is provided a single field value at a time and must return a `String`. The built-in parsing method is provided as the second argument for cases where default parsing is preferred.
9595
* Default: A built-in method is used to parse out a variety of different value types to well-known formats.
9696
* Note: Using this option may override other options, including `useDateIso8601Format` and `useLocaleFormat`.
9797
* `prependHeader` - Boolean - Should the auto-generated header be prepended as the first line in the CSV?

lib/converter.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ export interface IFullOptions extends ISharedOptions {
114114
* Specify how values should be converted into CSV format. This function is provided a single field value at a time and must return a `String`.
115115
* Note: Using this option may override other options, including `useDateIso8601Format` and `useLocaleFormat`.
116116
*/
117-
parseValue?: (fieldValue: any) => string;
117+
parseValue?: (
118+
fieldValue: any,
119+
defaultParser: (fieldValue: any) => string
120+
) => string;
118121
}
119122

120123
export function json2csv(data: object[],

lib/json2csv.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let path = require('doc-path'),
88
const Json2Csv = function(options) {
99
const wrapDelimiterCheckRegex = new RegExp(options.delimiter.wrap, 'g'),
1010
crlfSearchRegex = /\r?\n|\r/,
11-
valueParserFn = options.parseValue && typeof options.parseValue === 'function' ? options.parseValue : recordFieldValueToString,
11+
customValueParser = options.parseValue && typeof options.parseValue === 'function' ? options.parseValue : null,
1212
expandingWithoutUnwinding = options.expandArrayObjects && !options.unwindArrays,
1313
deeksOptions = {
1414
expandArrayObjects: expandingWithoutUnwinding,
@@ -251,7 +251,7 @@ const Json2Csv = function(options) {
251251
// Process the data in this record and return the
252252
processedRecordData = recordFieldData.map((fieldValue) => {
253253
fieldValue = trimRecordFieldValue(fieldValue);
254-
fieldValue = valueParserFn(fieldValue);
254+
fieldValue = customValueParser ? customValueParser(fieldValue, recordFieldValueToString) : recordFieldValueToString(fieldValue);
255255
fieldValue = preventCsvInjection(fieldValue);
256256
fieldValue = wrapFieldValueIfNecessary(fieldValue);
257257

test/json2csv.js

+10
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,16 @@ function runTests(jsonTestData, csvTestData) {
647647
});
648648
});
649649

650+
it('should pass the default value parser to custom value parser function when provided', (done) => {
651+
converter.json2csv(jsonTestData.trimmedFields, (err, csv) => {
652+
if (err) done(err);
653+
csv.should.equal(csvTestData.trimmedFields);
654+
done();
655+
}, {
656+
parseValue: (fieldValue, defaultParser) => defaultParser(fieldValue)
657+
});
658+
});
659+
650660
it('should wrap boolean values in wrap delimiters, if specified', (done) => {
651661
converter.json2csv(jsonTestData.emptyFieldValues, (err, csv) => {
652662
if (err) done(err);

0 commit comments

Comments
 (0)