1 Star 1 Fork 0

wangzichu/ts-challenges

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
3326.BEMStyleString.ts 2.50 KB
一键复制 编辑 原始数据 按行查看 历史
王子初 提交于 2023-04-20 18:23 . 第二次提交
/*
* @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @Date: 2023-04-19 11:27:32
* @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @LastEditTime: 2023-04-19 15:01:16
* @FilePath: /ts-challenges/3326.BEMStyleString.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
/*
3326 - BEM style string
-------
by Songhn (@songhn233) #medium #template-literal #union #tuple
### Question
The Block, Element, Modifier methodology (BEM) is a popular naming convention for classes in CSS.
For example, the block component would be represented as `btn`, element that depends upon the block would be represented as `btn__price`, modifier that changes the style of the block would be represented as `btn--big` or `btn__price--warning`.
Implement `BEM<B, E, M>` which generate string union from these three parameters. Where `B` is a string literal, `E` and `M` are string arrays (can be empty).
> View on GitHub: https://tsch.js.org/3326
*/
/* _____________ Your Code Here _____________ */
type isNeverToStringM<T extends string> = [T] extends [never] ? '' : `--${T}`
type isNeverToStringE<T extends string> = [T] extends [never] ? '' : `__${T}`
type BEMObj<B extends string, E extends string[], M extends string[]> = {[k in `${B}${isNeverToStringE<E[number]>}${isNeverToStringM<M[number]>}`]:`${B}${isNeverToStringE<E[number]>}${isNeverToStringM<M[number]>}`}
type BEM<B extends string, E extends string[], M extends string[]> = BEMObj<B,E,M>[keyof BEMObj<B,E,M>]
type test = BEM<'btn', ['price'], []>
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<BEM<'btn', ['price'], []>, 'btn__price'>>,
Expect<Equal<BEM<'btn', ['price'], ['warning', 'success']>, 'btn__price--warning' | 'btn__price--success' >>,
Expect<Equal<BEM<'btn', [], ['small', 'medium', 'large']>, 'btn--small' | 'btn--medium' | 'btn--large' >>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/3326/answer
> View solutions: https://tsch.js.org/3326/solutions
> More Challenges: https://tsch.js.org
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wang-zichu/ts-challenges.git
git@gitee.com:wang-zichu/ts-challenges.git
wang-zichu
ts-challenges
ts-challenges
master

搜索帮助