index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { isPlainObject, isFunction } from "utils/validate";
  2. /**
  3. * 使用某个组件上的作用域插槽
  4. * @param {VueComponent} inject
  5. * @param {String} slotName
  6. * @param {Node} defaultElement
  7. * @param {Object} props
  8. */
  9. export function useScopedSlot(slot, def, props) {
  10. return slot ? slot(props) : def;
  11. }
  12. export function padZero(val) {
  13. return val < 10 ? `0${val}` : val;
  14. }
  15. export function hoursTimeFormat(t) {
  16. const date = new Date(t);
  17. const nowDate = new Date();
  18. const Y = t => {
  19. return t.getFullYear();
  20. };
  21. const MD = t => {
  22. return `${t.getMonth() + 1}-${t.getDate()}`;
  23. };
  24. const dateY = Y(date);
  25. const nowDateY = Y(nowDate);
  26. let format;
  27. if (dateY !== nowDateY) {
  28. format = "y年m月d日 h:i";
  29. } else if (`${dateY}-${MD(date)}` === `${nowDateY}-${MD(nowDate)}`) {
  30. format = "h:i";
  31. } else {
  32. format = "m月d日 h:i";
  33. }
  34. return timeFormat(t, format);
  35. }
  36. export function timeFormat(t, format) {
  37. if (!format) format = "y-m-d h:i:s";
  38. if (t) t = new Date(t);
  39. else t = new Date();
  40. const formatArr = [
  41. t.getFullYear().toString(),
  42. padZero((t.getMonth() + 1).toString()),
  43. padZero(t.getDate().toString()),
  44. padZero(t.getHours().toString()),
  45. padZero(t.getMinutes().toString()),
  46. padZero(t.getSeconds().toString())
  47. ];
  48. const reg = "ymdhis";
  49. for (let i = 0; i < formatArr.length; i++) {
  50. format = format.replace(reg.charAt(i), formatArr[i]);
  51. }
  52. return format;
  53. }
  54. export function fastDone(event, callback) {
  55. if (isFunction(event)) {
  56. event(() => {
  57. callback();
  58. });
  59. } else {
  60. callback();
  61. }
  62. }
  63. /**
  64. * 获取数组相交的值组成新数组
  65. * @param {Array} a
  66. * @param {Array} b
  67. */
  68. export function arrayIntersect(a, b) {
  69. return a.filter(x => b.includes(x));
  70. }
  71. export function error(text) {
  72. throw new Error(text);
  73. }
  74. export function cloneDeep(obj) {
  75. const newobj = { ...obj };
  76. for (const key in newobj) {
  77. const val = newobj[key];
  78. if (isPlainObject(val)) {
  79. newobj[key] = cloneDeep(val);
  80. }
  81. }
  82. return newobj;
  83. }
  84. export function mergeDeep(o1, o2) {
  85. for (const key in o2) {
  86. if (isPlainObject(o1[key])) {
  87. o1[key] = mergeDeep(o1[key], o2[key]);
  88. } else {
  89. o1[key] = o2[key];
  90. }
  91. }
  92. return o1;
  93. }
  94. export function toEmojiName(str) {
  95. return str.replace(/<img emoji-name=\"([^\"]*?)\" [^>]*>/gi, "[!$1]");
  96. }
  97. export function formatByte(value) {
  98. if (null == value || value == "") {
  99. return "0 Bytes";
  100. }
  101. var unitArr = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"];
  102. var index = 0;
  103. var srcsize = parseFloat(value);
  104. index = Math.floor(Math.log(srcsize) / Math.log(1024));
  105. var size = srcsize / Math.pow(1024, index);
  106. size = parseFloat(size.toFixed(2));
  107. return size + unitArr[index];
  108. }
  109. export function generateUUID() {
  110. var d = new Date().getTime();
  111. if (window.performance && typeof window.performance.now === "function") {
  112. d += performance.now(); //use high-precision timer if available
  113. }
  114. var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(
  115. c
  116. ) {
  117. var r = (d + Math.random() * 16) % 16 | 0;
  118. d = Math.floor(d / 16);
  119. return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
  120. });
  121. return uuid;
  122. }