sort.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.formatSort = formatSort;
  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 {
  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. function isReadonlyArray(value) {
  47. return Array.isArray(value);
  48. }
  49. /** @internal */
  50. function pairToMap(v) {
  51. return new Map([[`${v[0]}`, prepareDirection([v[1]])]]);
  52. }
  53. /** @internal */
  54. function deepToMap(t) {
  55. const sortEntries = t.map(([k, v]) => [`${k}`, prepareDirection(v)]);
  56. return new Map(sortEntries);
  57. }
  58. /** @internal */
  59. function stringsToMap(t) {
  60. const sortEntries = t.map(key => [`${key}`, 1]);
  61. return new Map(sortEntries);
  62. }
  63. /** @internal */
  64. function objectToMap(t) {
  65. const sortEntries = Object.entries(t).map(([k, v]) => [
  66. `${k}`,
  67. prepareDirection(v)
  68. ]);
  69. return new Map(sortEntries);
  70. }
  71. /** @internal */
  72. function mapToMap(t) {
  73. const sortEntries = Array.from(t).map(([k, v]) => [
  74. `${k}`,
  75. prepareDirection(v)
  76. ]);
  77. return new Map(sortEntries);
  78. }
  79. /** converts a Sort type into a type that is valid for the server (SortForCmd) */
  80. function formatSort(sort, direction) {
  81. if (sort == null)
  82. return undefined;
  83. if (typeof sort === 'string')
  84. return new Map([[sort, prepareDirection(direction)]]); // 'fieldName'
  85. if (typeof sort !== 'object') {
  86. throw new error_1.MongoInvalidArgumentError(`Invalid sort format: ${JSON.stringify(sort)} Sort must be a valid object`);
  87. }
  88. if (!isReadonlyArray(sort)) {
  89. if (isMap(sort))
  90. return mapToMap(sort); // Map<fieldName, SortDirection>
  91. if (Object.keys(sort).length)
  92. return objectToMap(sort); // { [fieldName: string]: SortDirection }
  93. return undefined;
  94. }
  95. if (!sort.length)
  96. return undefined;
  97. if (isDeep(sort))
  98. return deepToMap(sort); // [ [fieldName, sortDir], [fieldName, sortDir] ... ]
  99. if (isPair(sort))
  100. return pairToMap(sort); // [ fieldName, sortDir ]
  101. return stringsToMap(sort); // [ fieldName, fieldName ]
  102. }
  103. //# sourceMappingURL=sort.js.map