All files / src/utils index.ts

0% Statements 0/112
0% Branches 0/1
0% Functions 0/1
0% Lines 0/112

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                                                                                                                                                                                                                                 
/**
 * @description 判断是否为数组
 * @param {*} val array
 * @returns boolean
 */
export const isArray = (val: string) => Object.prototype.toString.call(val) === '[object Array]';

/**
 * @description 判断是否为对象
 * @param {*} val object
 * @returns boolean
 */
export const isObject = (val: string) => typeof val === 'object';

/**
 * @description 判断是否为字符串
 * @param {*} val string
 * @returns boolean
 */
export const isString = (val: string) => typeof val === 'string';

/**
 *  判断是否是数字
 * @param {Number} val
 */
export const checkNum = (val: string) => /^\d{1,}$/g.test(val);

/**
 *  判断是否是字母
 * @param {Number} val
 */
export const checkLetter = (val: string) => /^[a-zA-Z]+$/g.test(val);

/**
 * 判断是否是字母或数字
 * @param {Number || String} val  字符或数字
 */
export const checkNumOrLetter = (val: string) => /^[0-9a-zA-Z]*$/g.test(val);

/**
 * 判断是否是中文
 * @param {String} data  中文
 */
export const checkChinese = (val: string) => /^[\u4E00-\u9FA5]+$/g.test(val);

/**
 * 判断是否是邮箱地址
 * @param {String} val
 */
export const checkEmail = (val: string) => {
  const reg = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/g;
  if (reg.test(val)) return true;
};

/**
 * 判断是否是手机号,只要是13,14,15,16,17,18,19开头即可
 * @param {String} val
 */
export const checkTelphone = (val: string) => {
  const reg = /^((\+|00)86)?1[3-9]\d{9}$/g;
  if (reg.test(val)) return true;
};

/**
 * 判断是浏览器内核
 */
export const checkBrowser = () => {
  const u = navigator.userAgent;
  const obj = {
    trident: u.indexOf('Trident') > -1, // IE内核
    presto: u.indexOf('Presto') > -1, // opera内核
    webKit: u.indexOf('AppleWebKit') > -1, // 苹果、谷歌内核
    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') === -1 // 火狐内核
  };
  return Object.keys(obj)[Object.values(obj).indexOf(true)];
};

/**
 * 判断是终端类型,值有ios,android,iPad
 */
export const checkIosAndroidIpad = () => {
  const u = navigator.userAgent;
  const obj = {
    ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
    android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, // android终端或者uc浏览器
    iPad: u.indexOf('iPad') > -1 // 是否iPad
  };
  return Object.keys(obj)[Object.values(obj).indexOf(true)];
};

/**
 * localStorage 存贮
 * 目前对象值如果是函数 、RegExp等特殊对象存贮会被忽略
 * @param {String} key  属性
 * @param {Object} val 值
 */
export const localStorageSet = (key: string, val: any) => {
  if (isObject(val)) val = JSON.stringify(val);
  localStorage.setItem(key, val);
};

/**
 * localStorage 获取
 * @param {String} key  属性
 */
export const localStorageGet = (key: string) => localStorage.getItem(key) || '';

/**
 * localStorage 移除
 * @param {String} key  属性
 */
export const localStorageRemove = (key: string) => localStorage.removeItem(key);