Skip to content

Commit

Permalink
types(shared): improve capitalize types
Browse files Browse the repository at this point in the history
  • Loading branch information
tolking committed Jul 1, 2022
1 parent 2eb3322 commit ec3e6fe
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export const isMap = (val: unknown): val is Map<any, any> =>
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 isDate = (val: unknown): val is Date =>
toTypeString(val) === '[object Date]'
export const isFunction = (val: unknown): val is Function =>
typeof val === 'function'
export const isString = (val: unknown): val is string => typeof val === 'string'
Expand Down Expand Up @@ -99,7 +100,7 @@ const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
return ((str: string) => {
const hit = cache[str]
return hit || (cache[str] = fn(str))
}) as any
}) as T
}

const camelizeRE = /-(\w)/g
Expand All @@ -121,16 +122,17 @@ export const hyphenate = cacheStringFunction((str: string) =>
/**
* @private
*/
export const capitalize = cacheStringFunction(
(str: string) => str.charAt(0).toUpperCase() + str.slice(1)
)
export const capitalize = cacheStringFunction(<T extends string>(str: T) => {
return (str.charAt(0).toUpperCase() + str.slice(1)) as Capitalize<T>
})

/**
* @private
*/
export const toHandlerKey = cacheStringFunction((str: string) =>
str ? `on${capitalize(str)}` : ``
)
export const toHandlerKey = 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 =>
Expand Down

0 comments on commit ec3e6fe

Please sign in to comment.