首页 > 其他分享 >[Typescript] 37. Medium - KebabCase *

[Typescript] 37. Medium - KebabCase *

时间:2022-09-26 03:11:05浏览次数:49  
标签:Typescript bar KebabCase 37 REST Medium Expect foo type

Replace the camelCase or PascalCase string with kebab-case.

FooBarBaz -> foo-bar-baz

For example

type FooBarBaz = KebabCase<"FooBarBaz">;
const foobarbaz: FooBarBaz = "foo-bar-baz";

type DoNothing = KebabCase<"do-nothing">;
const doNothing: DoNothing = "do-nothing";

 

For the First letter, we just need to use lowercase, for the rest, we check if REST is the same as Uncapitalize version of REST, then keep the same, otherwise add '-' in between.

type KebabCase<S> = S extends `${infer First}${infer REST}` 
  ? REST extends Uncapitalize<REST> 
    ? `${Lowercase<First>}${KebabCase<REST>}`
    : `${Lowercase<First>}-${KebabCase<REST>}`
  : S;

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<KebabCase<'FooBarBaz'>, 'foo-bar-baz'>>,
  Expect<Equal<KebabCase<'fooBarBaz'>, 'foo-bar-baz'>>,
  Expect<Equal<KebabCase<'foo-bar'>, 'foo-bar'>>,
  Expect<Equal<KebabCase<'foo_bar'>, 'foo_bar'>>,
  Expect<Equal<KebabCase<'Foo-Bar'>, 'foo--bar'>>,
  Expect<Equal<KebabCase<'ABC'>, 'a-b-c'>>,
  Expect<Equal<KebabCase<'-'>, '-'>>,
  Expect<Equal<KebabCase<''>, ''>>,
  Expect<Equal<KebabCase<'

标签:Typescript,bar,KebabCase,37,REST,Medium,Expect,foo,type
From: https://www.cnblogs.com/Answer1215/p/16729573.html

相关文章

  • TypeScript type 关键字
    联合类型、对象类型等都有可能不止用一次,type关键字给类型声明一个别名,就不需要再次声明同样的类型,直接引用类型别名。我在某个时候写了一个函数,其签名param是一个联合......
  • 二、typescript类(一)属性和方法
    一、概述:1.类的创建:使用class关键字定义一个类;1classPersion{2}2.对象中主要包含了两个部分:属性 方法3.属性可以分为:静态属性和实例属性;直接定义的属性......
  • Typescript类型体操 - BEM style string
    题目中文Block,Element,Modifier方法(BEM)是CSS中类的流行命名约定。例如,块组件用btn表示,依赖于块的元素用btn__price表示,更改块样式的修饰符将用btn--big或者btn......
  • RMAN-08137
    RMAN-08137:warning:archivedlognotdeleted,neededforstandbyorupstreamcaptureprocessarchivedlogfilename=+ARCH/MARC/ARCHIVELOG/2022_09_21/thread_2_......
  • [Typescript] 36. Medium - Merge
    Mergetwotypesintoanewtype.Keysofthesecondtypeoverrideskeysofthefirsttype.Forexampletypefoo={name:string;age:string;}typecoo=......
  • Typescript类型体操 - FlattenDepth
    题目中文递归将数组展开到指定的深度示例:typea=FlattenDepth<[1,2,[3,4],[[[5]]]],2>;//[1,2,3,4,[5]].flattern2timestypeb=FlattenDepth<[1,......
  • TypeScript Array数组 生成两个数组的交集,并且在数组中进行删除操作
    TypeScriptArray数组 生成两个数组的交集,并且在数组中进行删除操作 /***@methodcutArr删除数组1中,与数组2重复的数据*Arr([1,2,3,5],[2,3,4])=>[1,5......
  • TypeScript Array 数组 两个数组取交集
    TypeScriptArray数组两个数组取交集   //取交集  privateArrayIntersection(a,b)  {    varai=0,bi=0;    varresult=new......
  • React Typescript 中的依赖道具
    ReactTypescript中的依赖道具一个依赖的道具是一个prop,只有在另一个prop具有特定值时才应设置。在下面的示例中,我们可以看到一个用例(单击[打开沙盒](https://code......
  • Typescript类中extends和implements的作用
    在ES6中,类的继承可以通过extends实现。classAnimal{name;sayHello(){}}classDogextendsAnimal{}//constdog=newDog();//在Dog的实例dog......