| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 | /** * @description JS对象深度合并 * @param {object} target 需要拷贝的对象 * @param {object} source 拷贝的来源对象 * @returns {object|boolean} 深度合并后的对象或者false(入参有不是对象) */export function deepMerge(target = {}, source = {}) {  target = deepClone(target);  if (typeof target !== 'object' || typeof source !== 'object') return false;  for (const prop in source) {    if (!source.hasOwnProperty(prop)) continue;    if (prop in target) {      if (typeof target[prop] !== 'object') {        target[prop] = source[prop];      } else if (typeof source[prop] !== 'object') {        target[prop] = source[prop];      } else if (target[prop].concat && source[prop].concat) {        target[prop] = target[prop].concat(source[prop]);      } else {        target[prop] = deepMerge(target[prop], source[prop]);      }    } else {      target[prop] = source[prop];    }  }  return target;}/** * @description 去除空格 * @param String str 需要去除空格的字符串 * @param String pos both(左右)|left|right|all 默认both */function trim(str, pos = 'both') {  str = String(str);  if (pos == 'both') {    return str.replace(/^\s+|\s+$/g, '');  }  if (pos == 'left') {    return str.replace(/^\s*/, '');  }  if (pos == 'right') {    return str.replace(/(\s*$)/g, '');  }  if (pos == 'all') {    return str.replace(/\s+/g, '');  }  return str;}/** * @description 样式转换 * 对象转字符串,或者字符串转对象 * @param {object | string} customStyle 需要转换的目标 * @param {String} target 转换的目的,object-转为对象,string-转为字符串 * @returns {object|string} */export function addStyle(customStyle, target = 'object') {  // 字符串转字符串,对象转对象情形,直接返回  if (    test.empty(customStyle) ||    (typeof customStyle === 'object' && target === 'object') ||    (target === 'string' && typeof customStyle === 'string')  ) {    return customStyle;  }  // 字符串转对象  if (target === 'object') {    // 去除字符串样式中的两端空格(中间的空格不能去掉,比如padding: 20px 0如果去掉了就错了),空格是无用的    customStyle = trim(customStyle);    // 根据";"将字符串转为数组形式    const styleArray = customStyle.split(';');    const style = {};    // 历遍数组,拼接成对象    for (let i = 0; i < styleArray.length; i++) {      // 'font-size:20px;color:red;',如此最后字符串有";"的话,会导致styleArray最后一个元素为空字符串,这里需要过滤      if (styleArray[i]) {        const item = styleArray[i].split(':');        style[trim(item[0])] = trim(item[1]);      }    }    return style;  }  // 这里为对象转字符串形式  let string = '';  for (const i in customStyle) {    // 驼峰转为中划线的形式,否则css内联样式,无法识别驼峰样式属性名    const key = i.replace(/([A-Z])/g, '-$1').toLowerCase();    string += `${key}:${customStyle[i]};`;  }  // 去除两端空格  return trim(string);}/** * @description 进行延时,以达到可以简写代码的目的 * @param {number} value 堵塞时间 单位ms 毫秒 * @returns {Promise} 返回promise */export function sleep(value = 30) {  return new Promise((resolve) => {    setTimeout(() => {      resolve();    }, value);  });}// 判断是图片还是视频export const checkIsImage = (url) => {  // var link = new URL(url)  // var path = link.pathname  var extension = url.split('.').pop().toLowerCase()  var imageExtensions = ['jpg', 'jpeg', 'gif', 'png', 'jfif']  var videoExtensions = ['mp4', 'wmv', 'avi', 'mov']  // 图片  if (imageExtensions.includes(extension)) {    return true  }  // 视频  if (videoExtensions.includes(extension)) {    return false  }  return null}export const generateUUID = (len = 32, firstU = true, radix = null) => {  const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');  const uuid = [];  radix = radix || chars.length;  if (len) {    // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位    for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];  } else {    let r;    // rfc4122标准要求返回的uuid中,某些位为固定的字符    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';    uuid[14] = '4';    for (let i = 0; i < 36; i++) {      if (!uuid[i]) {        r = 0 | (Math.random() * 16);        uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];      }    }  }  // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class  if (firstU) {    uuid.shift();    return `u${uuid.join('')}`;  }  return uuid.join('');}
 |