| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import { makeMap } from './makeMap'
- export { makeMap }
- export * from './patchFlags'
- export * from './shapeFlags'
- export * from './globalsWhitelist'
- export * from './codeframe'
- export * from './mockWarn'
- export * from './normalizeProp'
- export * from './domTagConfig'
- export * from './domAttrConfig'
- export * from './escapeHtml'
- export * from './looseEqual'
- export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
- ? Object.freeze({})
- : {}
- export const EMPTY_ARR: [] = []
- export const NOOP = () => {}
- /**
- * Always return false.
- */
- export const NO = () => false
- const onRE = /^on[^a-z]/
- export const isOn = (key: string) => onRE.test(key)
- export const extend = <T extends object, U extends object>(
- a: T,
- b: U
- ): T & U => {
- for (const key in b) {
- ;(a as any)[key] = b[key]
- }
- return a as any
- }
- export const remove = <T>(arr: T[], el: T) => {
- const i = arr.indexOf(el)
- if (i > -1) {
- arr.splice(i, 1)
- }
- }
- const hasOwnProperty = Object.prototype.hasOwnProperty
- export const hasOwn = (
- val: object,
- key: string | symbol
- ): key is keyof typeof val => hasOwnProperty.call(val, key)
- export const isArray = Array.isArray
- export const isFunction = (val: unknown): val is Function =>
- typeof val === 'function'
- export const isString = (val: unknown): val is string => typeof val === 'string'
- export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
- export const isObject = (val: unknown): val is Record<any, any> =>
- val !== null && typeof val === 'object'
- export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
- return isObject(val) && isFunction(val.then) && isFunction(val.catch)
- }
- export const objectToString = Object.prototype.toString
- export const toTypeString = (value: unknown): string =>
- objectToString.call(value)
- export const toRawType = (value: unknown): string => {
- return toTypeString(value).slice(8, -1)
- }
- export const isPlainObject = (val: unknown): val is object =>
- toTypeString(val) === '[object Object]'
- export const isReservedProp = /*#__PURE__*/ makeMap(
- 'key,ref,' +
- 'onVnodeBeforeMount,onVnodeMounted,' +
- 'onVnodeBeforeUpdate,onVnodeUpdated,' +
- 'onVnodeBeforeUnmount,onVnodeUnmounted'
- )
- const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
- const cache: Record<string, string> = Object.create(null)
- return ((str: string) => {
- const hit = cache[str]
- return hit || (cache[str] = fn(str))
- }) as any
- }
- const camelizeRE = /-(\w)/g
- export const camelize = cacheStringFunction(
- (str: string): string => {
- return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
- }
- )
- const hyphenateRE = /\B([A-Z])/g
- export const hyphenate = cacheStringFunction(
- (str: string): string => {
- return str.replace(hyphenateRE, '-$1').toLowerCase()
- }
- )
- export const capitalize = cacheStringFunction(
- (str: string): string => {
- return str.charAt(0).toUpperCase() + str.slice(1)
- }
- )
- // compare whether a value has changed, accounting for NaN.
- export const hasChanged = (value: any, oldValue: any): boolean =>
- value !== oldValue && (value === value || oldValue === oldValue)
- // for converting {{ interpolation }} values to displayed strings.
- export const toDisplayString = (val: unknown): string => {
- return val == null
- ? ''
- : isArray(val) || (isPlainObject(val) && val.toString === objectToString)
- ? JSON.stringify(val, null, 2)
- : String(val)
- }
- export const invokeArrayFns = (fns: Function[], arg?: any) => {
- for (let i = 0; i < fns.length; i++) {
- fns[i](arg)
- }
- }
- export const def = (obj: object, key: string | symbol, value: any) => {
- Object.defineProperty(obj, key, { value })
- }
|