index.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { makeMap } from './makeMap'
  2. export { makeMap }
  3. export * from './patchFlags'
  4. export * from './shapeFlags'
  5. export * from './slotFlags'
  6. export * from './globalsWhitelist'
  7. export * from './codeframe'
  8. export * from './normalizeProp'
  9. export * from './domTagConfig'
  10. export * from './domAttrConfig'
  11. export * from './escapeHtml'
  12. export * from './looseEqual'
  13. export * from './toDisplayString'
  14. export * from './typeUtils'
  15. export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
  16. ? Object.freeze({})
  17. : {}
  18. export const EMPTY_ARR = __DEV__ ? Object.freeze([]) : []
  19. export const NOOP = () => {}
  20. /**
  21. * Always return false.
  22. */
  23. export const NO = () => false
  24. const onRE = /^on[^a-z]/
  25. export const isOn = (key: string) => onRE.test(key)
  26. export const isModelListener = (key: string) => key.startsWith('onUpdate:')
  27. export const extend = Object.assign
  28. export const remove = <T>(arr: T[], el: T) => {
  29. const i = arr.indexOf(el)
  30. if (i > -1) {
  31. arr.splice(i, 1)
  32. }
  33. }
  34. const hasOwnProperty = Object.prototype.hasOwnProperty
  35. export const hasOwn = (
  36. val: object,
  37. key: string | symbol
  38. ): key is keyof typeof val => hasOwnProperty.call(val, key)
  39. export const isArray = Array.isArray
  40. export const isMap = (val: unknown): val is Map<any, any> =>
  41. toTypeString(val) === '[object Map]'
  42. export const isSet = (val: unknown): val is Set<any> =>
  43. toTypeString(val) === '[object Set]'
  44. export const isDate = (val: unknown): val is Date => val instanceof Date
  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. // extract "RawType" from strings like "[object RawType]"
  59. return toTypeString(value).slice(8, -1)
  60. }
  61. export const isPlainObject = (val: unknown): val is object =>
  62. toTypeString(val) === '[object Object]'
  63. export const isIntegerKey = (key: unknown) =>
  64. isString(key) &&
  65. key !== 'NaN' &&
  66. key[0] !== '-' &&
  67. '' + parseInt(key, 10) === key
  68. export const isReservedProp = /*#__PURE__*/ makeMap(
  69. // the leading comma is intentional so empty string "" is also included
  70. ',key,ref,ref_for,ref_key,' +
  71. 'onVnodeBeforeMount,onVnodeMounted,' +
  72. 'onVnodeBeforeUpdate,onVnodeUpdated,' +
  73. 'onVnodeBeforeUnmount,onVnodeUnmounted'
  74. )
  75. export const isBuiltInDirective = /*#__PURE__*/ makeMap(
  76. 'bind,cloak,else-if,else,for,html,if,model,on,once,pre,show,slot,text,memo'
  77. )
  78. const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
  79. const cache: Record<string, string> = Object.create(null)
  80. return ((str: string) => {
  81. const hit = cache[str]
  82. return hit || (cache[str] = fn(str))
  83. }) as any
  84. }
  85. const camelizeRE = /-(\w)/g
  86. /**
  87. * @private
  88. */
  89. export const camelize = cacheStringFunction((str: string): string => {
  90. return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
  91. })
  92. const hyphenateRE = /\B([A-Z])/g
  93. /**
  94. * @private
  95. */
  96. export const hyphenate = cacheStringFunction((str: string) =>
  97. str.replace(hyphenateRE, '-$1').toLowerCase()
  98. )
  99. /**
  100. * @private
  101. */
  102. export const capitalize = cacheStringFunction(
  103. (str: string) => str.charAt(0).toUpperCase() + str.slice(1)
  104. )
  105. /**
  106. * @private
  107. */
  108. export const toHandlerKey = cacheStringFunction((str: string) =>
  109. str ? `on${capitalize(str)}` : ``
  110. )
  111. // compare whether a value has changed, accounting for NaN.
  112. export const hasChanged = (value: any, oldValue: any): boolean =>
  113. !Object.is(value, oldValue)
  114. export const invokeArrayFns = (fns: Function[], arg?: any) => {
  115. for (let i = 0; i < fns.length; i++) {
  116. fns[i](arg)
  117. }
  118. }
  119. export const def = (obj: object, key: string | symbol, value: any) => {
  120. Object.defineProperty(obj, key, {
  121. configurable: true,
  122. enumerable: false,
  123. value
  124. })
  125. }
  126. export const toNumber = (val: any): any => {
  127. const n = parseFloat(val)
  128. return isNaN(n) ? val : n
  129. }
  130. let _globalThis: any
  131. export const getGlobalThis = (): any => {
  132. return (
  133. _globalThis ||
  134. (_globalThis =
  135. typeof globalThis !== 'undefined'
  136. ? globalThis
  137. : typeof self !== 'undefined'
  138. ? self
  139. : typeof window !== 'undefined'
  140. ? window
  141. : typeof global !== 'undefined'
  142. ? global
  143. : {})
  144. )
  145. }