common.js 2.9 KB

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