sort.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.formatSort = void 0;
  4. const error_1 = require("./error");
  5. /** @internal */
  6. function prepareDirection(direction = 1) {
  7. const value = `${direction}`.toLowerCase();
  8. if (isMeta(direction))
  9. return direction;
  10. switch (value) {
  11. case 'ascending':
  12. case 'asc':
  13. case '1':
  14. return 1;
  15. case 'descending':
  16. case 'desc':
  17. case '-1':
  18. return -1;
  19. default:
  20. throw new error_1.MongoInvalidArgumentError(`Invalid sort direction: ${JSON.stringify(direction)}`);
  21. }
  22. }
  23. /** @internal */
  24. function isMeta(t) {
  25. return typeof t === 'object' && t != null && '$meta' in t && typeof t.$meta === 'string';
  26. }
  27. /** @internal */
  28. function isPair(t) {
  29. if (Array.isArray(t) && t.length === 2) {
  30. try {
  31. prepareDirection(t[1]);
  32. return true;
  33. }
  34. catch (e) {
  35. return false;
  36. }
  37. }
  38. return false;
  39. }
  40. function isDeep(t) {
  41. return Array.isArray(t) && Array.isArray(t[0]);
  42. }
  43. function isMap(t) {
  44. return t instanceof Map && t.size > 0;
  45. }
  46. /** @internal */
  47. function pairToMap(v) {
  48. return new Map([[`${v[0]}`, prepareDirection([v[1]])]]);
  49. }
  50. /** @internal */
  51. function deepToMap(t) {
  52. const sortEntries = t.map(([k, v]) => [`${k}`, prepareDirection(v)]);
  53. return new Map(sortEntries);
  54. }
  55. /** @internal */
  56. function stringsToMap(t) {
  57. const sortEntries = t.map(key => [`${key}`, 1]);
  58. return new Map(sortEntries);
  59. }
  60. /** @internal */
  61. function objectToMap(t) {
  62. const sortEntries = Object.entries(t).map(([k, v]) => [
  63. `${k}`,
  64. prepareDirection(v)
  65. ]);
  66. return new Map(sortEntries);
  67. }
  68. /** @internal */
  69. function mapToMap(t) {
  70. const sortEntries = Array.from(t).map(([k, v]) => [
  71. `${k}`,
  72. prepareDirection(v)
  73. ]);
  74. return new Map(sortEntries);
  75. }
  76. /** converts a Sort type into a type that is valid for the server (SortForCmd) */
  77. function formatSort(sort, direction) {
  78. if (sort == null)
  79. return undefined;
  80. if (typeof sort === 'string')
  81. return new Map([[sort, prepareDirection(direction)]]);
  82. if (typeof sort !== 'object') {
  83. throw new error_1.MongoInvalidArgumentError(`Invalid sort format: ${JSON.stringify(sort)} Sort must be a valid object`);
  84. }
  85. if (!Array.isArray(sort)) {
  86. return isMap(sort) ? mapToMap(sort) : Object.keys(sort).length ? objectToMap(sort) : undefined;
  87. }
  88. if (!sort.length)
  89. return undefined;
  90. if (isDeep(sort))
  91. return deepToMap(sort);
  92. if (isPair(sort))
  93. return pairToMap(sort);
  94. return stringsToMap(sort);
  95. }
  96. exports.formatSort = formatSort;
  97. //# sourceMappingURL=sort.js.map