isPathExcluded.js 798 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. const isDefiningProjection = require('./isDefiningProjection');
  3. /**
  4. * Determines if `path` is excluded by `projection`
  5. *
  6. * @param {Object} projection
  7. * @param {String} path
  8. * @return {Boolean}
  9. * @api private
  10. */
  11. module.exports = function isPathExcluded(projection, path) {
  12. if (projection == null) {
  13. return false;
  14. }
  15. if (path === '_id') {
  16. return projection._id === 0;
  17. }
  18. const paths = Object.keys(projection);
  19. let type = null;
  20. for (const _path of paths) {
  21. if (isDefiningProjection(projection[_path])) {
  22. type = projection[path] === 1 ? 'inclusive' : 'exclusive';
  23. break;
  24. }
  25. }
  26. if (type === 'inclusive') {
  27. return projection[path] !== 1;
  28. }
  29. if (type === 'exclusive') {
  30. return projection[path] === 0;
  31. }
  32. return false;
  33. };