text.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. const CastError = require('../../error/cast');
  3. const castBoolean = require('../../cast/boolean');
  4. const castString = require('../../cast/string');
  5. /*!
  6. * Casts val to an object suitable for `$text`. Throws an error if the object
  7. * can't be casted.
  8. *
  9. * @param {Any} val value to cast
  10. * @param {String} [path] path to associate with any errors that occured
  11. * @return {Object} casted object
  12. * @see https://docs.mongodb.com/manual/reference/operator/query/text/
  13. * @api private
  14. */
  15. module.exports = function(val, path) {
  16. if (val == null || typeof val !== 'object') {
  17. throw new CastError('$text', val, path);
  18. }
  19. if (val.$search != null) {
  20. val.$search = castString(val.$search, path + '.$search');
  21. }
  22. if (val.$language != null) {
  23. val.$language = castString(val.$language, path + '.$language');
  24. }
  25. if (val.$caseSensitive != null) {
  26. val.$caseSensitive = castBoolean(val.$caseSensitive,
  27. path + '.$castSensitive');
  28. }
  29. if (val.$diacriticSensitive != null) {
  30. val.$diacriticSensitive = castBoolean(val.$diacriticSensitive,
  31. path + '.$diacriticSensitive');
  32. }
  33. return val;
  34. };