首页 > 其他分享 >[Typescript] 99. Hard - CamelCase

[Typescript] 99. Hard - CamelCase

时间:2022-11-12 17:45:33浏览次数:43  
标签:ACC Typescript ToCamelCase _____________ CamelCase 99 Expect type

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

相关文章

  • 麒麟V10_SP1_4.19.71-9-KR990 root用户运行qt程序,提示需要授权,请问如何授权?
    麒麟V10_SP1_4.19.71-9-KR990root用户运行qt程序,提示需要授权,但未指定授权协议,普通用户运行同一个qt程序正常。请问如何授权?请麒麟系统技术老师解惑。例子如下:在系统自带qt......
  • [Typescript] 98. Medium - Append to object
    Implementatypethataddsanewfieldtotheinterface.Thetypetakesthethreearguments.Theoutputshouldbeanobjectwiththenewfield.Forexamplety......
  • Typescript类型体操 - Unique
    题目中文实现类型的Lodash.uniq,Unique接受数组T,返回没有重复值的数组TEnglishImplementthetypeversionofLodash.uniq,UniquetakesanArrayT,returns......
  • Typescript类型体操 - LastIndexOf
    题目中文实现类型的Array.lastIndexOf,LastIndexOf<T,U>接受泛型参数ArrayT和anyU并返回数组T中最后一个U的索引示例:typeRes1=LastIndexOf<[1,2,3......
  • 【TS】1103- 30个小知识让你更清楚TypeScript
    TypeScript是Microsoft开发的JavaScript的开源超集,用于在不破坏现有程序的情况下添加附加功能。由于其独特的优势,例如,静态类型和许多速记符号,TypeScript现在被前端和......
  • [Typescript] Zod in actions
    import{z}from"zod";exportenumSUBTYPE{ABORT="abort",START="start",UPLOAD="upload",LOADING="loading",}exportconstTYPE="print"......
  • typescript装饰器
    属性装饰器参数exportdefaultfunction(proto,key){//两个参数}给属性增加metadataimport'reflect-metadata';exportdefaultfunction(label,type?......
  • [Typescript] 97. Hard - Capitalize Words
    Implement CapitalizeWords<T> whichconvertsthefirstletterof eachwordofastring touppercaseandleavestherestas-is.Forexampletypecapitalized......
  • 安装 TypeScript 并编译成JS
    官网:https://github.com/microsoft/TypeScriptTypeScript是一种由微软开发的开源、跨平台的编程语言。它是JavaScript的超集,最终会被编译为JavaScript代码。TypeScript......
  • OJ中Typescript语法整理
    基础原始类型原始类型:number/string/boolean/null/undefined/symbol对象类型:oject(数组,对象,函数等)自定义复杂的对象类型:typeCustomArray=(number|string)let......