| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- import { makeMap } from './makeMap'
- export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
- ? Object.freeze({})
- : {}
- export const EMPTY_ARR: readonly never[] = __DEV__ ? Object.freeze([]) : []
- export const NOOP = (): void => {}
- /**
- * Always return false.
- */
- export const NO = () => false
- export const isOn = (key: string): boolean =>
- key.charCodeAt(0) === 111 /* o */ &&
- key.charCodeAt(1) === 110 /* n */ &&
- // uppercase letter
- (key.charCodeAt(2) > 122 || key.charCodeAt(2) < 97)
- export const isModelListener = (key: string): key is `onUpdate:${string}` =>
- key.startsWith('onUpdate:')
- export const extend: typeof Object.assign = Object.assign
- export const remove = <T>(arr: T[], el: T): void => {
- 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: typeof Array.isArray = Array.isArray
- export const isMap = (val: unknown): val is Map<any, any> =>
- toTypeString(val) === '[object Map]'
- export const isSet = (val: unknown): val is Set<any> =>
- toTypeString(val) === '[object Set]'
- export const isDate = (val: unknown): val is Date =>
- toTypeString(val) === '[object Date]'
- export const isRegExp = (val: unknown): val is RegExp =>
- toTypeString(val) === '[object RegExp]'
- 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)) &&
- isFunction((val as any).then) &&
- isFunction((val as any).catch)
- )
- }
- export const objectToString: typeof Object.prototype.toString =
- Object.prototype.toString
- export const toTypeString = (value: unknown): string =>
- objectToString.call(value)
- export const toRawType = (value: unknown): string => {
- // extract "RawType" from strings like "[object RawType]"
- return toTypeString(value).slice(8, -1)
- }
- export const isPlainObject = (val: unknown): val is object =>
- toTypeString(val) === '[object Object]'
- export const isIntegerKey = (key: unknown): boolean =>
- isString(key) &&
- key !== 'NaN' &&
- key[0] !== '-' &&
- '' + parseInt(key, 10) === key
- export const isReservedProp: (key: string) => boolean = /*@__PURE__*/ makeMap(
- // the leading comma is intentional so empty string "" is also included
- ',key,ref,ref_for,ref_key,' +
- 'onVnodeBeforeMount,onVnodeMounted,' +
- 'onVnodeBeforeUpdate,onVnodeUpdated,' +
- 'onVnodeBeforeUnmount,onVnodeUnmounted',
- )
- export const isBuiltInDirective: (key: string) => boolean =
- /*@__PURE__*/ makeMap(
- 'bind,cloak,else-if,else,for,html,if,model,on,once,pre,show,slot,text,memo',
- )
- 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 T
- }
- const camelizeRE = /-(\w)/g
- /**
- * @private
- */
- export const camelize: (str: string) => string = cacheStringFunction(
- (str: string): string => {
- return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
- },
- )
- const hyphenateRE = /\B([A-Z])/g
- /**
- * @private
- */
- export const hyphenate: (str: string) => string = cacheStringFunction(
- (str: string) => str.replace(hyphenateRE, '-$1').toLowerCase(),
- )
- /**
- * @private
- */
- export const capitalize: <T extends string>(str: T) => Capitalize<T> =
- cacheStringFunction(<T extends string>(str: T) => {
- return (str.charAt(0).toUpperCase() + str.slice(1)) as Capitalize<T>
- })
- /**
- * @private
- */
- export const toHandlerKey: <T extends string>(
- str: T,
- ) => T extends '' ? '' : `on${Capitalize<T>}` = cacheStringFunction(
- <T extends string>(str: T) => {
- const s = str ? `on${capitalize(str)}` : ``
- return s as T extends '' ? '' : `on${Capitalize<T>}`
- },
- )
- // compare whether a value has changed, accounting for NaN.
- export const hasChanged = (value: any, oldValue: any): boolean =>
- !Object.is(value, oldValue)
- export const invokeArrayFns = (fns: Function[], ...arg: any[]): void => {
- for (let i = 0; i < fns.length; i++) {
- fns[i](...arg)
- }
- }
- export const def = (
- obj: object,
- key: string | symbol,
- value: any,
- writable = false,
- ): void => {
- Object.defineProperty(obj, key, {
- configurable: true,
- enumerable: false,
- writable,
- value,
- })
- }
- /**
- * "123-foo" will be parsed to 123
- * This is used for the .number modifier in v-model
- */
- export const looseToNumber = (val: any): any => {
- const n = parseFloat(val)
- return isNaN(n) ? val : n
- }
- /**
- * Only concerns number-like strings
- * "123-foo" will be returned as-is
- */
- export const toNumber = (val: any): any => {
- const n = isString(val) ? Number(val) : NaN
- return isNaN(n) ? val : n
- }
- // for typeof global checks without @types/node
- declare var global: {}
- let _globalThis: any
- export const getGlobalThis = (): any => {
- return (
- _globalThis ||
- (_globalThis =
- typeof globalThis !== 'undefined'
- ? globalThis
- : typeof self !== 'undefined'
- ? self
- : typeof window !== 'undefined'
- ? window
- : typeof global !== 'undefined'
- ? global
- : {})
- )
- }
- const identRE = /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/
- export function genPropsAccessExp(name: string): string {
- return identRE.test(name)
- ? `__props.${name}`
- : `__props[${JSON.stringify(name)}]`
- }
- export function genCacheKey(source: string, options: any): string {
- return (
- source +
- JSON.stringify(options, (_, val) =>
- typeof val === 'function' ? val.toString() : val,
- )
- )
- }
|