工具函数库

130 阅读2分钟

1、校验移动端

const verifyMobile = () => {
  const windowConfig = window.navigator;
  const isMobile = !!windowConfig.userAgent.match(/AppleWebKit.*Mobile.*/);
  return isMobile;
}

2、校验IOS

const verifyIos = () => {
    const isIOS = !!window.navigator.userAgent.match(/(i[^;]+;( U;)? CPU.+Mac OS X/);
    return isIOS;
}

3、字典value转换成对应的label

/**
 * 判断数据类型
 * undefined和null原样返回
 * 返回string,number,array,boolean,object,symbol,function,bigInt
 */
const backType = value => {
	if (value === undefined || value === null) {
		return value
	}
	if (Object.prototype.toString.call(value) === '[object String]') {
		return 'string'
	}
	if (Object.prototype.toString.call(value) === '[object Number]') {
		return 'number'
	}
	if (Object.prototype.toString.call(value) === '[object Array]') {
		return 'array'
	}
	if (Object.prototype.toString.call(value) === '[object Boolean]') {
		return 'boolean'
	}
	if (Object.prototype.toString.call(value) === '[object Object]') {
		return 'object'
	}
	if (Object.prototype.toString.call(value) === '[object Symbol]') {
		return 'symbol'
	}
	if (Object.prototype.toString.call(value) === '[object Function]') {
		return 'function'
	}
	if (Object.prototype.toString.call(value) === '[object BigInt]') {
		return 'bigInt'
	}
}

/**
 * 字典value转换成对应的label
 */
const getLabel = (list, value, split = ',', join = ' ') => {
	// 没有枚举后者枚举不是列表直接返回空
	if (!list || backType(value) === 'array' || !list.length) {
		return ''
	}
	// 多个枚举字符串转换成数组匹配
	if (backType(value) === 'string' && value.indexOf(split) !== -1) {
		return getLabel(list, value.split(split), split, join)
	}
	// 枚举值是数组
	if (backType(value) === 'array') {
		const arr = value.map(item => {
			return getLabel(list, item, join)
		})
		return arr.join(join)
	}
	const item = list.filter(obj => {
		return `${obj.code}` === `${value}`
	})
	return (item[0] && item[0].name) || value
}

4、时间格式转换


const DateFormat = function (date: any, fmt: string) {
	fmt = fmt || 'yyyy-MM-dd hh:mm:ss'
	if (date === null || typeof date === 'undefined' || date === '') {
		return null
	} else {
		// 时间要转成obj,否则报错
		date = new Date(date)
	}
	var o: any = {
		'M+': date.getMonth() + 1, // 月
		'd+': date.getDate(), // 日
		'h+': date.getHours(), // 时
		'm+': date.getMinutes(), // 分
		's+': date.getSeconds(), // 秒
		'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
		S: date.getMilliseconds(), // 毫秒
	}
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
	for (var k in o) {
		if (new RegExp('(' + k + ')').test(fmt)) {
			fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
		}
	}
	return fmt
}

5、获取Cookie

const getCookie = (cookie_name: string) => {
  var cookies = document.cookie;
  //索引长度,开始索引的位置
  var cookie_pos = cookies.indexOf(cookie_name);

  let value = '';
  // 如果找到了索引,就代表cookie存在,否则不存在
  if (cookie_pos != -1) {
      // 把cookie_pos放在值的开始,只要给值加1即可
      //计算取cookie值得开始索引,加的1为“=”
      cookie_pos = cookie_pos + cookie_name.length + 1;
      //计算取cookie值得结束索引
      var cookie_end = cookies.indexOf(";", cookie_pos);

      if (cookie_end == -1) {
          cookie_end = cookies.length;
      }
      //得到想要的cookie的值
      value = unescape(cookies.substring(cookie_pos, cookie_end));
  }
  return value;
}

6、节流函数

// 节流
throttle = (fn: any, interval = 500) => {
  let run = true;
  return function () {
    if (!run) return;
    run = false;
    setTimeout(() => {
      fn();
      run = true;
    }, interval);
  };
}

7、防抖函数

// 防抖;
debounce = (fn: any, interval = 500) => {
  let timer: any = null;
  return function () {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn();
    }, interval);
  };
}