vue源碼中的檢測方法

// 判斷是否爲undefined或null函數

const isDef = (v) => {
  return v !== undefined && v !== null
}

// 判斷是否爲Promise 函數prototype

const isPromise = (val) => {
  return (
    val !== undefine &&
    typeof val.then === 'function' &&
    typeof val.catch === 'function'
  )
}

// 判斷是否爲簡單數據類型code

const isPrimitive (value) => {
  return (
    typeof value === 'string' ||
    typeof value === 'number' ||
    typeof value === 'symbol' ||
    typeof value === 'boolean'
  )
}

// 嚴格檢查複雜數據類型字符串

const isPlainObject = (obj) => {
  return Object.prototype.toString.call(obj) === '[object Object]'
}

const isRegExp = (v) => {
  return Object.prototype.toString.call(v) === '[object RegExp]'
}

// 將駝峯字符串轉成鏈接符 magicEightTall 轉換成 magic-eight-tallstring

const hyphenateRE = /\B([A-Z])/g
const hyphenate = (str) => {
  return str.replace(hyphenateRE, '-$1').toLowerCase()
}

// 將鏈接符轉成駝峯字符串 magic-eight-tall 轉換成 magicEightTallit

const camelizeRE = /-(\w)/g
const camelize = (str) => {
    return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
}

// 若是不想重複轉換,可用如下方法調用轉換函數io

const cached = (fn) => {
    const cache = Object.create(null)
    console.log(cache);
    return ((str) => {
      const hit = cache[str]
      return hit || (cache[str] = fn(str))
    })
};

// 例
const camelize = cached((str) => {
    return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
})
相關文章
相關標籤/搜索