common.js 2.9 KB

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