all files / lib/offshore/utils/ transformations.js

66.67% Statements 12/18
50% Branches 4/8
50% Functions 1/2
66.67% Lines 10/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                                                       
/**
 * Transformations
 */
 
var Transformations = module.exports = {};
 
// Add JSON Transformation methods
Transformations.json = {};
 
/**
 * Write Method Transformations
 *
 * Used to stream back valid JSON from Offshore
 */
 
Transformations.json.write = function(model, index, cb) {
  var transformedModel;
 
  Eif (!model) transformedModel = '';
 
  // Transform to JSON
  Iif (model) {
    try {
      transformedModel = JSON.stringify(model);
    } catch (e) {
      return cb(e);
    }
  }
 
  // Prefix with opening [
  Eif (index === 0) { transformedModel = '['; }
 
  // Prefix with comma after first model
  Iif (index > 1) transformedModel = ',' + transformedModel;
 
  cb(null, transformedModel);
};
 
/**
 * Close off JSON Array
 */
Transformations.json.end = function(cb) {
  var suffix = ']';
  cb(null, suffix);
};