common.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const ONE_KB = 1024;
  2. const ONE_MB = ONE_KB * 1024;
  3. const ONE_GB = ONE_MB * 1024;
  4. const ONE_TB = ONE_GB * 1024;
  5. const ONE_PB = ONE_TB * 1024;
  6. function sizeFormat(size) {
  7. if (size < 0) {
  8. return "0 B";
  9. } else if (size < ONE_KB) {
  10. return size.toFixed(0) + " B";
  11. } else if (size < ONE_MB) {
  12. return (size / ONE_KB).toFixed(2) + " KB";
  13. } else if (size < ONE_GB) {
  14. return (size / ONE_MB).toFixed(2) + " MB";
  15. } else if (size < ONE_TB) {
  16. return (size / ONE_GB).toFixed(2) + " GB";
  17. } else if (size < ONE_PB) {
  18. return (size / ONE_TB).toFixed(2) + " TB";
  19. } else {
  20. return (size / ONE_PB).toFixed(2) + " PB";
  21. }
  22. }
  23. function cpuSpeedFormat(speed) {
  24. if (speed > 1000) {
  25. const GHz = speed / 1000;
  26. return GHz.toFixed(2) + " GHz";
  27. } else {
  28. return speed.toFixed(2) + " MHz";
  29. }
  30. }
  31. function cpuCoreFormat(cores) {
  32. if (cores === 1) {
  33. return "1 Core";
  34. } else {
  35. return cores + " Cores";
  36. }
  37. }
  38. function base64(str) {
  39. return Base64.encode(str);
  40. }
  41. function safeBase64(str) {
  42. return base64(str)
  43. .replace(/\+/g, '-')
  44. .replace(/=/g, '')
  45. .replace(/\//g, '_');
  46. }
  47. function formatSecond(second) {
  48. if (second < 60) {
  49. return second.toFixed(0) + ' s';
  50. } else if (second < 3600) {
  51. return (second / 60).toFixed(0) + ' m';
  52. } else if (second < 3600 * 24) {
  53. return (second / 3600).toFixed(0) + ' h';
  54. } else {
  55. return (second / 3600 / 24).toFixed(0) + ' d';
  56. }
  57. }
  58. function addZero(num) {
  59. if (num < 10) {
  60. return "0" + num;
  61. } else {
  62. return num;
  63. }
  64. }
  65. function toFixed(num, n) {
  66. n = Math.pow(10, n);
  67. return Math.round(num * n) / n;
  68. }
  69. function debounce(fn, delay) {
  70. var timeoutID = null;
  71. return function () {
  72. clearTimeout(timeoutID);
  73. var args = arguments;
  74. var that = this;
  75. timeoutID = setTimeout(function () {
  76. fn.apply(that, args);
  77. }, delay);
  78. };
  79. }
  80. function getCookie(cname) {
  81. let name = cname + '=';
  82. let ca = document.cookie.split(';');
  83. for (let i = 0; i < ca.length; i++) {
  84. let c = ca[i];
  85. while (c.charAt(0) == ' ') {
  86. c = c.substring(1);
  87. }
  88. if (c.indexOf(name) == 0) {
  89. // decode cookie value only
  90. return decodeURIComponent(c.substring(name.length, c.length));
  91. }
  92. }
  93. return '';
  94. }
  95. function setCookie(cname, cvalue, exdays) {
  96. const d = new Date();
  97. d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
  98. let expires = 'expires=' + d.toUTCString();
  99. // encode cookie value
  100. document.cookie = cname + '=' + encodeURIComponent(cvalue) + ';' + expires + ';path=/';
  101. }
  102. function usageColor(data, threshold, total) {
  103. switch (true) {
  104. case data === null:
  105. return 'blue';
  106. case total <= 0:
  107. return 'blue';
  108. case data < total - threshold:
  109. return 'cyan';
  110. case data < total:
  111. return 'orange';
  112. default:
  113. return 'red';
  114. }
  115. }
  116. function doAllItemsExist(array1, array2) {
  117. for (let i = 0; i < array1.length; i++) {
  118. if (!array2.includes(array1[i])) {
  119. return false;
  120. }
  121. }
  122. return true;
  123. }
  124. function buildURL({ host, port, isTLS, base, path }) {
  125. if (!host || host.length === 0) host = window.location.hostname;
  126. if (!port || port.length === 0) port = window.location.port;
  127. if (isTLS === undefined) isTLS = window.location.protocol === "https:";
  128. const protocol = isTLS ? "https:" : "http:";
  129. port = String(port);
  130. if (port === "" || (isTLS && port === "443") || (!isTLS && port === "80")) {
  131. port = "";
  132. } else {
  133. port = `:${port}`;
  134. }
  135. return `${protocol}//${host}${port}${base}${path}`;
  136. }