SoFunction
Updated on 2025-04-05

Implementation of detection method in vue source code

Determine whether it is undefined or null

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

Determine whether it is a Promise function

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

Determine whether it is a simple data type

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

Strictly check complex data types

const isPlainObject = (obj) => {
 return (obj) === '[object Object]'
}

const isRegExp = (v) => {
 return (v) === '[object RegExp]'
}

Convert camel string to connector magicEightTall to magic-eight-tall

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

Convert the linker to camel string magic-eight-tall to magicEightTall

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

If you do not want to repeat the conversion, you can call the conversion function by calling the following method

const cached = (fn) => {
  const cache = (null)
  (cache);
  return ((str) => {
   const hit = cache[str]
   return hit || (cache[str] = fn(str))
  })
};

example

const camelize = cached((str) => {
  return (camelizeRE, (_, c) => c ? () : '')
})

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.