isExclusive.js 809 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const isDefiningProjection = require('./isDefiningProjection');
  3. /*!
  4. * ignore
  5. */
  6. module.exports = function isExclusive(projection) {
  7. if (projection == null) {
  8. return null;
  9. }
  10. const keys = Object.keys(projection);
  11. let ki = keys.length;
  12. let exclude = null;
  13. if (ki === 1 && keys[0] === '_id') {
  14. exclude = !projection._id;
  15. } else {
  16. while (ki--) {
  17. // Does this projection explicitly define inclusion/exclusion?
  18. // Explicitly avoid `$meta` and `$slice`
  19. const key = keys[ki];
  20. if (key !== '_id' && isDefiningProjection(projection[key])) {
  21. exclude = (projection[key] != null && typeof projection[key] === 'object') ?
  22. isExclusive(projection[key]) :
  23. !projection[key];
  24. break;
  25. }
  26. }
  27. }
  28. return exclude;
  29. };