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

46 lines
1.3 KiB
TypeScript
Raw Normal View History

export const EMPTY_OBJ: { readonly [key: string]: any } = Object.freeze({})
2019-05-28 17:19:47 +08:00
export const EMPTY_ARR: [] = []
export const NOOP = () => {}
export const reservedPropRE = /^(?:key|ref|slots)$|^vnode/
2018-10-18 00:20:54 +08:00
export const isOn = (key: string) => key[0] === 'o' && key[1] === 'n'
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'
2019-06-03 13:44:45 +08:00
export const isReservedProp = (key: string): boolean =>
key === 'key' || key === 'ref'
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)
}
2019-06-19 16:43:34 +08:00
export function invokeHandlers(
handlers: Function | Function[],
args: any[] = EMPTY_ARR
) {
if (isArray(handlers)) {
for (let i = 0; i < handlers.length; i++) {
handlers[i].apply(null, args)
}
} else {
handlers.apply(null, args)
}
}