common.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. const GHz = speed / 1000;
  25. return GHz.toFixed(2) + " GHz";
  26. }
  27. function cpuCoreFormat(cores) {
  28. if (cores === 1) {
  29. return "1 Core";
  30. } else {
  31. return cores + " Cores";
  32. }
  33. }
  34. function base64(str) {
  35. return Base64.encode(str);
  36. }
  37. function safeBase64(str) {
  38. return base64(str)
  39. .replace(/\+/g, '-')
  40. .replace(/=/g, '')
  41. .replace(/\//g, '_');
  42. }
  43. function formatSecond(second) {
  44. if (second < 60) {
  45. return second.toFixed(0) + ' s';
  46. } else if (second < 3600) {
  47. return (second / 60).toFixed(0) + ' m';
  48. } else if (second < 3600 * 24) {
  49. return (second / 3600).toFixed(0) + ' h';
  50. } else {
  51. return (second / 3600 / 24).toFixed(0) + ' d';
  52. }
  53. }
  54. function addZero(num) {
  55. if (num < 10) {
  56. return "0" + num;
  57. } else {
  58. return num;
  59. }
  60. }
  61. function toFixed(num, n) {
  62. n = Math.pow(10, n);
  63. return Math.round(num * n) / n;
  64. }
  65. function debounce(fn, delay) {
  66. var timeoutID = null;
  67. return function () {
  68. clearTimeout(timeoutID);
  69. var args = arguments;
  70. var that = this;
  71. timeoutID = setTimeout(function () {
  72. fn.apply(that, args);
  73. }, delay);
  74. };
  75. }
  76. function getCookie(cname) {
  77. let name = cname + '=';
  78. let ca = document.cookie.split(';');
  79. for (let i = 0; i < ca.length; i++) {
  80. let c = ca[i];
  81. while (c.charAt(0) == ' ') {
  82. c = c.substring(1);
  83. }
  84. if (c.indexOf(name) == 0) {
  85. // decode cookie value only
  86. return decodeURIComponent(c.substring(name.length, c.length));
  87. }
  88. }
  89. return '';
  90. }
  91. function setCookie(cname, cvalue, exdays) {
  92. const d = new Date();
  93. d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
  94. let expires = 'expires=' + d.toUTCString();
  95. // encode cookie value
  96. document.cookie = cname + '=' + encodeURIComponent(cvalue) + ';' + expires + ';path=/';
  97. }
  98. function usageColor(data, threshold, total) {
  99. switch (true) {
  100. case data === null:
  101. return 'blue';
  102. case total <= 0:
  103. return 'blue';
  104. case data < total - threshold:
  105. return 'cyan';
  106. case data < total:
  107. return 'orange';
  108. default:
  109. return 'red';
  110. }
  111. }
  112. function doAllItemsExist(array1, array2) {
  113. for (let i = 0; i < array1.length; i++) {
  114. if (!array2.includes(array1[i])) {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }