index.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /**
  15. * List of @babel/parser plugins that are used for template expression
  16. * transforms and SFC script transforms. By default we enable proposals slated
  17. * for ES2020. This will need to be updated as the spec moves forward.
  18. * Full list at https://babeljs.io/docs/en/next/babel-parser#plugins
  19. */
  20. export const babelParserDefaultPlugins = [
  21. 'bigInt',
  22. 'optionalChaining',
  23. 'nullishCoalescingOperator'
  24. ] as const
  25. export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
  26. ? Object.freeze({})
  27. : {}
  28. export const EMPTY_ARR = __DEV__ ? Object.freeze([]) : []
  29. export const NOOP = () => {}
  30. /**
  31. * Always return false.
  32. */
  33. export const NO = () => false
  34. const onRE = /^on[^a-z]/
  35. export const isOn = (key: string) => onRE.test(key)
  36. export const isModelListener = (key: string) => key.startsWith('onUpdate:')
  37. export const extend = Object.assign
  38. export const remove = <T>(arr: T[], el: T) => {
  39. const i = arr.indexOf(el)
  40. if (i > -1) {
  41. arr.splice(i, 1)
  42. }
  43. }
  44. const hasOwnProperty = Object.prototype.hasOwnProperty
  45. export const hasOwn = (
  46. val: object,
  47. key: string | symbol
  48. ): key is keyof typeof val => hasOwnProperty.call(val, key)
  49. export const isArray = Array.isArray
  50. export const isMap = (val: unknown): val is Map<any, any> =>
  51. toTypeString(val) === '[object Map]'
  52. export const isSet = (val: unknown): val is Set<any> =>
  53. toTypeString(val) === '[object Set]'
  54. export const isDate = (val: unknown): val is Date => val instanceof Date
  55. export const isFunction = (val: unknown): val is Function =>
  56. typeof val === 'function'
  57. export const isString = (val: unknown): val is string => typeof val === 'string'
  58. export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
  59. export const isObject = (val: unknown): val is Record<any, any> =>
  60. val !== null && typeof val === 'object'
  61. export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
  62. return isObject(val) && isFunction(val.then) && isFunction(val.catch)
  63. }
  64. export const objectToString = Object.prototype.toString
  65. export const toTypeString = (value: unknown): string =>
  66. objectToString.call(value)
  67. export const toRawType = (value: unknown): string => {
  68. // extract "RawType" from strings like "[object RawType]"
  69. return toTypeString(value).slice(8, -1)
  70. }
  71. export const isPlainObject = (val: unknown): val is object =>
  72. toTypeString(val) === '[object Object]'
  73. export const isIntegerKey = (key: unknown) =>
  74. isString(key) &&
  75. key !== 'NaN' &&
  76. key[0] !== '-' &&
  77. '' + parseInt(key, 10) === key
  78. export const isReservedProp = /*#__PURE__*/ makeMap(
  79. // the leading comma is intentional so empty string "" is also included
  80. ',key,ref,' +
  81. 'onVnodeBeforeMount,onVnodeMounted,' +
  82. 'onVnodeBeforeUpdate,onVnodeUpdated,' +
  83. 'onVnodeBeforeUnmount,onVnodeUnmounted'
  84. )
  85. const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
  86. const cache: Record<string, string> = Object.create(null)
  87. return ((str: string) => {
  88. const hit = cache[str]
  89. return hit || (cache[str] = fn(str))
  90. }) as any
  91. }
  92. const camelizeRE = /-(\w)/g
  93. /**
  94. * @private
  95. */
  96. export const camelize = cacheStringFunction(
  97. (str: string): string => {
  98. return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
  99. }
  100. )
  101. const hyphenateRE = /\B([A-Z])/g
  102. /**
  103. * @private
  104. */
  105. export const hyphenate = cacheStringFunction((str: string) =>
  106. str.replace(hyphenateRE, '-$1').toLowerCase()
  107. )
  108. /**
  109. * @private
  110. */
  111. export const capitalize = cacheStringFunction(
  112. (str: string) => str.charAt(0).toUpperCase() + str.slice(1)
  113. )
  114. /**
  115. * @private
  116. */
  117. export const toHandlerKey = cacheStringFunction(
  118. (str: string) => (str ? `on${capitalize(str)}` : ``)
  119. )
  120. // compare whether a value has changed, accounting for NaN.
  121. export const hasChanged = (value: any, oldValue: any): boolean =>
  122. value !== oldValue && (value === value || oldValue === oldValue)
  123. export const invokeArrayFns = (fns: Function[], arg?: any) => {
  124. for (let i = 0; i < fns.length; i++) {
  125. fns[i](arg)
  126. }
  127. }
  128. export const def = (obj: object, key: string | symbol, value: any) => {
  129. Object.defineProperty(obj, key, {
  130. configurable: true,
  131. enumerable: false,
  132. value
  133. })
  134. }
  135. export const toNumber = (val: any): any => {
  136. const n = parseFloat(val)
  137. return isNaN(n) ? val : n
  138. }
  139. let _globalThis: any
  140. export const getGlobalThis = (): any => {
  141. return (
  142. _globalThis ||
  143. (_globalThis =
  144. typeof globalThis !== 'undefined'
  145. ? globalThis
  146. : typeof self !== 'undefined'
  147. ? self
  148. : typeof window !== 'undefined'
  149. ? window
  150. : typeof global !== 'undefined'
  151. ? global
  152. : {})
  153. )
  154. }