11'use strict' ;
22
33var _ = require ( 'underscore' ) ,
4+ constants = require ( './constants' ) ,
45 Promise = require ( 'bluebird' ) ;
56
67var options = { } ; // Initialize the options - this will be populated when the json2csv function is called.
@@ -28,7 +29,7 @@ var generateHeading = function(data) {
2829 // If there is a difference between the schemas, throw the inconsistent schema error
2930 var diff = _ . difference ( firstDocSchema , _ . flatten ( keyList ) ) ;
3031 if ( ! _ . isEqual ( diff , [ ] ) ) {
31- return reject ( new Error ( 'Not all documents have the same schema.' ) ) ;
32+ return reject ( new Error ( constants . Errors . json2csv . notSameSchema ) ) ;
3233 }
3334 } ) ;
3435 return resolve ( _ . flatten ( keys [ 0 ] ) ) ;
@@ -98,15 +99,15 @@ module.exports = {
9899 // Function to export internally
99100 // Takes options as a document, data as a JSON document array, and a callback that will be used to report the results
100101 json2csv : function ( opts , data , callback ) {
101- if ( ! callback ) { throw new Error ( 'A callback is required!' ) ; } // If a callback wasn't provided, throw an error
102+ if ( ! callback ) { throw new Error ( constants . Errors . callbackRequired ) ; } // If a callback wasn't provided, throw an error
102103
103- if ( ! opts ) { return callback ( new Error ( 'Options were not passed and are required.' ) ) ; } // Shouldn't happen, but just in case
104+ if ( ! opts ) { return callback ( new Error ( constants . Errors . optionsRequired ) ) ; } // Shouldn't happen, but just in case
104105 else { options = opts ; } // Options were passed, set the global options value
105106
106- if ( ! data ) { return callback ( new Error ( 'Cannot call json2csv on ' + data + '.' ) ) ; } // If we don't receive data, report an error
107+ if ( ! data ) { return callback ( new Error ( constants . Errors . json2csv . cannotCallJson2CsvOn + data + '.' ) ) ; } // If we don't receive data, report an error
107108
108109 if ( ! _ . isObject ( data ) ) { // If the data was not a single document or an array of documents
109- return callback ( new Error ( 'Data provided was not an array of documents.' ) ) ; // Report the error back to the caller
110+ return callback ( new Error ( constants . Errors . json2csv . dataNotArrayOfDocuments ) ) ; // Report the error back to the caller
110111 } else if ( _ . isObject ( data ) && ! data . length ) { // Single document, not an array
111112 data = [ data ] ; // Convert to an array of the given document
112113 }
@@ -128,4 +129,4 @@ module.exports = {
128129 } ) ;
129130 }
130131
131- } ;
132+ } ;
0 commit comments