源代码
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 节点 是否包含目标节点
}