@@ -21,6 +21,7 @@ var json_regularJson = require('./JSON/regularJson'),
2121 json_noData = require ( './JSON/noData.json' ) ,
2222 json_singleDoc = require ( './JSON/singleDoc.json' ) ,
2323 json_arrayValue = require ( './JSON/arrayValueDocs.json' ) ,
24+ json_arrayValue_specificKeys = require ( './JSON/arrayValueDocs_specificKeys.json' ) ,
2425 json_sameSchemaDifferentOrdering = require ( './JSON/sameSchemaDifferentOrdering' ) ,
2526 json_differentSchemas = require ( './JSON/differentSchemas' ) ,
2627 csv_regularJson = '' ,
@@ -29,7 +30,8 @@ var json_regularJson = require('./JSON/regularJson'),
2930 csv_nestedQuotes = '' ,
3031 csv_noData = '' ,
3132 csv_singleDoc = '' ,
32- csv_arrayValue = '' ;
33+ csv_arrayValue = '' ,
34+ csv_arrayValue_specificKeys = '' ;
3335
3436var json2csvTests = function ( ) {
3537 describe ( 'json2csv - non-promisified' , function ( done ) {
@@ -90,6 +92,16 @@ var json2csvTests = function () {
9092 } , options ) ;
9193 } ) ;
9294
95+ it ( 'should parse the specified keys to CSV' , function ( done ) {
96+ // Create a copy so we don't modify the actual options object
97+ var opts = _ . extend ( JSON . parse ( JSON . stringify ( options ) ) , { KEYS : [ 'info.name' , 'year' ] } ) ;
98+ converter . json2csv ( json_arrayValue , function ( err , csv ) {
99+ csv . should . equal ( csv_arrayValue_specificKeys ) ;
100+ csv . split ( options . EOL ) . length . should . equal ( 5 ) ;
101+ done ( ) ;
102+ } , opts ) ;
103+ } ) ;
104+
93105 it ( 'should parse an array of JSON documents with the same schema but different ordering of fields' , function ( done ) {
94106 converter . json2csv ( json_sameSchemaDifferentOrdering , function ( err , csv ) {
95107 csv . should . equal ( csv_regularJson ) ;
@@ -510,6 +522,20 @@ var json2csvTests = function () {
510522 } ) ;
511523 } ) ;
512524
525+ it ( 'should parse the specified keys to CSV' , function ( done ) {
526+ // Create a copy so we don't modify the actual options object
527+ var opts = _ . extend ( JSON . parse ( JSON . stringify ( options ) ) , { KEYS : [ 'info.name' , 'year' ] } ) ;
528+ converter . json2csvAsync ( json_arrayValue , opts )
529+ . then ( function ( csv ) {
530+ csv . should . equal ( csv_arrayValue_specificKeys ) ;
531+ csv . split ( options . EOL ) . length . should . equal ( 5 ) ;
532+ done ( ) ;
533+ } )
534+ . catch ( function ( err ) {
535+ throw err ;
536+ } ) ;
537+ } ) ;
538+
513539 it ( 'should parse an array of JSON documents with the same schema but different ordering of fields' , function ( done ) {
514540 converter . json2csvAsync ( json_sameSchemaDifferentOrdering )
515541 . then ( function ( csv ) {
@@ -617,6 +643,15 @@ var csv2jsonTests = function () {
617643 } , options ) ;
618644 } ) ;
619645
646+ it ( 'should parse the specified keys to JSON' , function ( done ) {
647+ var opts = _ . extend ( JSON . parse ( JSON . stringify ( options ) ) , { KEYS : [ 'info.name' , 'year' ] } ) ;
648+ converter . csv2jsonAsync ( csv_arrayValue . replace ( / , / g, options . DELIMITER . FIELD ) , function ( err , json ) {
649+ var isEqual = _ . isEqual ( json , json_arrayValue_specificKeys ) ;
650+ true . should . equal ( isEqual ) ;
651+ done ( ) ;
652+ } , opts ) ;
653+ } ) ;
654+
620655 it ( 'should throw an error about not having been passed data - 1' , function ( done ) {
621656 converter . csv2json ( null , function ( err , json ) {
622657 err . message . should . equal ( 'Cannot call csv2json on null.' ) ;
@@ -876,6 +911,19 @@ var csv2jsonTests = function () {
876911 } ) ;
877912 } ) ;
878913
914+ it ( 'should parse the specified keys to JSON' , function ( done ) {
915+ var opts = _ . extend ( JSON . parse ( JSON . stringify ( options ) ) , { KEYS : [ 'info.name' , 'year' ] } ) ;
916+ converter . csv2jsonAsync ( csv_arrayValue . replace ( / , / g, options . DELIMITER . FIELD ) , opts )
917+ . then ( function ( json ) {
918+ var isEqual = _ . isEqual ( json , json_arrayValue_specificKeys ) ;
919+ true . should . equal ( isEqual ) ;
920+ done ( ) ;
921+ } )
922+ . catch ( function ( err ) {
923+ throw err ;
924+ } ) ;
925+ } ) ;
926+
879927 it ( 'should throw an error about not having been passed data - 1' , function ( done ) {
880928 converter . csv2jsonAsync ( null , options )
881929 . then ( function ( json ) {
@@ -1063,6 +1111,13 @@ module.exports = {
10631111 csv_arrayValue = data . toString ( ) ;
10641112 callback ( null ) ;
10651113 } ) ;
1114+ } ,
1115+ function ( callback ) {
1116+ fs . readFile ( 'test/CSV/arrayValueDocs_specificKeys.csv' , function ( err , data ) {
1117+ if ( err ) callback ( err ) ;
1118+ csv_arrayValue_specificKeys = data . toString ( ) ;
1119+ callback ( null ) ;
1120+ } ) ;
10661121 }
10671122 ] ,
10681123 function ( err , results ) {
0 commit comments