index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @description JS对象深度合并
  3. * @param {object} target 需要拷贝的对象
  4. * @param {object} source 拷贝的来源对象
  5. * @returns {object|boolean} 深度合并后的对象或者false(入参有不是对象)
  6. */
  7. export function deepMerge(target = {}, source = {}) {
  8. target = deepClone(target);
  9. if (typeof target !== 'object' || typeof source !== 'object') return false;
  10. for (const prop in source) {
  11. if (!source.hasOwnProperty(prop)) continue;
  12. if (prop in target) {
  13. if (typeof target[prop] !== 'object') {
  14. target[prop] = source[prop];
  15. } else if (typeof source[prop] !== 'object') {
  16. target[prop] = source[prop];
  17. } else if (target[prop].concat && source[prop].concat) {
  18. target[prop] = target[prop].concat(source[prop]);
  19. } else {
  20. target[prop] = deepMerge(target[prop], source[prop]);
  21. }
  22. } else {
  23. target[prop] = source[prop];
  24. }
  25. }
  26. return target;
  27. }
  28. /**
  29. * @description 去除空格
  30. * @param String str 需要去除空格的字符串
  31. * @param String pos both(左右)|left|right|all 默认both
  32. */
  33. function trim(str, pos = 'both') {
  34. str = String(str);
  35. if (pos == 'both') {
  36. return str.replace(/^\s+|\s+$/g, '');
  37. }
  38. if (pos == 'left') {
  39. return str.replace(/^\s*/, '');
  40. }
  41. if (pos == 'right') {
  42. return str.replace(/(\s*$)/g, '');
  43. }
  44. if (pos == 'all') {
  45. return str.replace(/\s+/g, '');
  46. }
  47. return str;
  48. }
  49. /**
  50. * @description 样式转换
  51. * 对象转字符串,或者字符串转对象
  52. * @param {object | string} customStyle 需要转换的目标
  53. * @param {String} target 转换的目的,object-转为对象,string-转为字符串
  54. * @returns {object|string}
  55. */
  56. export function addStyle(customStyle, target = 'object') {
  57. // 字符串转字符串,对象转对象情形,直接返回
  58. if (
  59. test.empty(customStyle) ||
  60. (typeof customStyle === 'object' && target === 'object') ||
  61. (target === 'string' && typeof customStyle === 'string')
  62. ) {
  63. return customStyle;
  64. }
  65. // 字符串转对象
  66. if (target === 'object') {
  67. // 去除字符串样式中的两端空格(中间的空格不能去掉,比如padding: 20px 0如果去掉了就错了),空格是无用的
  68. customStyle = trim(customStyle);
  69. // 根据";"将字符串转为数组形式
  70. const styleArray = customStyle.split(';');
  71. const style = {};
  72. // 历遍数组,拼接成对象
  73. for (let i = 0; i < styleArray.length; i++) {
  74. // 'font-size:20px;color:red;',如此最后字符串有";"的话,会导致styleArray最后一个元素为空字符串,这里需要过滤
  75. if (styleArray[i]) {
  76. const item = styleArray[i].split(':');
  77. style[trim(item[0])] = trim(item[1]);
  78. }
  79. }
  80. return style;
  81. }
  82. // 这里为对象转字符串形式
  83. let string = '';
  84. for (const i in customStyle) {
  85. // 驼峰转为中划线的形式,否则css内联样式,无法识别驼峰样式属性名
  86. const key = i.replace(/([A-Z])/g, '-$1').toLowerCase();
  87. string += `${key}:${customStyle[i]};`;
  88. }
  89. // 去除两端空格
  90. return trim(string);
  91. }
  92. /**
  93. * @description 进行延时,以达到可以简写代码的目的
  94. * @param {number} value 堵塞时间 单位ms 毫秒
  95. * @returns {Promise} 返回promise
  96. */
  97. export function sleep(value = 30) {
  98. return new Promise((resolve) => {
  99. setTimeout(() => {
  100. resolve();
  101. }, value);
  102. });
  103. }
  104. // 判断是图片还是视频
  105. export const checkIsImage = (url) => {
  106. // var link = new URL(url)
  107. // var path = link.pathname
  108. var extension = url.split('.').pop().toLowerCase()
  109. var imageExtensions = ['jpg', 'jpeg', 'gif', 'png', 'jfif']
  110. var videoExtensions = ['mp4', 'wmv', 'avi', 'mov']
  111. // 图片
  112. if (imageExtensions.includes(extension)) {
  113. return true
  114. }
  115. // 视频
  116. if (videoExtensions.includes(extension)) {
  117. return false
  118. }
  119. return null
  120. }
  121. export const generateUUID = (len = 32, firstU = true, radix = null) => {
  122. const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  123. const uuid = [];
  124. radix = radix || chars.length;
  125. if (len) {
  126. // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
  127. for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
  128. } else {
  129. let r;
  130. // rfc4122标准要求返回的uuid中,某些位为固定的字符
  131. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  132. uuid[14] = '4';
  133. for (let i = 0; i < 36; i++) {
  134. if (!uuid[i]) {
  135. r = 0 | (Math.random() * 16);
  136. uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
  137. }
  138. }
  139. }
  140. // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
  141. if (firstU) {
  142. uuid.shift();
  143. return `u${uuid.join('')}`;
  144. }
  145. return uuid.join('');
  146. }