isExclusive.js 874 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. const isDefiningProjection = require('./isDefiningProjection');
  3. const isPOJO = require('../isPOJO');
  4. /*!
  5. * ignore
  6. */
  7. module.exports = function isExclusive(projection) {
  8. if (projection == null) {
  9. return null;
  10. }
  11. const keys = Object.keys(projection);
  12. let exclude = null;
  13. if (keys.length === 1 && keys[0] === '_id') {
  14. exclude = !projection._id;
  15. } else {
  16. for (let ki = 0; ki < keys.length; ++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 = isPOJO(projection[key]) ?
  22. (isExclusive(projection[key]) ?? exclude) :
  23. !projection[key];
  24. if (exclude != null) {
  25. break;
  26. }
  27. }
  28. }
  29. }
  30. return exclude;
  31. };