2019-10-26 23:24:37 +08:00
|
|
|
import { makeMap } from './makeMap'
|
|
|
|
|
|
|
|
export { makeMap }
|
2019-10-01 09:17:12 +08:00
|
|
|
export * from './patchFlags'
|
2019-11-07 04:22:46 +08:00
|
|
|
export * from './globalsWhitelist'
|
|
|
|
export * from './codeframe'
|
2020-01-22 22:29:35 +08:00
|
|
|
export * from './mockWarn'
|
2020-01-28 06:23:42 +08:00
|
|
|
export * from './normalizeProp'
|
2020-01-29 07:48:27 +08:00
|
|
|
export * from './domTagConfig'
|
|
|
|
export * from './domAttrConfig'
|
2019-10-01 09:17:12 +08:00
|
|
|
|
2019-09-18 04:23:29 +08:00
|
|
|
export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
|
|
|
|
? Object.freeze({})
|
|
|
|
: {}
|
2019-05-28 17:19:47 +08:00
|
|
|
export const EMPTY_ARR: [] = []
|
2018-10-17 03:47:51 +08:00
|
|
|
|
|
|
|
export const NOOP = () => {}
|
|
|
|
|
2019-10-11 02:54:06 +08:00
|
|
|
/**
|
|
|
|
* Always return false.
|
|
|
|
*/
|
|
|
|
export const NO = () => false
|
|
|
|
|
2018-10-18 00:20:54 +08:00
|
|
|
export const isOn = (key: string) => key[0] === 'o' && key[1] === 'n'
|
|
|
|
|
2019-08-23 10:07:51 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-06 08:48:14 +08:00
|
|
|
const hasOwnProperty = Object.prototype.hasOwnProperty
|
|
|
|
export const hasOwn = (
|
|
|
|
val: object,
|
|
|
|
key: string | symbol
|
|
|
|
): key is keyof typeof val => hasOwnProperty.call(val, key)
|
|
|
|
|
2018-10-17 03:47:51 +08:00
|
|
|
export const isArray = Array.isArray
|
2019-10-22 11:37:03 +08:00
|
|
|
export const isFunction = (val: unknown): val is Function =>
|
2018-10-17 03:47:51 +08:00
|
|
|
typeof val === 'function'
|
2019-10-22 11:37:03 +08:00
|
|
|
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> =>
|
2018-10-17 03:47:51 +08:00
|
|
|
val !== null && typeof val === 'object'
|
|
|
|
|
2019-10-22 11:37:03 +08:00
|
|
|
export function isPromise<T = any>(val: unknown): val is Promise<T> {
|
2019-10-18 03:47:26 +08:00
|
|
|
return isObject(val) && isFunction(val.then) && isFunction(val.catch)
|
|
|
|
}
|
|
|
|
|
2019-09-24 03:36:30 +08:00
|
|
|
export const objectToString = Object.prototype.toString
|
|
|
|
export const toTypeString = (value: unknown): string =>
|
|
|
|
objectToString.call(value)
|
|
|
|
|
2019-10-25 23:15:04 +08:00
|
|
|
export function toRawType(value: unknown): string {
|
|
|
|
return toTypeString(value).slice(8, -1)
|
|
|
|
}
|
|
|
|
|
2019-10-22 11:37:03 +08:00
|
|
|
export const isPlainObject = (val: unknown): val is object =>
|
2019-09-24 03:36:30 +08:00
|
|
|
toTypeString(val) === '[object Object]'
|
|
|
|
|
2019-10-26 23:24:37 +08:00
|
|
|
export const isReservedProp = /*#__PURE__*/ makeMap(
|
|
|
|
'key,ref,' +
|
|
|
|
'onVnodeBeforeMount,onVnodeMounted,' +
|
|
|
|
'onVnodeBeforeUpdate,onVnodeUpdated,' +
|
|
|
|
'onVnodeBeforeUnmount,onVnodeUnmounted'
|
|
|
|
)
|
2019-06-03 13:44:45 +08:00
|
|
|
|
2019-12-17 02:06:43 +08:00
|
|
|
function 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
|
2018-10-17 03:47:51 +08:00
|
|
|
}
|
|
|
|
|
2019-12-17 02:06:43 +08:00
|
|
|
const camelizeRE = /-(\w)/g
|
|
|
|
export const camelize = cacheStringFunction(
|
|
|
|
(str: string): string => {
|
|
|
|
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-10-17 03:47:51 +08:00
|
|
|
const hyphenateRE = /\B([A-Z])/g
|
2019-12-17 02:06:43 +08:00
|
|
|
export const hyphenate = cacheStringFunction(
|
|
|
|
(str: string): string => {
|
|
|
|
return str.replace(hyphenateRE, '-$1').toLowerCase()
|
|
|
|
}
|
|
|
|
)
|
2018-10-17 03:47:51 +08:00
|
|
|
|
2019-12-17 02:06:43 +08:00
|
|
|
export const capitalize = cacheStringFunction(
|
|
|
|
(str: string): string => {
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
|
|
}
|
|
|
|
)
|
2019-10-23 23:53:43 +08:00
|
|
|
|
|
|
|
// compare whether a value has changed, accounting for NaN.
|
|
|
|
export const hasChanged = (value: any, oldValue: any): boolean =>
|
|
|
|
value !== oldValue && (value === value || oldValue === oldValue)
|
2020-01-27 06:35:21 +08:00
|
|
|
|
|
|
|
// for converting {{ interpolation }} values to displayed strings.
|
|
|
|
export function toDisplayString(val: unknown): string {
|
|
|
|
return val == null
|
|
|
|
? ''
|
|
|
|
: isArray(val) || (isPlainObject(val) && val.toString === objectToString)
|
|
|
|
? JSON.stringify(val, null, 2)
|
|
|
|
: String(val)
|
|
|
|
}
|