FieldPathExpression.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var FieldPathExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /** Create a field path expression. Evaluation will extract the value
  4. | associated with the given field path from the source document.
  5. | @param fieldPath the field path string, without any leading document indicator
  6. **/
  7. var klass = function FieldPathExpression(path){
  8. if(arguments.length !== 1) throw new Error("args expected: path");
  9. this.path = new FieldPath(path);
  10. }, base = require("./Expression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  11. // DEPENDENCIES
  12. var FieldPath = require("../FieldPath");
  13. // PROTOTYPE MEMBERS
  14. proto.evaluate = function evaluate(obj){
  15. return this._evaluatePath(obj, 0, this.path.fields.length);
  16. };
  17. /** Internal implementation of evaluate(), used recursively.
  18. | The internal implementation doesn't just use a loop because of the
  19. | possibility that we need to skip over an array. If the path is "a.b.c",
  20. | and a is an array, then we fan out from there, and traverse "b.c" for each
  21. | element of a:[...]. This requires that a be an array of objects in order
  22. | to navigate more deeply.
  23. | @param index current path field index to extract
  24. | @param pathLength maximum number of fields on field path
  25. | @param pDocument current document traversed to (not the top-level one)
  26. | @returns the field found; could be an array
  27. **/
  28. proto._evaluatePath = function _evaluatePath(obj, i, len){
  29. var fieldName = this.path.fields[i],
  30. field = obj[fieldName];
  31. // if the field doesn't exist, quit with an undefined value
  32. if (field === undefined) return undefined;
  33. // if we've hit the end of the path, stop
  34. if (++i >= len) return field;
  35. // We're diving deeper. If the value was null, return null
  36. if(field === null) return undefined;
  37. if (field.constructor === Object) {
  38. return this._evaluatePath(field, i, len);
  39. } else if (Array.isArray(field)) {
  40. var results = [];
  41. for (var i2 = 0, l2 = field.length; i2 < l2; i2++) {
  42. var subObj = field[i2],
  43. subObjType = typeof(subObj);
  44. if (subObjType === "undefined" || subObj === null) {
  45. results.push(subObj);
  46. } else if (subObj.constructor === Object) {
  47. results.push(this._evaluatePath(subObj, i, len));
  48. } else {
  49. throw new Error("the element '" + fieldName + "' along the dotted path '" + this.path.getPath() + "' is not an object, and cannot be navigated.; code 16014");
  50. }
  51. }
  52. return results;
  53. }
  54. return undefined;
  55. };
  56. proto.optimize = function(){
  57. return this;
  58. };
  59. proto.addDependencies = function addDependencies(deps){
  60. deps.push(this.path.getPath());
  61. return deps;
  62. };
  63. proto.toJson = function toJson(){
  64. return this.path.getPath(true);
  65. };
  66. //TODO: proto.addToBsonObj = ...?
  67. //TODO: proto.addToBsonArray = ...?
  68. return klass;
  69. })();