index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { makeMap } from './makeMap'
  2. export { makeMap }
  3. export * from './patchFlags'
  4. export * from './shapeFlags'
  5. export * from './globalsWhitelist'
  6. export * from './codeframe'
  7. export * from './mockWarn'
  8. export * from './normalizeProp'
  9. export * from './domTagConfig'
  10. export * from './domAttrConfig'
  11. export * from './escapeHtml'
  12. export * from './looseEqual'
  13. export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
  14. ? Object.freeze({})
  15. : {}
  16. export const EMPTY_ARR: [] = []
  17. export const NOOP = () => {}
  18. /**
  19. * Always return false.
  20. */
  21. export const NO = () => false
  22. const onRE = /^on[^a-z]/
  23. export const isOn = (key: string) => onRE.test(key)
  24. export const extend = <T extends object, U extends object>(
  25. a: T,
  26. b: U
  27. ): T & U => {
  28. for (const key in b) {
  29. ;(a as any)[key] = b[key]
  30. }
  31. return a as any
  32. }
  33. export const remove = <T>(arr: T[], el: T) => {
  34. const i = arr.indexOf(el)
  35. if (i > -1) {
  36. arr.splice(i, 1)
  37. }
  38. }
  39. const hasOwnProperty = Object.prototype.hasOwnProperty
  40. export const hasOwn = (
  41. val: object,
  42. key: string | symbol
  43. ): key is keyof typeof val => hasOwnProperty.call(val, key)
  44. export const isArray = Array.isArray
  45. export const isFunction = (val: unknown): val is Function =>
  46. typeof val === 'function'
  47. export const isString = (val: unknown): val is string => typeof val === 'string'
  48. export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
  49. export const isObject = (val: unknown): val is Record<any, any> =>
  50. val !== null && typeof val === 'object'
  51. export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
  52. return isObject(val) && isFunction(val.then) && isFunction(val.catch)
  53. }
  54. export const objectToString = Object.prototype.toString
  55. export const toTypeString = (value: unknown): string =>
  56. objectToString.call(value)
  57. export const toRawType = (value: unknown): string => {
  58. return toTypeString(value).slice(8, -1)
  59. }
  60. export const isPlainObject = (val: unknown): val is object =>
  61. toTypeString(val) === '[object Object]'
  62. export const isReservedProp = /*#__PURE__*/ makeMap(
  63. 'key,ref,' +
  64. 'onVnodeBeforeMount,onVnodeMounted,' +
  65. 'onVnodeBeforeUpdate,onVnodeUpdated,' +
  66. 'onVnodeBeforeUnmount,onVnodeUnmounted'
  67. )
  68. const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
  69. const cache: Record<string, string> = Object.create(null)
  70. return ((str: string) => {
  71. const hit = cache[str]
  72. return hit || (cache[str] = fn(str))
  73. }) as any
  74. }
  75. const camelizeRE = /-(\w)/g
  76. export const camelize = cacheStringFunction(
  77. (str: string): string => {
  78. return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
  79. }
  80. )
  81. const hyphenateRE = /\B([A-Z])/g
  82. export const hyphenate = cacheStringFunction(
  83. (str: string): string => {
  84. return str.replace(hyphenateRE, '-$1').toLowerCase()
  85. }
  86. )
  87. export const capitalize = cacheStringFunction(
  88. (str: string): string => {
  89. return str.charAt(0).toUpperCase() + str.slice(1)
  90. }
  91. )
  92. // compare whether a value has changed, accounting for NaN.
  93. export const hasChanged = (value: any, oldValue: any): boolean =>
  94. value !== oldValue && (value === value || oldValue === oldValue)
  95. // for converting {{ interpolation }} values to displayed strings.
  96. export const toDisplayString = (val: unknown): string => {
  97. return val == null
  98. ? ''
  99. : isArray(val) || (isPlainObject(val) && val.toString === objectToString)
  100. ? JSON.stringify(val, null, 2)
  101. : String(val)
  102. }
  103. export const invokeArrayFns = (fns: Function[], arg?: any) => {
  104. for (let i = 0; i < fns.length; i++) {
  105. fns[i](arg)
  106. }
  107. }
  108. export const def = (obj: object, key: string | symbol, value: any) => {
  109. Object.defineProperty(obj, key, { value })
  110. }