You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.1 KiB

  1. /*!
  2. * Module requirements.
  3. */
  4. var castArraysOfNumbers = require('./helpers').castArraysOfNumbers;
  5. var castToNumber = require('./helpers').castToNumber;
  6. /*!
  7. * ignore
  8. */
  9. exports.cast$geoIntersects = cast$geoIntersects;
  10. exports.cast$near = cast$near;
  11. exports.cast$within = cast$within;
  12. function cast$near(val) {
  13. var SchemaArray = require('../array');
  14. if (Array.isArray(val)) {
  15. castArraysOfNumbers(val, this);
  16. return val;
  17. }
  18. _castMinMaxDistance(this, val);
  19. if (val && val.$geometry) {
  20. return cast$geometry(val, this);
  21. }
  22. return SchemaArray.prototype.castForQuery.call(this, val);
  23. }
  24. function cast$geometry(val, self) {
  25. switch (val.$geometry.type) {
  26. case 'Polygon':
  27. case 'LineString':
  28. case 'Point':
  29. castArraysOfNumbers(val.$geometry.coordinates, self);
  30. break;
  31. default:
  32. // ignore unknowns
  33. break;
  34. }
  35. _castMinMaxDistance(this, val);
  36. return val;
  37. }
  38. function cast$within(val) {
  39. _castMinMaxDistance(this, val);
  40. if (val.$box || val.$polygon) {
  41. var type = val.$box ? '$box' : '$polygon';
  42. val[type].forEach(function(arr) {
  43. if (!Array.isArray(arr)) {
  44. var msg = 'Invalid $within $box argument. '
  45. + 'Expected an array, received ' + arr;
  46. throw new TypeError(msg);
  47. }
  48. arr.forEach(function(v, i) {
  49. arr[i] = castToNumber.call(this, v);
  50. });
  51. });
  52. } else if (val.$center || val.$centerSphere) {
  53. type = val.$center ? '$center' : '$centerSphere';
  54. val[type].forEach(function(item, i) {
  55. if (Array.isArray(item)) {
  56. item.forEach(function(v, j) {
  57. item[j] = castToNumber.call(this, v);
  58. });
  59. } else {
  60. val[type][i] = castToNumber.call(this, item);
  61. }
  62. });
  63. } else if (val.$geometry) {
  64. cast$geometry(val, this);
  65. }
  66. return val;
  67. }
  68. function cast$geoIntersects(val) {
  69. var geo = val.$geometry;
  70. if (!geo) {
  71. return;
  72. }
  73. cast$geometry(val, this);
  74. return val;
  75. }
  76. function _castMinMaxDistance(self, val) {
  77. if (val.$maxDistance) {
  78. val.$maxDistance = castToNumber.call(self, val.$maxDistance);
  79. }
  80. if (val.$minDistance) {
  81. val.$minDistance = castToNumber.call(self, val.$minDistance);
  82. }
  83. }