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

87.82% Statements 173/197
85.86% Branches 164/191
69.57% Functions 16/23
91.43% Lines 160/175
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456             2458×     2458×   660× 694×     660×     660× 660×           2458×   222×     222× 222×         2458×   626× 626×       626× 555× 84× 83×             626× 13×       613× 613×           2458×         8301×       8301× 4921× 4921× 5431× 1097× 1097× 2470× 2470× 2470×         1097× 1097×         4921×   4915×   3380×           6543×     6543×   6540× 680×             5860×     5831× 9423×         5831×           5831× 1862×       5831×     5831×         201× 201× 201× 201× 201× 201× 201×         5630×       5831× 50× 50× 50× 5781× 584× 584×     5831× 46× 46× 46× 5785× 148× 148×     5831× 366× 366×       5831× 19× 19×     5831× 11× 11×     5831× 10× 10×     5831× 10× 10×     5831× 10× 10×     5831×   953× 12×         953×       953×       953×       5831× 1029×     5831× 5831×       5825×     1050× 318×     318×     318× 318×     1050× 13× 13× 26× 26×     26×   26×     13×       1050×   1114× 347×     347×       1112× 1112×       1048× 1112×         1048×         5823×                   17×   17×   17×     17×   17×   17×                                             8259× 8259× 8224×       8224× 8153×         71×   71× 71×     71×                         8259× 8259×                                                                                                                                   8224×    
var _ = require('lodash');
var util = require('./helpers');
var hop = util.object.hasOwnProperty;
var switchback = require('switchback');
var errorify = require('../error');
var WLUsageError = require('../error/WLUsageError');
 
module.exports = {
 
  // Expand Primary Key criteria into objects
  expandPK: function(context, options) {
 
    // Default to id as primary key
    var pk = 'id';
 
    // If autoPK is not used, attempt to find a primary key
    if (!context.autoPK) {
      // Check which attribute is used as primary key
      for (var key in context.attributes) {
        if (!util.object.hasOwnProperty(context.attributes[key], 'primaryKey')) continue;
 
        // Check if custom primaryKey value is falsy
        Iif (!context.attributes[key].primaryKey) continue;
 
        // If a custom primary key is defined, use it
        pk = key;
        break;
      }
    }
 
    // Check if options is an integer or string and normalize criteria
    // to object, using the specified primary key field.
    if (_.isNumber(options) || _.isString(options) || Array.isArray(options)) {
      // Temporary store the given criteria
      var pkCriteria = _.clone(options);
 
      // Make the criteria object, with the primary key
      options = {};
      options[pk] = pkCriteria;
    }
 
    // If we're querying by primary key, create a coercion function for it
    // depending on the data type of the key
    if (options && options[pk]) {
 
      var coercePK;
      Iif(!context.attributes[pk]) {
        return pk;
      }
 
      if (context.attributes[pk].type == 'integer') {
        coercePK = function(pk) {return +pk;};
      } else if (context.attributes[pk].type == 'string') {
        coercePK = function(pk) {return String(pk).toString();};
 
      // If the data type is unspecified, return the key as-is
      } else {
        coercePK = function(pk) {return pk;};
      }
 
      // If the criteria is an array of PKs, coerce them all
      if (Array.isArray(options[pk])) {
        options[pk] = options[pk].map(coercePK);
 
      // Otherwise just coerce the one
      } else {
        Eif (!_.isObject(options[pk])) {
          options[pk] = coercePK(options[pk]);
        }
      }
 
    }
 
    return options;
 
  },
 
  where: function(criteria) {
    var self = this;
    // If an IN was specified in the top level query and is an empty array, we can return an
    // empty object without running the query because nothing will match anyway. Let's return
    // false from here so the query knows to exit out.
    if (criteria && _.isObject(criteria)) {
      var falsy = false;
      Object.keys(criteria).forEach(function(key) {
        if (Array.isArray(criteria[key])) {
          var newCriteria = [];
          criteria[key].forEach(function(criterion) {
            var normalizedCriterion = self.where(criterion);
            Eif (normalizedCriterion !== false) {
              newCriteria.push(normalizedCriterion);
            } else if (key !== "or") {
              falsy = true;
            }
          });
          criteria[key] = newCriteria;
          if (criteria[key].length === 0) {
            falsy = true;
          }
        }
      });
 
      if (falsy) {
        return false;
      }
      return criteria;
    } else {
      return criteria;
    }
  },
 
  // Normalize the different ways of specifying criteria into a uniform object
  criteria: function(origCriteria) {
    var criteria = _.cloneDeep(origCriteria);
 
    // If original criteria is already false, keep it that way.
    if (criteria === false) return criteria;
 
    if (!criteria) {
      return {
        where: null
      };
    }
 
    // Let the calling method normalize array criteria. It could be an IN query
    // where we need the PK of the collection or a .findOrCreateEach
    if (Array.isArray(criteria)) return criteria;
 
    // Empty undefined values from criteria object
    _.each(criteria, function(val, key) {
      Iif (_.isUndefined(val)) criteria[key] = null;
    });
 
    // Convert non-objects (ids) into a criteria
    // TODO: use customizable primary key attribute
    Iif (!_.isObject(criteria)) {
      criteria = {
        id: +criteria || criteria
      };
    }
 
    if (_.isObject(criteria) && !criteria.where && criteria.where !== null) {
      criteria = { where: criteria };
    }
 
    // Return string to indicate an error
    Iif (!_.isObject(criteria)) throw new WLUsageError('Invalid options/criteria :: ' + criteria);
 
    // If criteria doesn't seem to contain operational keys, assume all the keys are criteria
    if (!criteria.where && !criteria.joins && !criteria.join && !criteria.limit && !criteria.skip &&
      !criteria.sort && !criteria.sum && !criteria.average &&
      !criteria.groupBy && !criteria.min && !criteria.max && !criteria.select) {
 
      // Delete any residuals and then use the remaining keys as attributes in a criteria query
      delete criteria.where;
      delete criteria.joins;
      delete criteria.join;
      delete criteria.limit;
      delete criteria.skip;
      delete criteria.sort;
      criteria = {
        where: criteria
      };
 
    // If where is null, turn it into an object
    } else if (_.isNull(criteria.where)) criteria.where = {};
 
 
    // Move Limit, Skip, sort outside the where criteria
    if (hop(criteria, 'where') && criteria.where !== null && hop(criteria.where, 'limit')) {
      criteria.limit = parseInt(_.clone(criteria.where.limit), 10);
      Iif (criteria.limit < 0) criteria.limit = 0;
      delete criteria.where.limit;
    } else if (hop(criteria, 'limit')) {
      criteria.limit = parseInt(criteria.limit, 10);
      Iif (criteria.limit < 0) criteria.limit = 0;
    }
 
    if (hop(criteria, 'where') && criteria.where !== null && hop(criteria.where, 'skip')) {
      criteria.skip = parseInt(_.clone(criteria.where.skip), 10);
      Iif (criteria.skip < 0) criteria.skip = 0;
      delete criteria.where.skip;
    } else if (hop(criteria, 'skip')) {
      criteria.skip = parseInt(criteria.skip, 10);
      Iif (criteria.skip < 0) criteria.skip = 0;
    }
 
    if (hop(criteria, 'where') && criteria.where !== null && hop(criteria.where, 'sort')) {
      criteria.sort = _.clone(criteria.where.sort);
      delete criteria.where.sort;
    }
 
    // Pull out aggregation keys from where key
    if (hop(criteria, 'where') && criteria.where !== null && hop(criteria.where, 'sum')) {
      criteria.sum = _.clone(criteria.where.sum);
      delete criteria.where.sum;
    }
 
    if (hop(criteria, 'where') && criteria.where !== null && hop(criteria.where, 'average')) {
      criteria.average = _.clone(criteria.where.average);
      delete criteria.where.average;
    }
 
    if (hop(criteria, 'where') && criteria.where !== null && hop(criteria.where, 'groupBy')) {
      criteria.groupBy = _.clone(criteria.where.groupBy);
      delete criteria.where.groupBy;
    }
 
    if (hop(criteria, 'where') && criteria.where !== null && hop(criteria.where, 'min')) {
      criteria.min = _.clone(criteria.where.min);
      delete criteria.where.min;
    }
 
    if (hop(criteria, 'where') && criteria.where !== null && hop(criteria.where, 'max')) {
      criteria.max = _.clone(criteria.where.max);
      delete criteria.where.max;
    }
 
    if (hop(criteria, 'where') && criteria.where !== null && hop(criteria.where, 'select') || hop(criteria, 'select')) {
 
      if(criteria.where.select) {
        criteria.select = _.clone(criteria.where.select);
      }
 
      // If the select contains a '*' then remove the whole projection, a '*'
      // will always return all records.
      Iif(!_.isArray(criteria.select)) {
        criteria.select = [criteria.select];
      }
 
      Iif(_.includes(criteria.select, '*')) {
        delete criteria.select;
      }
 
      delete criteria.where.select;
    }
 
    // If WHERE is {}, always change it back to null
    if (criteria.where && _.keys(criteria.where).length === 0) {
      criteria.where = null;
    }
 
    criteria.where = this.where(criteria.where);
    if (criteria.where === false) {
      return false;
    }
 
    // Normalize sort criteria
    if (hop(criteria, 'sort') && criteria.sort !== null) {
 
      // Split string into attr and sortDirection parts (default to 'asc')
      if (_.isString(criteria.sort)) {
        var parts = criteria.sort.split(' ');
 
        // Set default sort to asc
        parts[1] = parts[1] ? parts[1].toLowerCase() : 'asc';
 
        // Expand criteria.sort into object
        criteria.sort = {};
        criteria.sort[parts[0]] = parts[1];
      }
 
      if (_.isArray(criteria.sort)) {
        var sort = {};
        criteria.sort.forEach(function(attr) {
          Eif (_.isString(attr)) {
            var parts = attr.split(' ');
 
            // Set default sort to asc
            parts[1] = parts[1] ? parts[1].toLowerCase() : 'asc';
 
            sort[parts[0]] = parts[1];
          }
        });
        criteria.sort = sort;
      }
 
      // normalize ASC/DESC notation
      Object.keys(criteria.sort).forEach(function(attr) {
 
        if (_.isString(criteria.sort[attr])) {
          criteria.sort[attr] = criteria.sort[attr].toLowerCase();
 
          // Throw error on invalid sort order
          if (criteria.sort[attr] !== 'asc' && criteria.sort[attr] !== 'desc') {
            throw new WLUsageError('Invalid sort criteria :: ' + criteria.sort);
          }
        }
 
        if (criteria.sort[attr] === 'asc') criteria.sort[attr] = 1;
        if (criteria.sort[attr] === 'desc') criteria.sort[attr] = -1;
      });
 
      // normalize binary sorting criteria
      Object.keys(criteria.sort).forEach(function(attr) {
        if (criteria.sort[attr] === 0) criteria.sort[attr] = -1;
      });
 
      // Verify that user either specified a proper object
      // or provided explicit comparator function
      Iif (!_.isObject(criteria.sort) && !_.isFunction(criteria.sort)) {
        throw new WLUsageError('Invalid sort criteria for ' + attrName + ' :: ' + direction);
      }
    }
 
    return criteria;
  },
 
  // Normalize the capitalization and % wildcards in a like query
  // Returns false if criteria is invalid,
  // otherwise returns normalized criteria obj.
  // Enhancer is an optional function to run on each criterion to preprocess the string
  likeCriteria: function(criteria, attributes, enhancer) {
 
    // Only accept criteria as an object
    Iif (criteria !== Object(criteria)) return false;
 
    criteria = _.clone(criteria);
 
    if (!criteria.where) criteria = { where: criteria };
 
    // Apply enhancer to each
    if (enhancer) criteria.where = util.objMap(criteria.where, enhancer);
 
    criteria.where = { like: criteria.where };
 
    return criteria;
  },
 
 
  // Normalize a result set from an adapter
  resultSet: function(resultSet) {
 
    // Ensure that any numbers that can be parsed have been
    return util.pluralize(resultSet, numberizeModel);
  },
 
 
  /**
   * Normalize the different ways of specifying callbacks in built-in Offshore methods.
   * Switchbacks vs. Callbacks (but not deferred objects/promises)
   *
   * @param  {Function|Handlers} cb
   * @return {Handlers}
   */
  callback: function(cb) {
 
    // Build modified callback:
    // (only works for functions currently)
    var wrappedCallback;
    if (_.isFunction(cb)) {
      wrappedCallback = function(err) {
 
        // If no error occurred, immediately trigger the original callback
        // without messing up the context or arguments:
        if (!err) {
          return applyInOriginalCtx(cb, arguments);
        }
 
        // If an error argument is present, upgrade it to a WLError
        // (if it isn't one already)
        err = errorify(err);
 
        var modifiedArgs = Array.prototype.slice.call(arguments, 1);
        modifiedArgs.unshift(err);
 
        // Trigger callback without messing up the context or arguments:
        return applyInOriginalCtx(cb, modifiedArgs);
      };
    }
 
 
    //
    // TODO: Make it clear that switchback support it experimental.
    //
    // Push switchback support off until >= v0.11
    // or at least add a warning about it being a `stage 1: experimental`
    // feature.
    //
 
    if (!_.isFunction(cb)) wrappedCallback = cb;
    return switchback(wrappedCallback, {
      invalid: 'error', // Redirect 'invalid' handler to 'error' handler
      error: function _defaultErrorHandler() {
        console.error.apply(console, Array.prototype.slice.call(arguments));
      }
    });
 
 
    // ????
    // TODO: determine support target for 2-way switchback usage
    // ????
 
    // Allow callback to be -HANDLED- in different ways
    // at the app-level.
    // `cb` may be passed in (at app-level) as either:
    //    => an object of handlers
    //    => or a callback function
    //
    // If a callback function was provided, it will be
    // automatically upgraded to a simplerhandler object.
    // var cb_fromApp = switchback(cb);
 
    // Allow callback to be -INVOKED- in different ways.
    // (adapter def)
    // var cb_fromAdapter = cb_fromApp;
 
  }
};
 
// If any attribute looks like a number, but it's a string
// cast it to a number
function numberizeModel(model) {
  return util.objMap(model, numberize);
}
 
 
// If specified attr looks like a number, but it's a string, cast it to a number
function numberize(attr) {
  if (_.isString(attr) && isNumbery(attr) && parseInt(attr, 10) < Math.pow(2, 53)) return +attr;
  else return attr;
}
 
// Returns whether this value can be successfully parsed as a finite number
function isNumbery(value) {
  return Math.pow(+value, 2) > 0;
}
 
// Replace % with %%%
function escapeLikeQuery(likeCriterion) {
  return likeCriterion.replace(/[^%]%[^%]/g, '%%%');
}
 
// Replace %%% with %
function unescapeLikeQuery(likeCriterion) {
  return likeCriterion.replace(/%%%/g, '%');
}
 
 
/**
 * Like _.partial, but accepts an array of arguments instead of
 * comma-seperated args (if _.partial is `call`, this is `apply`.)
 * The biggest difference from `_.partial`, other than the usage,
 * is that this helper actually CALLS the partially applied function.
 *
 * This helper is mainly useful for callbacks.
 *
 * @param  {Function} fn   [description]
 * @param  {[type]}   args [description]
 * @return {[type]}        [description]
 */
 
function applyInOriginalCtx(fn, args) {
  return (_.partial.apply(null, [fn].concat(Array.prototype.slice.call(args))))();
}