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