FieldPath.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. /**
  3. * Constructor for field paths.
  4. *
  5. * The constructed object will have getPathLength() > 0.
  6. * Uassert if any component field names do not pass validation.
  7. *
  8. * @class FieldPath
  9. * @namespace mungedb-aggregate.pipeline
  10. * @module mungedb-aggregate
  11. * @constructor
  12. * @param fieldPath the dotted field path string or non empty pre-split vector.
  13. */
  14. var FieldPath = module.exports = function FieldPath(path) {
  15. var fieldNames = typeof path === "object" && typeof path.length === "number" ? path : path.split(".");
  16. if (fieldNames.length === 0) throw new Error("FieldPath cannot be constructed from an empty vector (String or Array).; massert code 16409");
  17. this.fieldNames = [];
  18. for (var i = 0, n = fieldNames.length; i < n; ++i) {
  19. this._pushFieldName(fieldNames[i]);
  20. }
  21. }, klass = FieldPath, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  22. klass.PREFIX = "$";
  23. /**
  24. * Get the full path.
  25. * @method getPath
  26. * @param fieldPrefix whether or not to include the field prefix
  27. * @returns the complete field path
  28. */
  29. proto.getPath = function getPath(fieldPrefix) {
  30. return (!!fieldPrefix ? FieldPath.PREFIX : "") + this.fieldNames.join(".");
  31. };
  32. //SKIPPED: writePath - merged into getPath
  33. /**
  34. * A FieldPath like this but missing the first element (useful for recursion). Precondition getPathLength() > 1.
  35. * @method tail
  36. */
  37. proto.tail = function tail() {
  38. return new FieldPath(this.fieldNames.slice(1));
  39. };
  40. /**
  41. * Get a particular path element from the path.
  42. * @method getFieldName
  43. * @param i the zero based index of the path element.
  44. * @returns the path element
  45. */
  46. proto.getFieldName = function getFieldName(i) { //TODO: eventually replace this with just using .fieldNames[i] directly
  47. return this.fieldNames[i];
  48. };
  49. klass._uassertValidFieldName = function _uassertValidFieldName(fieldName) {
  50. if (fieldName.length === 0) throw new Error("FieldPath field names may not be empty strings; code 15998");
  51. if (fieldName[0] === "$") throw new Error("FieldPath field names may not start with '$'; code 16410");
  52. if (fieldName.indexOf("\0") !== -1) throw new Error("FieldPath field names may not contain '\\0'; code 16411");
  53. if (fieldName.indexOf(".") !== -1) throw new Error("FieldPath field names may not contain '.'; code 16412");
  54. };
  55. proto._pushFieldName = function _pushFieldName(fieldName) {
  56. klass._uassertValidFieldName(fieldName);
  57. this.fieldNames.push(fieldName);
  58. };
  59. /**
  60. * Get the number of path elements in the field path.
  61. * @method getPathLength
  62. * @returns the number of path elements
  63. */
  64. proto.getPathLength = function getPathLength() {
  65. return this.fieldNames.length;
  66. };