general.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { makeMap } from './makeMap'
  2. export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
  3. ? Object.freeze({})
  4. : {}
  5. export const EMPTY_ARR = __DEV__ ? Object.freeze([]) : []
  6. export const NOOP = () => {}
  7. /**
  8. * Always return false.
  9. */
  10. export const NO = () => false
  11. const onRE = /^on[^a-z]/
  12. export const isOn = (key: string) => onRE.test(key)
  13. export const isModelListener = (key: string) => key.startsWith('onUpdate:')
  14. export const extend = Object.assign
  15. export const remove = <T>(arr: T[], el: T) => {
  16. const i = arr.indexOf(el)
  17. if (i > -1) {
  18. arr.splice(i, 1)
  19. }
  20. }
  21. const hasOwnProperty = Object.prototype.hasOwnProperty
  22. export const hasOwn = (
  23. val: object,
  24. key: string | symbol
  25. ): key is keyof typeof val => hasOwnProperty.call(val, key)
  26. export const isArray = Array.isArray
  27. export const isMap = (val: unknown): val is Map<any, any> =>
  28. toTypeString(val) === '[object Map]'
  29. export const isSet = (val: unknown): val is Set<any> =>
  30. toTypeString(val) === '[object Set]'
  31. export const isDate = (val: unknown): val is Date =>
  32. toTypeString(val) === '[object Date]'
  33. export const isRegExp = (val: unknown): val is RegExp =>
  34. toTypeString(val) === '[object RegExp]'
  35. export const isFunction = (val: unknown): val is Function =>
  36. typeof val === 'function'
  37. export const isString = (val: unknown): val is string => typeof val === 'string'
  38. export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
  39. export const isObject = (val: unknown): val is Record<any, any> =>
  40. val !== null && typeof val === 'object'
  41. export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
  42. return isObject(val) && isFunction(val.then) && isFunction(val.catch)
  43. }
  44. export const objectToString = Object.prototype.toString
  45. export const toTypeString = (value: unknown): string =>
  46. objectToString.call(value)
  47. export const toRawType = (value: unknown): string => {
  48. // extract "RawType" from strings like "[object RawType]"
  49. return toTypeString(value).slice(8, -1)
  50. }
  51. export const isPlainObject = (val: unknown): val is object =>
  52. toTypeString(val) === '[object Object]'
  53. export const isIntegerKey = (key: unknown) =>
  54. isString(key) &&
  55. key !== 'NaN' &&
  56. key[0] !== '-' &&
  57. '' + parseInt(key, 10) === key
  58. export const isReservedProp = /*#__PURE__*/ makeMap(
  59. // the leading comma is intentional so empty string "" is also included
  60. ',key,ref,ref_for,ref_key,' +
  61. 'onVnodeBeforeMount,onVnodeMounted,' +
  62. 'onVnodeBeforeUpdate,onVnodeUpdated,' +
  63. 'onVnodeBeforeUnmount,onVnodeUnmounted'
  64. )
  65. export const isBuiltInDirective = /*#__PURE__*/ makeMap(
  66. 'bind,cloak,else-if,else,for,html,if,model,on,once,pre,show,slot,text,memo'
  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 T
  74. }
  75. const camelizeRE = /-(\w)/g
  76. /**
  77. * @private
  78. */
  79. export const camelize = cacheStringFunction((str: string): string => {
  80. return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
  81. })
  82. const hyphenateRE = /\B([A-Z])/g
  83. /**
  84. * @private
  85. */
  86. export const hyphenate = cacheStringFunction((str: string) =>
  87. str.replace(hyphenateRE, '-$1').toLowerCase()
  88. )
  89. /**
  90. * @private
  91. */
  92. export const capitalize = cacheStringFunction(<T extends string>(str: T) => {
  93. return (str.charAt(0).toUpperCase() + str.slice(1)) as Capitalize<T>
  94. })
  95. /**
  96. * @private
  97. */
  98. export const toHandlerKey = cacheStringFunction(<T extends string>(str: T) => {
  99. const s = str ? `on${capitalize(str)}` : ``
  100. return s as T extends '' ? '' : `on${Capitalize<T>}`
  101. })
  102. // compare whether a value has changed, accounting for NaN.
  103. export const hasChanged = (value: any, oldValue: any): boolean =>
  104. !Object.is(value, oldValue)
  105. export const invokeArrayFns = (fns: Function[], arg?: any) => {
  106. for (let i = 0; i < fns.length; i++) {
  107. fns[i](arg)
  108. }
  109. }
  110. export const def = (obj: object, key: string | symbol, value: any) => {
  111. Object.defineProperty(obj, key, {
  112. configurable: true,
  113. enumerable: false,
  114. value
  115. })
  116. }
  117. /**
  118. * "123-foo" will be parsed to 123
  119. * This is used for the .number modifier in v-model
  120. */
  121. export const looseToNumber = (val: any): any => {
  122. const n = parseFloat(val)
  123. return isNaN(n) ? val : n
  124. }
  125. /**
  126. * Only concerns number-like strings
  127. * "123-foo" will be returned as-is
  128. */
  129. export const toNumber = (val: any): any => {
  130. const n = isString(val) ? Number(val) : NaN
  131. return isNaN(n) ? val : n
  132. }
  133. let _globalThis: any
  134. export const getGlobalThis = (): any => {
  135. return (
  136. _globalThis ||
  137. (_globalThis =
  138. typeof globalThis !== 'undefined'
  139. ? globalThis
  140. : typeof self !== 'undefined'
  141. ? self
  142. : typeof window !== 'undefined'
  143. ? window
  144. : typeof global !== 'undefined'
  145. ? global
  146. : {})
  147. )
  148. }
  149. const identRE = /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/
  150. export function genPropsAccessExp(name: string) {
  151. return identRE.test(name)
  152. ? `__props.${name}`
  153. : `__props[${JSON.stringify(name)}]`
  154. }