index.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { makeMap } from './makeMap'
  2. export { makeMap }
  3. export * from './patchFlags'
  4. export { isGloballyWhitelisted } from './globalsWhitelist'
  5. export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
  6. ? Object.freeze({})
  7. : {}
  8. export const EMPTY_ARR: [] = []
  9. export const NOOP = () => {}
  10. /**
  11. * Always return false.
  12. */
  13. export const NO = () => false
  14. export const isOn = (key: string) => key[0] === 'o' && key[1] === 'n'
  15. export const extend = <T extends object, U extends object>(
  16. a: T,
  17. b: U
  18. ): T & U => {
  19. for (const key in b) {
  20. ;(a as any)[key] = b[key]
  21. }
  22. return a as any
  23. }
  24. const hasOwnProperty = Object.prototype.hasOwnProperty
  25. export const hasOwn = (
  26. val: object,
  27. key: string | symbol
  28. ): key is keyof typeof val => hasOwnProperty.call(val, key)
  29. export const isArray = Array.isArray
  30. export const isFunction = (val: unknown): val is Function =>
  31. typeof val === 'function'
  32. export const isString = (val: unknown): val is string => typeof val === 'string'
  33. export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
  34. export const isObject = (val: unknown): val is Record<any, any> =>
  35. val !== null && typeof val === 'object'
  36. export function isPromise<T = any>(val: unknown): val is Promise<T> {
  37. return isObject(val) && isFunction(val.then) && isFunction(val.catch)
  38. }
  39. export const objectToString = Object.prototype.toString
  40. export const toTypeString = (value: unknown): string =>
  41. objectToString.call(value)
  42. export function toRawType(value: unknown): string {
  43. return toTypeString(value).slice(8, -1)
  44. }
  45. export const isPlainObject = (val: unknown): val is object =>
  46. toTypeString(val) === '[object Object]'
  47. export const isReservedProp = /*#__PURE__*/ makeMap(
  48. 'key,ref,' +
  49. 'onVnodeBeforeMount,onVnodeMounted,' +
  50. 'onVnodeBeforeUpdate,onVnodeUpdated,' +
  51. 'onVnodeBeforeUnmount,onVnodeUnmounted'
  52. )
  53. const camelizeRE = /-(\w)/g
  54. export const camelize = (str: string): string => {
  55. return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
  56. }
  57. const hyphenateRE = /\B([A-Z])/g
  58. export const hyphenate = (str: string): string => {
  59. return str.replace(hyphenateRE, '-$1').toLowerCase()
  60. }
  61. export const capitalize = (str: string): string => {
  62. return str.charAt(0).toUpperCase() + str.slice(1)
  63. }
  64. // compare whether a value has changed, accounting for NaN.
  65. export const hasChanged = (value: any, oldValue: any): boolean =>
  66. value !== oldValue && (value === value || oldValue === oldValue)