All files / mongoose/lib/schema/operators geospatial.js

10.2% Statements 5/49
0% Branches 0/36
0% Functions 0/9
10.2% Lines 5/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            1x 1x           1x 1x 1x                                                                                                                                                                                        
/*!
 * Module requirements.
 */
 
'use strict';
 
const castArraysOfNumbers = require('./helpers').castArraysOfNumbers;
const castToNumber = require('./helpers').castToNumber;
 
/*!
 * ignore
 */
 
exports.cast$geoIntersects = cast$geoIntersects;
exports.cast$near = cast$near;
exports.cast$within = cast$within;
 
function cast$near(val) {
  const SchemaArray = require('../array');
 
  if (Array.isArray(val)) {
    castArraysOfNumbers(val, this);
    return val;
  }
 
  _castMinMaxDistance(this, val);
 
  if (val && val.$geometry) {
    return cast$geometry(val, this);
  }
 
  if (!Array.isArray(val)) {
    throw new TypeError('$near must be either an array or an object ' +
      'with a $geometry property');
  }
 
  return SchemaArray.prototype.castForQuery.call(this, null, val);
}
 
function cast$geometry(val, self) {
  switch (val.$geometry.type) {
    case 'Polygon':
    case 'LineString':
    case 'Point':
      castArraysOfNumbers(val.$geometry.coordinates, self);
      break;
    default:
      // ignore unknowns
      break;
  }
 
  _castMinMaxDistance(self, val);
 
  return val;
}
 
function cast$within(val) {
  _castMinMaxDistance(this, val);
 
  if (val.$box || val.$polygon) {
    const type = val.$box ? '$box' : '$polygon';
    val[type].forEach(arr => {
      if (!Array.isArray(arr)) {
        const msg = 'Invalid $within $box argument. '
            + 'Expected an array, received ' + arr;
        throw new TypeError(msg);
      }
      arr.forEach((v, i) => {
        arr[i] = castToNumber.call(this, v);
      });
    });
  } else if (val.$center || val.$centerSphere) {
    const type = val.$center ? '$center' : '$centerSphere';
    val[type].forEach((item, i) => {
      if (Array.isArray(item)) {
        item.forEach((v, j) => {
          item[j] = castToNumber.call(this, v);
        });
      } else {
        val[type][i] = castToNumber.call(this, item);
      }
    });
  } else if (val.$geometry) {
    cast$geometry(val, this);
  }
 
  return val;
}
 
function cast$geoIntersects(val) {
  const geo = val.$geometry;
  if (!geo) {
    return;
  }
 
  cast$geometry(val, this);
  return val;
}
 
function _castMinMaxDistance(self, val) {
  if (val.$maxDistance) {
    val.$maxDistance = castToNumber.call(self, val.$maxDistance);
  }
  if (val.$minDistance) {
    val.$minDistance = castToNumber.call(self, val.$minDistance);
  }
}