isExclusive.js 678 B

1234567891011121314151617181920212223242526272829303132
  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. if (keys[ki] !== '_id' && isDefiningProjection(projection[keys[ki]])) {
  20. exclude = !projection[keys[ki]];
  21. break;
  22. }
  23. }
  24. }
  25. return exclude;
  26. };