Vue3源码阅读(三)shared中常用函数

工具函数

  • 源码位于shared/src/index.ts
  • EMPTY_OBJ 空对象
// Object.freeze是冻结对象,开发环境返回被冻结的空对象
export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
  ? Object.freeze({})
  : {}
  • EMPTY_ARR 空数组
export const EMPTY_ARR = __DEV__ ? Object.freeze([]) : []
  • NOOP 空函数
export const NOOP = () => {}
  • NO 永远返回 false 的函数
export const NO = () => false
  • isOn 判断字符串是不是 on 开头,并且 on 后首字母不是小写字母
const onRE = /^on[^a-z]/
export const isOn = (key: string) => onRE.test(key)
  • isModelListener 监听器 判断字符串是不是以onUpdate:开头
export const isModelListener = (key: string) => key.startsWith('onUpdate:')
  • extend 封装Object.assgin 合并
export const extend = Object.assign
  • remove 移除数组的一项
export const remove = <T>(arr: T[], el: T) => {
  const i = arr.indexOf(el)
  if (i > -1) {
    arr.splice(i, 1)
  }
}
  • hasOwn 是不是自己本身所拥有的属性
const hasOwnProperty = Object.prototype.hasOwnProperty
export const hasOwn = (
  val: object,
  key: string | symbol
): key is keyof typeof val => hasOwnProperty.call(val, key)
  • isArray 是否为数组
export const isArray = Array.isArray
  • isMap 是否为Map对象
export const isMap = (val: unknown): val is Map<any, any> =>
  toTypeString(val) === '[object Map]'
  • isSet 是否为集合
export const isSet = (val: unknown): val is Set<any> =>
  toTypeString(val) === '[object Set]'
  • isDate 是否为日期
export const isDate = (val: unknown): val is Date => val instanceof Date
  • isFunction 是否为函数
export const isFunction = (val: unknown): val is Function =>
  typeof val === 'function'
  • 转换为类型字符串
export const objectToString = Object.prototype.toString
export const toTypeString = (value: unknown): string =>
  objectToString.call(value)
  • isString 是否为字符串
export const isString = (val: unknown): val is string => typeof val === 'string'
  • isSymbol 是否为Symbol
export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
  • isObject 是否为对象
// 判断不为 null 的原因是 typeof null == 'object'
export const isObject = (val: unknown): val is Record<any, any> =>
  val !== null && typeof val === 'object'
  • isPromise 判断是不是 Promise
const isPromise = (val) => {
    return isObject(val) && isFunction(val.then) && isFunction(val.catch);
};
  • objectToString 对象转字符串
export const objectToString = Object.prototype.toString
  • toRawType 对象转字符串 截取后几位
export const toRawType = (value: unknown): string => {
  // extract "RawType" from strings like "[object RawType]"
  return toTypeString(value).slice(8, -1)
}
  • isPlainObject 判断是不是纯粹的对象
export const isPlainObject = (val: unknown): val is object =>
  toTypeString(val) === '[object Object]'
  • isIntegerKey 判断是不是数字型的字符串key值
const isIntegerKey = (key) => isString(key) &&
    key !== 'NaN' &&
    key[0] !== '-' &&
    '' + parseInt(key, 10) === key;
  • cacheStringFunction 缓存
const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
  const cache: Record<string, string> = Object.create(null)
  return ((str: string) => {
    const hit = cache[str]
    return hit || (cache[str] = fn(str))
  }) as any
}
  • hasChanged 是否有变化
// compare whether a value has changed, accounting for NaN.
export const hasChanged = (value: any, oldValue: any): boolean =>
  !Object.is(value, oldValue)
  • invokeArrayFns 执行数组里的函数
export const invokeArrayFns = (fns: Function[], arg?: any) => {
  for (let i = 0; i < fns.length; i++) {
    fns[i](arg)
  }
}
  • def 定义对象属性
export const def = (obj: object, key: string | symbol, value: any) => {
  Object.defineProperty(obj, key, {
    configurable: true,
    enumerable: false,
    value
  })
}
  • toNumber 转数字
export const toNumber = (val: any): any => {
  const n = parseFloat(val)
  return isNaN(n) ? val : n
}
// 其实 isNaN 本意是判断是不是 NaN 值,但是不准确的。ES6有Number.isNaN去判断
Number.isNaN('a')  // false
Number.isNaN(NaN); // true
  • getGlobalThis 全局对象
// 全局属性 globalThis 包含全局的 this 值,类似于全局对象
let _globalThis: any
export const getGlobalThis = (): any => {
  return (
    _globalThis ||
    (_globalThis =
      typeof globalThis !== 'undefined'
        ? globalThis
        : typeof self !== 'undefined'
        ? self
        : typeof window !== 'undefined'
        ? window
        : typeof global !== 'undefined'
        ? global
        : {})
  )
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值