Value.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. klass.coerceToInt =
  34. klass.coerceToLong =
  35. klass.coerceToDouble =
  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. // CW TODO for now, only compare like values
  109. if (lt !== rt) throw new Error("can't compare values of BSON types [" + lt + " " + l.constructor.name + "] and [" + rt + ":" + r.constructor.name + "]; code 16016");
  110. // Compare everything else
  111. switch (lt) {
  112. case "number":
  113. throw new Error("number types should have been handled earlier!");
  114. case "string":
  115. return klass.cmp(l,r);
  116. case "boolean":
  117. return l == r ? 0 : l ? 1 : -1;
  118. 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)
  119. case "null":
  120. return 0;
  121. case "object":
  122. if (l instanceof Array) {
  123. for (var i = 0, ll = l.length, rl = r.length; true ; ++i) {
  124. if (i > ll) {
  125. if (i > rl) return 0; // arrays are same length
  126. return -1; // left array is shorter
  127. }
  128. if (i > rl) return 1; // right array is shorter
  129. var cmp = Value.compare(l[i], r[i]);
  130. if (cmp !== 0) return cmp;
  131. }
  132. throw new Error("logic error in Value.compare for Array types!");
  133. }
  134. if (l instanceof Date) return klass.cmp(l,r);
  135. if (l instanceof RegExp) return klass.cmp(l,r);
  136. if (Document === undefined) Document = require("./Document"); //TODO: a dirty hack; need to investigate and clean up
  137. return Document.compare(l, r);
  138. default:
  139. throw new Error("unhandled left hand type:" + lt);
  140. }
  141. };
  142. //SKIPPED: hash_combine
  143. //SKIPPED: getWidestNumeric
  144. //SKIPPED: getApproximateSize
  145. //SKIPPED: toString
  146. //SKIPPED: operator <<
  147. //SKIPPED: serializeForSorter
  148. //SKIPPED: deserializeForSorter
  149. /**
  150. * Takes an array and removes items and adds them to returned array.
  151. * @method consume
  152. * @static
  153. * @param consumed {Array} The array to be copied, emptied.
  154. **/
  155. klass.consume = function consume(consumed) {
  156. return consumed.splice(0);
  157. };
  158. //NOTE: DEVIATION FROM MONGO: these are inlined using the below code or perhaps not relevant since we are not boxing
  159. // missing(val): val == undefined
  160. // nullish(val): val == null || val == undefined
  161. // numeric(val): typeof val == "number"
  162. // getArrayLength(arr): arr.length
  163. // getString(val): val.toString() //NOTE: same for getStringData(val) I think
  164. // getOid
  165. // getBool
  166. // getDate
  167. // getTimestamp
  168. // getRegex(re): re.source
  169. // getRegexFlags(re): re.toString().slice(-re.toString().lastIndexOf('/') + 2)
  170. // getSymbol
  171. // getCode
  172. // getInt
  173. // getLong
  174. //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
  175. //SKIPPED: getType -- need this or something like it? would be some more stuff from bsontypes
  176. // from bsontypes
  177. klass.canonicalize = function canonicalize(x) {
  178. var xType = typeof(x);
  179. if (xType == "object") xType = x === null ? "null" : x.constructor.name;
  180. switch (xType) {
  181. case "MinKey":
  182. return -1;
  183. case "MaxKey":
  184. return 127;
  185. case "EOO":
  186. case "undefined":
  187. case undefined:
  188. return 0;
  189. case "jstNULL":
  190. case "null":
  191. case "Null":
  192. return 5;
  193. case "NumberDouble":
  194. case "NumberInt":
  195. case "NumberLong":
  196. case "number":
  197. return 10;
  198. case "Symbol":
  199. case "string":
  200. return 15;
  201. case "Object":
  202. return 20;
  203. case "Array":
  204. return 25;
  205. case "Binary":
  206. return 30;
  207. case "ObjectId":
  208. return 35;
  209. case "ObjectID":
  210. return 35;
  211. case "boolean":
  212. case "Boolean":
  213. return 40;
  214. case "Date":
  215. case "Timestamp":
  216. return 45;
  217. case "RegEx":
  218. case "RegExp":
  219. return 50;
  220. case "DBRef":
  221. return 55;
  222. case "Code":
  223. return 60;
  224. case "CodeWScope":
  225. return 65;
  226. default:
  227. // Default value for Object
  228. return 20;
  229. }
  230. };