index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. export const isOn = (key: string) => key[0] === 'o' && key[1] === 'n'
  23. export const extend = <T extends object, U extends object>(
  24. a: T,
  25. b: U
  26. ): T & U => {
  27. for (const key in b) {
  28. ;(a as any)[key] = b[key]
  29. }
  30. return a as any
  31. }
  32. export const remove = <T>(arr: T[], el: T) => {
  33. const i = arr.indexOf(el)
  34. if (i > -1) {
  35. arr.splice(i, 1)
  36. }
  37. }
  38. const hasOwnProperty = Object.prototype.hasOwnProperty
  39. export const hasOwn = (
  40. val: object,
  41. key: string | symbol
  42. ): key is keyof typeof val => hasOwnProperty.call(val, key)
  43. export const isArray = Array.isArray
  44. export const isFunction = (val: unknown): val is Function =>
  45. typeof val === 'function'
  46. export const isString = (val: unknown): val is string => typeof val === 'string'
  47. export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
  48. export const isObject = (val: unknown): val is Record<any, any> =>
  49. val !== null && typeof val === 'object'
  50. export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
  51. return isObject(val) && isFunction(val.then) && isFunction(val.catch)
  52. }
  53. export const objectToString = Object.prototype.toString
  54. export const toTypeString = (value: unknown): string =>
  55. objectToString.call(value)
  56. export const toRawType = (value: unknown): string => {
  57. return toTypeString(value).slice(8, -1)
  58. }
  59. export const isPlainObject = (val: unknown): val is object =>
  60. toTypeString(val) === '[object Object]'
  61. export const isReservedProp = /*#__PURE__*/ makeMap(
  62. 'key,ref,' +
  63. 'onVnodeBeforeMount,onVnodeMounted,' +
  64. 'onVnodeBeforeUpdate,onVnodeUpdated,' +
  65. 'onVnodeBeforeUnmount,onVnodeUnmounted'
  66. )
  67. const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
  68. const cache: Record<string, string> = Object.create(null)
  69. return ((str: string) => {
  70. const hit = cache[str]
  71. return hit || (cache[str] = fn(str))
  72. }) as any
  73. }
  74. const camelizeRE = /-(\w)/g
  75. export const camelize = cacheStringFunction(
  76. (str: string): string => {
  77. return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
  78. }
  79. )
  80. const hyphenateRE = /\B([A-Z])/g
  81. export const hyphenate = cacheStringFunction(
  82. (str: string): string => {
  83. return str.replace(hyphenateRE, '-$1').toLowerCase()
  84. }
  85. )
  86. export const capitalize = cacheStringFunction(
  87. (str: string): string => {
  88. return str.charAt(0).toUpperCase() + str.slice(1)
  89. }
  90. )
  91. // compare whether a value has changed, accounting for NaN.
  92. export const hasChanged = (value: any, oldValue: any): boolean =>
  93. value !== oldValue && (value === value || oldValue === oldValue)
  94. // for converting {{ interpolation }} values to displayed strings.
  95. export const toDisplayString = (val: unknown): string => {
  96. return val == null
  97. ? ''
  98. : isArray(val) || (isPlainObject(val) && val.toString === objectToString)
  99. ? JSON.stringify(val, null, 2)
  100. : String(val)
  101. }