all files / lib/waterline/core/ typecast.js

93.98% Statements 78/83
93.48% Branches 43/46
100% Functions 11/11
94.59% Lines 70/74
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                                  266×   266×                     266×   266× 1169×                         1319×   1319×       1319×     3575× 3575× 155×       3420×     3398×     3398×     1655× 1655×     969× 969×     52× 52×         390× 390×     319× 319×     13× 13×       1319×                     1655×                       969×         969×     967× 967×         967×                     52×   52× 52×         52×                     319×   319×       316× 316×         316× 315×   314×                     390× 390× 70× 320×   319×     390× 390×                     13×    
/**
 * Module dependencies
 */
 
var types = require('../utils/types');
var utils = require('../utils/helpers');
var hasOwnProperty = utils.object.hasOwnProperty;
var _ = require('lodash');
 
/**
 * Cast Types
 *
 * Will take values and cast they to the correct type based on the
 * type defined in the schema.
 *
 * Especially handy for converting numbers passed as strings to the
 * correct integer type.
 *
 * Should be run before sending values to an adapter.
 */
 
var Cast = module.exports = function() {
  this._types = {};
 
  return this;
};
 
/**
 * Builds an internal _types object that contains each
 * attribute with it's type. This can later be used to
 * transform values into the correct type.
 *
 * @param {Object} attrs
 */
 
Cast.prototype.initialize = function(attrs) {
  var self = this;
 
  Object.keys(attrs).forEach(function(key) {
    self._types[key] = ~types.indexOf(attrs[key].type) ? attrs[key].type : 'string';
  });
};
 
/**
 * Converts a set of values into the proper types
 * based on the Collection's schema.
 *
 * @param {Object} values
 * @return {Object}
 * @api public
 */
 
Cast.prototype.run = function(values) {
  var self = this;
 
  Iif (values === undefined || values === null) {
    return;
  }
 
  Object.keys(values).forEach(function(key) {
 
    // Set undefined to null
    if (_.isUndefined(values[key])) values[key] = null;
    if (!hasOwnProperty(self._types, key) || values[key] === null || !hasOwnProperty(values, key)) {
      return;
    }
 
    // If the value is a plain object, don't attempt to cast it
    if (_.isPlainObject(values[key])) return;
 
    // Find the value's type
    var type = self._types[key];
 
    // Casting Function
    switch (type) {
      case 'string':
      case 'text':
        values[key] = self.string(values[key]);
        break;
 
      case 'integer':
        values[key] = self.integer(key, values[key]);
        break;
 
      case 'float':
        values[key] = self.float(values[key]);
        break;
 
      case 'date':
      case 'time':
      case 'datetime':
        values[key] = self.date(values[key]);
        break;
 
      case 'boolean':
        values[key] = self.boolean(values[key]);
        break;
 
      case 'array':
        values[key] = self.array(values[key]);
        break;
    }
  });
 
  return values;
};
 
/**
 * Cast String Values
 *
 * @param {String} str
 * @return {String}
 * @api private
 */
 
Cast.prototype.string = function string(str) {
  return typeof str.toString !== 'undefined' ? str.toString() : '' + str;
};
 
/**
 * Cast Integer Values
 *
 * @param {String} key
 * @param {Integer} value
 * @return {Integer}
 * @api private
 */
 
Cast.prototype.integer = function integer(key, value) {
  var _value;
 
  // Attempt to see if the value is resembles a MongoID
  // if so let's not try and cast it and instead return a string representation of
  // it. Needed for sails-mongo.
  if (utils.matchMongoId(value)) return value.toString();
 
  // Attempt to parseInt
  try {
    _value = parseInt(value, 10);
  } catch(e) {
    return value;
  }
 
  return _value;
};
 
/**
 * Cast Float Values
 *
 * @param {Float} value
 * @return {Float}
 * @api private
 */
 
Cast.prototype.float = function float(value) {
  var _value;
 
  try {
    _value = parseFloat(value);
  } catch(e) {
    return value;
  }
 
  return _value;
};
 
/**
 * Cast Boolean Values
 *
 * @param {Boolean} value
 * @return {Boolean}
 * @api private
 */
 
Cast.prototype.boolean = function boolean(value) {
  var parsed;
 
  if (_.isString(value)) {
    if (value === 'true') return true;
    if (value === 'false') return false;
    return value;
  }
 
  // Nicely cast [0, 1] to true and false
  try {
    parsed = parseInt(value, 10);
  } catch(e) {
    return false;
  }
 
  if (parsed === 0) return false;
  if (parsed === 1) return true;
 
  return value;
};
 
/**
 * Cast Date Values
 *
 * @param {String|Date} value
 * @return {Date}
 * @api private
 */
 
Cast.prototype.date = function date(value) {
  var _value;
  if (value.__proto__ == Date.prototype) {
    _value = new Date(value.getTime());
  } else if (typeof value.toDate === 'function') {
    _value = value.toDate();
  } else {
    _value = new Date(Date.parse(value));
  }
 
  Iif (_value.toString() === 'Invalid Date') return value;
  return _value;
};
 
/**
 * Cast Array Values
 *
 * @param {Array|String} value
 * @return {Array}
 * @api private
 */
 
Cast.prototype.array = function array(value) {
  if (Array.isArray(value)) return value;
  return [value];
};