sort-predicates.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. const { getAbbreviatedMimeType,
  3. getUriNameWithQuery,
  4. getUriHostPort,
  5. loadCauseString } = require("./request-utils");
  6. /**
  7. * Predicates used when sorting items.
  8. *
  9. * @param object first
  10. * The first item used in the comparison.
  11. * @param object second
  12. * The second item used in the comparison.
  13. * @return number
  14. * <0 to sort first to a lower index than second
  15. * =0 to leave first and second unchanged with respect to each other
  16. * >0 to sort second to a lower index than first
  17. */
  18. function waterfall(first, second) {
  19. return first.startedMillis - second.startedMillis;
  20. }
  21. function status(first, second) {
  22. return first.status == second.status
  23. ? first.startedMillis - second.startedMillis
  24. : first.status - second.status;
  25. }
  26. function method(first, second) {
  27. if (first.method == second.method) {
  28. return first.startedMillis - second.startedMillis;
  29. }
  30. return first.method > second.method ? 1 : -1;
  31. }
  32. function file(first, second) {
  33. let firstUrl = getUriNameWithQuery(first.url).toLowerCase();
  34. let secondUrl = getUriNameWithQuery(second.url).toLowerCase();
  35. if (firstUrl == secondUrl) {
  36. return first.startedMillis - second.startedMillis;
  37. }
  38. return firstUrl > secondUrl ? 1 : -1;
  39. }
  40. function domain(first, second) {
  41. let firstDomain = getUriHostPort(first.url).toLowerCase();
  42. let secondDomain = getUriHostPort(second.url).toLowerCase();
  43. if (firstDomain == secondDomain) {
  44. return first.startedMillis - second.startedMillis;
  45. }
  46. return firstDomain > secondDomain ? 1 : -1;
  47. }
  48. function cause(first, second) {
  49. let firstCause = loadCauseString(first.cause.type);
  50. let secondCause = loadCauseString(second.cause.type);
  51. if (firstCause == secondCause) {
  52. return first.startedMillis - second.startedMillis;
  53. }
  54. return firstCause > secondCause ? 1 : -1;
  55. }
  56. function type(first, second) {
  57. let firstType = getAbbreviatedMimeType(first.mimeType).toLowerCase();
  58. let secondType = getAbbreviatedMimeType(second.mimeType).toLowerCase();
  59. if (firstType == secondType) {
  60. return first.startedMillis - second.startedMillis;
  61. }
  62. return firstType > secondType ? 1 : -1;
  63. }
  64. function transferred(first, second) {
  65. return first.transferredSize - second.transferredSize;
  66. }
  67. function size(first, second) {
  68. return first.contentSize - second.contentSize;
  69. }
  70. exports.Sorters = {
  71. status,
  72. method,
  73. file,
  74. domain,
  75. cause,
  76. type,
  77. transferred,
  78. size,
  79. waterfall,
  80. };