11'use strict' ;
22
3- var json2Csv = require ( './json-2-csv' ) ,
4- csv2Json = require ( './csv-2-json' ) ,
5- _ = require ( 'underscore' ) ;
3+ var json2Csv = require ( './json-2-csv' ) , // Require our json-2-csv code
4+ csv2Json = require ( './csv-2-json' ) , // Require our csv-2-json code
5+ _ = require ( 'underscore' ) ; // Require underscore
66
7- var OPTIONS = function ( ) { // Ensures fields don't change from our defaults
7+ // Default options; By using a function this is essentially a 'static' variable
8+ var OPTIONS = function ( ) {
89 return {
910 DELIMITER : ',' ,
1011 EOL : '\n' ,
1112 PARSE_CSV_NUMBERS : false
1213 } ;
1314}
1415
16+ // Build the options to be passed to the appropriate function
17+ // If a user does not provide custom options, then we use our default
18+ // If options are provided, then we set each valid key that was passed
1519var buildOptions = function ( opts ) {
1620 var out = _ . extend ( OPTIONS ( ) , { } ) ;
1721 if ( ! opts ) { return out ; } // If undefined or null, return defaults
@@ -23,13 +27,23 @@ var buildOptions = function (opts) {
2327 return out ; // Return customized version
2428} ;
2529
30+ // Export the following functions that will be client accessible
2631module . exports = {
2732
33+ // Client accessible json2csv function
34+ // Takes an array of JSON documents to be converted,
35+ // a callback that will be called with (err, csv) after
36+ // processing is completed, and optional options
2837 json2csv : function ( array , callback , opts ) {
29- opts = buildOptions ( opts ) ;
30- json2Csv . json2csv ( opts , array , callback ) ;
38+ opts = buildOptions ( opts ) ; // Build the options
39+ json2Csv . json2csv ( opts , array , callback ) ; // Call our internal json2csv function
3140 } ,
3241
42+
43+ // Client accessible csv2json function
44+ // Takes a string of CSV to be converted to a JSON document array,
45+ // a callback that will be called with (err, csv) after
46+ // processing is completed, and optional options
3347 csv2json : function ( csv , callback , opts ) {
3448 opts = buildOptions ( opts ) ;
3549 csv2Json . csv2json ( opts , csv , callback ) ;
0 commit comments