SEARCH.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.transformReply = exports.transformArguments = exports.IS_READ_ONLY = exports.FIRST_KEY_INDEX = void 0;
  4. const _1 = require(".");
  5. exports.FIRST_KEY_INDEX = 1;
  6. exports.IS_READ_ONLY = true;
  7. function transformArguments(index, query, options) {
  8. return (0, _1.pushSearchOptions)(['FT.SEARCH', index, query], options);
  9. }
  10. exports.transformArguments = transformArguments;
  11. function transformReply(reply, withoutDocuments) {
  12. const documents = [];
  13. let i = 1;
  14. while (i < reply.length) {
  15. documents.push({
  16. id: reply[i++],
  17. value: withoutDocuments ? Object.create(null) : documentValue(reply[i++])
  18. });
  19. }
  20. return {
  21. total: reply[0],
  22. documents
  23. };
  24. }
  25. exports.transformReply = transformReply;
  26. function documentValue(tuples) {
  27. const message = Object.create(null);
  28. let i = 0;
  29. while (i < tuples.length) {
  30. const key = tuples[i++], value = tuples[i++];
  31. if (key === '$') { // might be a JSON reply
  32. try {
  33. Object.assign(message, JSON.parse(value));
  34. continue;
  35. }
  36. catch {
  37. // set as a regular property if not a valid JSON
  38. }
  39. }
  40. message[key] = value;
  41. }
  42. return message;
  43. }