2019-10-26 15:24:37 +00:00
|
|
|
import { makeMap } from './makeMap'
|
|
|
|
|
|
|
|
export { makeMap }
|
2019-10-01 01:17:12 +00:00
|
|
|
export * from './patchFlags'
|
2020-02-14 06:36:42 +00:00
|
|
|
export * from './shapeFlags'
|
2019-11-06 20:22:46 +00:00
|
|
|
export * from './globalsWhitelist'
|
|
|
|
export * from './codeframe'
|
2020-01-22 14:29:35 +00:00
|
|
|
export * from './mockWarn'
|
2020-01-27 22:23:42 +00:00
|
|
|
export * from './normalizeProp'
|
2020-01-28 23:48:27 +00:00
|
|
|
export * from './domTagConfig'
|
|
|
|
export * from './domAttrConfig'
|
2020-02-03 03:08:20 +00:00
|
|
|
export * from './escapeHtml'
|
2020-02-05 19:23:03 +00:00
|
|
|
export * from './looseEqual'
|
2020-05-04 14:38:03 +00:00
|
|
|
export * from './toDisplayString'
|
2019-10-01 01:17:12 +00:00
|
|
|
|
2020-07-06 19:56:24 +00:00
|
|
|
/**
|
|
|
|
* List of @babel/parser plugins that are used for template expression
|
|
|
|
* transforms and SFC script transforms. By default we enable proposals slated
|
|
|
|
* for ES2020. This will need to be updated as the spec moves forward.
|
|
|
|
* Full list at https://babeljs.io/docs/en/next/babel-parser#plugins
|
|
|
|
*/
|
|
|
|
export const babelParserDefautPlugins = [
|
|
|
|
'bigInt',
|
|
|
|
'optionalChaining',
|
|
|
|
'nullishCoalescingOperator'
|
2020-07-09 01:11:57 +00:00
|
|
|
] as const
|
2020-07-06 19:56:24 +00:00
|
|
|
|
2019-09-17 20:23:29 +00:00
|
|
|
export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
|
|
|
|
? Object.freeze({})
|
|
|
|
: {}
|
2019-05-28 09:19:47 +00:00
|
|
|
export const EMPTY_ARR: [] = []
|
2018-10-16 19:47:51 +00:00
|
|
|
|
|
|
|
export const NOOP = () => {}
|
|
|
|
|
2019-10-10 18:54:06 +00:00
|
|
|
/**
|
|
|
|
* Always return false.
|
|
|
|
*/
|
|
|
|
export const NO = () => false
|
|
|
|
|
2020-04-10 15:57:07 +00:00
|
|
|
const onRE = /^on[^a-z]/
|
|
|
|
export const isOn = (key: string) => onRE.test(key)
|
2018-10-17 16:20:54 +00:00
|
|
|
|
2020-06-10 20:54:23 +00:00
|
|
|
export const extend = Object.assign
|
2019-08-23 02:07:51 +00:00
|
|
|
|
2020-02-18 18:52:59 +00:00
|
|
|
export const remove = <T>(arr: T[], el: T) => {
|
|
|
|
const i = arr.indexOf(el)
|
|
|
|
if (i > -1) {
|
|
|
|
arr.splice(i, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:48:14 +00: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-16 19:47:51 +00:00
|
|
|
export const isArray = Array.isArray
|
2020-07-15 13:37:51 +00:00
|
|
|
export const isDate = (val: unknown): val is Date => val instanceof Date
|
2019-10-22 03:37:03 +00:00
|
|
|
export const isFunction = (val: unknown): val is Function =>
|
2018-10-16 19:47:51 +00:00
|
|
|
typeof val === 'function'
|
2019-10-22 03:37:03 +00: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-16 19:47:51 +00:00
|
|
|
val !== null && typeof val === 'object'
|
|
|
|
|
2020-02-17 19:43:16 +00:00
|
|
|
export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
|
2019-10-17 19:47:26 +00:00
|
|
|
return isObject(val) && isFunction(val.then) && isFunction(val.catch)
|
|
|
|
}
|
|
|
|
|
2019-09-23 19:36:30 +00:00
|
|
|
export const objectToString = Object.prototype.toString
|
|
|
|
export const toTypeString = (value: unknown): string =>
|
|
|
|
objectToString.call(value)
|
|
|
|
|
2020-02-17 19:43:16 +00:00
|
|
|
export const toRawType = (value: unknown): string => {
|
2019-10-25 15:15:04 +00:00
|
|
|
return toTypeString(value).slice(8, -1)
|
|
|
|
}
|
|
|
|
|
2019-10-22 03:37:03 +00:00
|
|
|
export const isPlainObject = (val: unknown): val is object =>
|
2019-09-23 19:36:30 +00:00
|
|
|
toTypeString(val) === '[object Object]'
|
|
|
|
|
2019-10-26 15:24:37 +00:00
|
|
|
export const isReservedProp = /*#__PURE__*/ makeMap(
|
|
|
|
'key,ref,' +
|
|
|
|
'onVnodeBeforeMount,onVnodeMounted,' +
|
|
|
|
'onVnodeBeforeUpdate,onVnodeUpdated,' +
|
|
|
|
'onVnodeBeforeUnmount,onVnodeUnmounted'
|
|
|
|
)
|
2019-06-03 05:44:45 +00:00
|
|
|
|
2020-02-17 19:43:16 +00:00
|
|
|
const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
|
2019-12-16 18:06:43 +00:00
|
|
|
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-16 19:47:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 18:06:43 +00:00
|
|
|
const camelizeRE = /-(\w)/g
|
2020-07-13 21:36:46 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2019-12-16 18:06:43 +00:00
|
|
|
export const camelize = cacheStringFunction(
|
|
|
|
(str: string): string => {
|
|
|
|
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-10-16 19:47:51 +00:00
|
|
|
const hyphenateRE = /\B([A-Z])/g
|
2020-07-13 21:36:46 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2019-12-16 18:06:43 +00:00
|
|
|
export const hyphenate = cacheStringFunction(
|
|
|
|
(str: string): string => {
|
|
|
|
return str.replace(hyphenateRE, '-$1').toLowerCase()
|
|
|
|
}
|
|
|
|
)
|
2018-10-16 19:47:51 +00:00
|
|
|
|
2020-07-13 21:36:46 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2019-12-16 18:06:43 +00:00
|
|
|
export const capitalize = cacheStringFunction(
|
|
|
|
(str: string): string => {
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
|
|
}
|
|
|
|
)
|
2019-10-23 15:53:43 +00: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-26 22:35:21 +00:00
|
|
|
|
2020-04-06 21:57:27 +00:00
|
|
|
export const invokeArrayFns = (fns: Function[], arg?: any) => {
|
2020-04-05 00:51:42 +00:00
|
|
|
for (let i = 0; i < fns.length; i++) {
|
|
|
|
fns[i](arg)
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 21:57:27 +00:00
|
|
|
|
|
|
|
export const def = (obj: object, key: string | symbol, value: any) => {
|
2020-05-02 20:16:51 +00:00
|
|
|
Object.defineProperty(obj, key, {
|
|
|
|
configurable: true,
|
2020-06-30 13:26:25 +00:00
|
|
|
enumerable: false,
|
2020-05-02 20:16:51 +00:00
|
|
|
value
|
|
|
|
})
|
2020-04-06 21:57:27 +00:00
|
|
|
}
|
2020-05-18 14:09:10 +00:00
|
|
|
|
|
|
|
export const toNumber = (val: any): any => {
|
|
|
|
const n = parseFloat(val)
|
|
|
|
return isNaN(n) ? val : n
|
|
|
|
}
|