vue3-yuanma/packages/shared/src/index.ts

60 lines
1.7 KiB
TypeScript
Raw Normal View History

export * from './patchFlags'
export { globalsWhitelist } from './globalsWhitelist'
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: [] = []
export const NOOP = () => {}
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)
export const isArray = Array.isArray
export const isFunction = (val: any): val is Function =>
typeof val === 'function'
export const isString = (val: any): val is string => typeof val === 'string'
export const isObject = (val: any): val is Record<any, any> =>
val !== null && typeof val === 'object'
export const objectToString = Object.prototype.toString
export const toTypeString = (value: unknown): string =>
objectToString.call(value)
export const isPlainObject = (val: any): val is object =>
toTypeString(val) === '[object Object]'
2019-09-01 05:06:39 +08:00
const vnodeHooksRE = /^vnode/
2019-06-03 13:44:45 +08:00
export const isReservedProp = (key: string): boolean =>
2019-09-01 05:06:39 +08:00
key === 'key' || key === 'ref' || vnodeHooksRE.test(key)
2019-06-03 13:44:45 +08:00
const camelizeRE = /-(\w)/g
export const camelize = (str: string): string => {
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
}
const hyphenateRE = /\B([A-Z])/g
export const hyphenate = (str: string): string => {
return str.replace(hyphenateRE, '-$1').toLowerCase()
}
export const capitalize = (str: string): string => {
return str.charAt(0).toUpperCase() + str.slice(1)
}