ParsedDeps.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. /**
  3. * This class is designed to quickly extract the needed fields into a Document.
  4. * It should only be created by a call to DepsTracker.toParsedDeps.
  5. *
  6. * @class ParsedDeps
  7. * @namespace mungedb-aggregate.pipeline
  8. * @module mungedb-aggregate
  9. * @constructor
  10. * @param {Object} fields The fields needed in a Document
  11. */
  12. var ParsedDeps = module.exports = function ParsedDeps(fields) {
  13. this._fields = fields;
  14. }, klass = ParsedDeps, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  15. var Value = require("./Value");
  16. /**
  17. * Extracts fields from the input into a new Document, based on the caller.
  18. * @method extractFields
  19. * @param {Object} input The JSON object to extract from
  20. * @return {Document}
  21. */
  22. proto.extractFields = function extractFields(input) {
  23. return proto._documentHelper(input, this._fields);
  24. };
  25. /**
  26. * Handles array-type values for extractFields()
  27. * Mutually recursive with arrayHelper
  28. * @method _arrayHelper
  29. * @private
  30. * @param {Object} array Array to iterate over
  31. * @param {Object} neededFields
  32. * @return {Array}
  33. */
  34. proto._arrayHelper = function _arrayHelper(array, neededFields) {
  35. var values = [];
  36. for (var i = 0, l = array.length; i < l; i++) {
  37. var jsonElement = array[i];
  38. if (Value.getType(jsonElement) === "Object") {
  39. var sub = this._documentHelper(jsonElement, neededFields);
  40. values.push(sub);
  41. }
  42. if (Value.getType(jsonElement) === "Array") {
  43. values.push(this._arrayHelper(jsonElement, neededFields));
  44. }
  45. }
  46. return values;
  47. };
  48. /**
  49. * Handles object-type values for extractFields()
  50. * @method _documentHelper
  51. * @private
  52. * @param {Object} json Object to iterate over and filter
  53. * @param {Object} neededFields Fields to not exclude
  54. * @return {Document}
  55. */
  56. proto._documentHelper = function _documentHelper(json, neededFields) {
  57. var md = {};
  58. for (var fieldName in json) { //jshint ignore:line
  59. var jsonElement = json[fieldName],
  60. isNeeded = neededFields[fieldName];
  61. if (isNeeded === undefined)
  62. continue;
  63. if (typeof isNeeded === "boolean") {
  64. md[fieldName] = jsonElement;
  65. continue;
  66. }
  67. if (!(isNeeded instanceof Object)) throw new Error("dassert failure");
  68. if (Value.getType(jsonElement) === "Object") {
  69. var sub = this._documentHelper(jsonElement, isNeeded);
  70. md[fieldName] = sub;
  71. }
  72. if (Value.getType(jsonElement) === "Array") {
  73. md[fieldName] = this._arrayHelper(jsonElement, isNeeded);
  74. }
  75. }
  76. return md;
  77. };