Implement CamelCase<T>
which converts snake_case
string to camelCase
.
For example
type camelCase1 = CamelCase<'hello_world_with_types'> // expected to be 'helloWorldWithTypes'
type camelCase2 = CamelCase<'HELLO_WORLD_WITH_TYPES'> // expected to be same as previous one
/* _____________ Your Code Here _____________ */
type ToCamelCase<S extends string, ACC extends string = ''> = S extends `${infer F}${infer REST}`
? F extends '_'
? ToCamelCase<Capitalize<REST>, `${ACC}`>
: ToCamelCase<REST, `${ACC}${F}`>
: ACC;
type CamelCase<S extends string> = ToCamelCase<Lowercase<S>> // conver all chars to lower case first
type x = CamelCase<'FOOBAR'>
type y = CamelCase<'foo_bar_hello_world'>
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<CamelCase<'foobar'>, 'foobar'>>,
Expect<Equal<CamelCase<'FOOBAR'>, 'foobar'>>,
Expect<Equal<CamelCase<'foo_bar'>, 'fooBar'>>,
Expect<Equal<CamelCase<'foo_bar_hello_world'>, 'fooBarHelloWorld'>>,
Expect<Equal<CamelCase<'HELLO_WORLD_WITH_TYPES'>, 'helloWorldWithTypes'>>,
Expect<Equal<CamelCase<'-'>, '-'>>,
Expect<Equal<CamelCase<''>, ''>>,
Expect<Equal<CamelCase<'
标签:ACC,Typescript,ToCamelCase,_____________,CamelCase,99,Expect,type
From: https://www.cnblogs.com/Answer1215/p/16884249.html