记vue中常用的一些检测方法

util.js 封装:

const toStr = Object.prototype.toString;

export function isUndef(v) {
  return v === undefined || v === null;
}

export function isDef(v) {
  return v !== undefined && v !== null;
}

export function isTrue(v) {
  return v === true;
}

export function isFalse(v) {
  return v === false;
}

// 检测对象类型
export function isRegExp(v) {
  return toStr.call(v) === '[object RegExp]';
}
export function isArray(v) {
  return toStr.call(v) === '[object Array]';
}
export function isString(v) {
  return toStr.call(v) === '[object String]';
}
export function isObject(v) {
  return toStr.call(v) === '[object Object]';
}
export function isFunction(v) {
  return toStr.call(v) === '[object Function]';
}
export function isNumber(v) {
  return toStr.call(v) === '[object Number]';
}
export function isDate(v) {
  return toStr.call(v) === '[object Date]';
}

// 删除数组中某一个
export function remove(arr, item) {
  if (arr.length) {
    const index = arr.indexOf(item);
    if (index > -1) {
      return arr.splice(index, 1);
    }
  }
  return arr;
}
export function matches(pattern, name) {
  if (Array.isArray(pattern)) {
    return pattern.indexOf(name) > -1;
  } else if (typeof pattern === 'string') {
    return pattern.split(',').indexOf(name) > -1;
  } else if (isRegExp(pattern)) {
    return pattern.test(name);
  }
  return false;
}
/**
 * todo
 */
export function isapp() {
  if (process.env.NODE_ENV === 'production') {
    // return navigator.userAgent.toLowerCase().indexOf(projectTag) >= 0;
    return navigator.userAgent.toLowerCase().indexOf('micromessenger') < 0;
  }
  return false;
}
export function moneyFilter(text) {
  if (text.indexOf('.') >= 0) {
    return text.split('.')[0] - 0;
  }
  return text - 0;
}

export function isIPhoneX() {
  // iPhone X、iPhone XS
  return /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 375 && window.screen.height === 812;
}
export function isIPhoneXSMax() {
  // iPhone XS Max
  return /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 414 && window.screen.height === 896;
}
export function isIPhoneXR() {
  // iPhone XR
  return /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 2 && window.screen.width === 414 && window.screen.height === 896;
}
export function IPhoneX() {
  return isIPhoneX() || isIPhoneXSMax() || isIPhoneXR();
}

在使用的页面中引入:

import { Function} from '路径';
 
使用:
Function(obj)
 
Function 是指你使用的检测方法 如检测对象那么Funcion == isObject
 
 
 

猜你喜欢

转载自www.cnblogs.com/ymdzha/p/10320012.html