Value.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. "use strict";
  2. /**
  3. * Represents a `Value` (i.e., an `Object`) in `mongo` but in `munge` this is only a set of static helpers since we treat all `Object`s like `Value`s.
  4. * @class Value
  5. * @namespace mungedb-aggregate.pipeline
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var Value = module.exports = function Value(){
  10. if(this.constructor == Value) throw new Error("Never create instances of this! Use the static helpers only.");
  11. }, klass = Value, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  12. var Document; // loaded lazily below //TODO: a dirty hack; need to investigate and clean up
  13. //SKIPPED: ValueStorage -- probably not required; use JSON?
  14. //SKIPPED: createIntOrLong -- not required; use Number
  15. //SKIPPED: operator <Array>[] -- not required; use arr[i]
  16. //SKIPPED: operator <Object>[] -- not required; use obj[key]
  17. //SKIPPED: operator << -- not required
  18. //SKIPPED: addToBsonObj -- not required; use obj[key] = <val>
  19. //SKIPPED: addToBsonArray -- not required; use arr.push(<val>)
  20. /** Coerce a value to a bool using BSONElement::trueValue() rules.
  21. * Some types unsupported. SERVER-6120
  22. * @method coerceToBool
  23. * @static
  24. */
  25. klass.coerceToBool = function coerceToBool(value) {
  26. if (typeof(value) == "string") return true;
  27. return !!value; // including null or undefined
  28. };
  29. /** Coercion operators to extract values with fuzzy type logic.
  30. * These currently assert if called on an unconvertible type.
  31. * TODO: decided how to handle unsupported types.
  32. */
  33. //SKIPPED: coerceToInt -- use coerceToNumber
  34. //SKIPPED: coerceToLong -- use coerceToNumber
  35. //SKIPPED: coerceToDouble -- use coerceToNumber
  36. klass.coerceToNumber = function coerceToNumber(value) {
  37. if (value === null) return 0;
  38. switch (typeof(value)) {
  39. case "undefined":
  40. return 0;
  41. case "number":
  42. return value;
  43. case "object":
  44. switch (value.constructor.name) {
  45. case "Long":
  46. return parseInt(value.toString(), 10);
  47. case "Double":
  48. return parseFloat(value.value, 10);
  49. default:
  50. throw new Error("can't convert from BSON type " + value.constructor.name + " to int; codes 16003, 16004, 16005");
  51. }
  52. return value;
  53. default:
  54. throw new Error("can't convert from BSON type " + typeof(value) + " to int; codes 16003, 16004, 16005");
  55. }
  56. };
  57. klass.coerceToDate = function coerceToDate(value) {
  58. //TODO: Support Timestamp BSON type?
  59. if (value instanceof Date) return value;
  60. throw new Error("can't convert from BSON type " + typeof(value) + " to Date; uassert code 16006");
  61. };
  62. //SKIPPED: coerceToTimeT -- not required; just use Date
  63. //SKIPPED: coerceToTm -- not required; just use Date
  64. //SKIPPED: tmToISODateString -- not required; just use Date
  65. klass.coerceToString = function coerceToString(value) {
  66. if (value === null) return "";
  67. switch (typeof(value)) {
  68. case "undefined":
  69. return "";
  70. case "number":
  71. return value.toString();
  72. case "string":
  73. return value;
  74. default:
  75. throw new Error("can't convert from BSON type " + typeof(value) + " to String; uassert code 16007");
  76. }
  77. };
  78. //SKIPPED: coerceToTimestamp
  79. /**
  80. * Helper function for Value.compare
  81. * @method cmp
  82. * @static
  83. */
  84. klass.cmp = function cmp(l, r){
  85. return l < r ? -1 : l > r ? 1 : 0;
  86. };
  87. /** Compare two Values.
  88. * @static
  89. * @method compare
  90. * @returns an integer less than zero, zero, or an integer greater than zero, depending on whether lhs < rhs, lhs == rhs, or lhs > rhs
  91. * Warning: may return values other than -1, 0, or 1
  92. */
  93. klass.compare = function compare(l, r) {
  94. //NOTE: deviation from mongo code: we have to do some coercing for null "types" because of javascript
  95. var lt = l === null ? "null" : typeof(l),
  96. rt = r === null ? "null" : typeof(r),
  97. ret;
  98. // NOTE: deviation from mongo code: javascript types do not work quite the same, so for proper results we always canonicalize, and we don't need the "speed" hack
  99. ret = (klass.cmp(klass.canonicalize(l), klass.canonicalize(r)));
  100. if(ret !== 0) return ret;
  101. // Numbers
  102. if (lt === "number" && rt === "number"){
  103. //NOTE: deviation from Mongo code: they handle NaN a bit differently
  104. if (isNaN(l)) return isNaN(r) ? 0 : -1;
  105. if (isNaN(r)) return 1;
  106. return klass.cmp(l,r);
  107. }
  108. // Compare MinKey and MaxKey cases
  109. if(l.constructor && l.constructor.name in {'MinKey':1,'MaxKey':1} ){
  110. if(l.constructor.name == r.constructor.name) {
  111. return 0;
  112. } else if (l.constructor.name === 'MinKey'){
  113. return -1;
  114. } else {
  115. return 1; // Must be MaxKey, which is greater than everything but MaxKey (which r cannot be)
  116. }
  117. }
  118. // hack: These should really get converted to their BSON type ids and then compared, we use int vs object in queries
  119. if (lt === "number" && rt === "object"){
  120. return -1;
  121. } else if (lt === "object" && rt === "number") {
  122. return 1;
  123. }
  124. // CW TODO for now, only compare like values
  125. if (lt !== rt) throw new Error("can't compare values of BSON types [" + lt + " " + l.constructor.name + "] and [" + rt + ":" + r.constructor.name + "]; code 16016");
  126. // Compare everything else
  127. switch (lt) {
  128. case "number":
  129. throw new Error("number types should have been handled earlier!");
  130. case "string":
  131. return klass.cmp(l,r);
  132. case "boolean":
  133. return l == r ? 0 : l ? 1 : -1;
  134. case "undefined": //NOTE: deviation from mongo code: we are comparing null to null or undefined to undefined (otherwise the ret stuff above would have caught it)
  135. case "null":
  136. return 0;
  137. case "object":
  138. if (l instanceof Array) {
  139. for (var i = 0, ll = l.length, rl = r.length; true ; ++i) {
  140. if (i > ll) {
  141. if (i > rl) return 0; // arrays are same length
  142. return -1; // left array is shorter
  143. }
  144. if (i > rl) return 1; // right array is shorter
  145. var cmp = Value.compare(l[i], r[i]);
  146. if (cmp !== 0) return cmp;
  147. }
  148. throw new Error("logic error in Value.compare for Array types!");
  149. }
  150. if (l instanceof Date) return klass.cmp(l,r);
  151. if (l instanceof RegExp) return klass.cmp(l,r);
  152. if (Document === undefined) Document = require("./Document"); //TODO: a dirty hack; need to investigate and clean up
  153. return Document.compare(l, r);
  154. default:
  155. throw new Error("unhandled left hand type:" + lt);
  156. }
  157. };
  158. //SKIPPED: hash_combine
  159. //SKIPPED: getWidestNumeric
  160. //SKIPPED: getApproximateSize
  161. //SKIPPED: toString
  162. //SKIPPED: operator <<
  163. //SKIPPED: serializeForSorter
  164. //SKIPPED: deserializeForSorter
  165. /**
  166. * Takes an array and removes items and adds them to returned array.
  167. * @method consume
  168. * @static
  169. * @param consumed {Array} The array to be copied, emptied.
  170. **/
  171. klass.consume = function consume(consumed) {
  172. return consumed.splice(0);
  173. };
  174. //NOTE: DEVIATION FROM MONGO: these are inlined using the below code or perhaps not relevant since we are not boxing
  175. // missing(val): val == undefined
  176. // nullish(val): val == null || val == undefined
  177. // numeric(val): typeof val == "number"
  178. // getArrayLength(arr): arr.length
  179. // getString(val): val.toString() //NOTE: same for getStringData(val) I think
  180. // getOid
  181. // getBool
  182. // getDate
  183. // getTimestamp
  184. // getRegex(re): re.source
  185. // getRegexFlags(re): re.toString().slice(-re.toString().lastIndexOf('/') + 2)
  186. // getSymbol
  187. // getCode
  188. // getInt
  189. // getLong
  190. //NOTE: also, because of this we are not throwing if the type does not match like the mongo code would but maybe that's okay
  191. //SKIPPED: getType -- need this or something like it? would be some more stuff from bsontypes
  192. // from bsontypes
  193. klass.canonicalize = function canonicalize(x) {
  194. var xType = typeof(x);
  195. if (xType == "object") xType = x === null ? "null" : x.constructor.name;
  196. switch (xType) {
  197. case "MinKey":
  198. return -1;
  199. case "MaxKey":
  200. return 127;
  201. case "EOO":
  202. case "undefined":
  203. case undefined:
  204. return 0;
  205. case "jstNULL":
  206. case "null":
  207. case "Null":
  208. return 5;
  209. case "NumberDouble":
  210. case "NumberInt":
  211. case "NumberLong":
  212. case "number":
  213. return 10;
  214. case "Symbol":
  215. case "string":
  216. return 15;
  217. case "Object":
  218. return 20;
  219. case "Array":
  220. return 25;
  221. case "Binary":
  222. return 30;
  223. case "ObjectId":
  224. return 35;
  225. case "ObjectID":
  226. return 35;
  227. case "boolean":
  228. case "Boolean":
  229. return 40;
  230. case "Date":
  231. case "Timestamp":
  232. return 45;
  233. case "RegEx":
  234. case "RegExp":
  235. return 50;
  236. case "DBRef":
  237. return 55;
  238. case "Code":
  239. return 60;
  240. case "CodeWScope":
  241. return 65;
  242. default:
  243. // Default value for Object
  244. return 20;
  245. }
  246. };