vant 源码解析之 utils/index.ts 工具函数

源代码

import Vue from 'vue';

export { use } from './use';

export const isServer: boolean = Vue.prototype.$isServer;
//Vue 原型上自带的属性 判断上是否运行在服务端上

export function noop() {}

export function isDef(value: any): boolean {
  // value:any 表示传入的参数是任意类型  boolean 表示返回值是boolean类型
  return value !== undefined && value !== null;
  // 传入的值不能是undefined 也不能是null
}
//  in 15.686s  0.486s

export function isObj(x: any): boolean {
  //判断一个值是否是 对象类型
  // 传入的参数 是 any 任意类型i
  // 返回值是 boolean
  const type = typeof x;
  // typeof 判断数据类型
  // typeof 判断数据类型时 null 也是 object 所以 type 不能为null
  // 函数也是 对象
  return x !== null && (type === 'object' || type === 'function');
}

export function get(object: any, path: string): any {
  const keys = path.split('.');
  let result = object;

  keys.forEach(key => {
    result = isDef(result[key]) ? result[key] : '';
  });

  return result;
}

const camelizeRE = /-(\w)/g;//\w 表示数字字母吓下划线
export function camelize(str: string): string {
  return str.replace(camelizeRE, (_, c) => c.toUpperCase());
  //toUpperCase 将字母转换成 大写
  
}

export function isAndroid(): boolean {
  /* istanbul ignore next */
  return isServer ? false : /android/.test(navigator.userAgent.toLowerCase());
  // 只有在非服务端
  // 判断是否是安卓设备
}

export function isIOS(): boolean {
  /* istanbul ignore next */
  return isServer ? false : /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
  // 只有在非服务端
  //判断是否是 ios设备
}

export function range(num: number, min: number, max: number): number {
  // Math.max 返回一组数中的最大者
  // Math.min 返沪一组数种的最小者
  return Math.min(Math.max(num, min), max);
}

export function isInDocument(element: HTMLElement): boolean {
  // 在ts j中 dom 节点的type类型是 HTMLElement
  // 返回值是boolean
  return document.body.contains(element);
  // document.body 节点 是否包含目标节点
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值