03326-medium-bem-style-string

Back

type BEM<B extends string, E extends string[], M extends string[]> = `${B}${E['length'] extends 0 ? '' : `__${E[number]}` }${M['length'] extends 0  ? "" : `--${M[number]}`}`

Solution by devshinthant #35259

type BEM<B extends string, E extends string[], M extends string[]> = `
  ${B}
  ${E extends [] ? '' : `__${E[number]}`}
  ${M extends [] ? '' : `--${M[number]}`}
`;
便于理解

Solution by wendao-liu #35060

type BEM<B extends string, E extends string[], M extends string[]> = `${B}${E extends [] ? '' : `__${E[number]}`}${M extends [] ? '' : `--${M[number]}`}`

Solution by devshinthant #34982

type BEM<B extends string, E extends string[], M extends string[]> = `${B}${E extends [] ? '' : `__${E[number]}`}${M extends [] ? '' : `--${M[number]}`}`

Solution by maximallain #34793

type Contact<T extends string, A extends string[] = [], S extends string = '__'> = A['length'] extends 0 ? T : `${T}${S}${A[number]}`

type BEM<B extends string, E extends string[], M extends string[]> = Contact<Contact<B, E>, M, '--'>

Solution by jzllove9 #34792

// your answers
type BEM<B extends string, E extends string[], M extends string[]> = E extends [infer F] ?
  M extends [] ? `${B}__${F & string}` : `${B}__${F & string}--${M[number]}`
  : `${B}--${M[number]}`



Solution by Jayce-liang #34777

Much easier than I expected

type BEM<B extends string, E extends string[], M extends string[]> = `${B}${E extends [] ? '' : `__${E[number]}`}${M extends [] ? '' : `--${M[number]}`}`
image

Solution by Git-I985 #34700

type WrapStr<A extends string, prefix extends string = ''> = [A] extends [never] ? '' : `${prefix}${A}`
type BEM<B extends string, E extends string[], M extends string[]> = `${B}${WrapStr<E[number], '__'>}${WrapStr<M[number], '--'>}`

Solution by ndfhsledk #34086

type BEM<B extends string, E extends string[], M extends string[]> = `${B}${E extends [] ? '' : `__${E[number]}`}${M extends [] ? '' : `--${M[number]}`}`

Solution by ouzexi #34064

// ============= Test Cases ============= import type { Equal, Expect } from './test-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' >>, ]

// ============= Your Code Here ============= type Format<L extends string[],S extends string> = L extends [] ? '':${S}${L[number]} type BEM<B extends string, E extends string[], M extends string[]> = ${B}${Format<E,'__'>}${Format<M, '--'>}

Solution by lijianhu1 #33790

// 你的答案
type Arr2Union<T> = T extends any[] ? T[number] : T;

type BEM<B extends string, E extends string[], M extends string[]> = E['length'] extends 0 ? `${B}--${Arr2Union<M>}` :  M['length'] extends 0 ? `${B}__${Arr2Union<E>}` : `${B}__${Arr2Union<E>}--${Arr2Union<M>}`

Solution by HelloGGG #33565

type Splicing<S extends string, T extends any[]> = T["length"] extends 0
  ? ""
  : `${S}${T[number]}`
type BEM<
  B extends string,
  E extends string[],
  M extends string[]
> = `${B}${Splicing<"__", E>}${Splicing<"--", M>}`

Solution by jinhuid #33214

// your answers
type BEM<B extends string, E extends string[], M extends string[]> = `${B}${E extends [] ? '' : `__${E[number]}`}${M extends [] ? '' : `--${M[number]}`}`;

Solution by pea-sys #32968

type BEM<B extends string, E extends string[], M extends string[]> = `${B}${E extends [] ? '' : `__${E[number]}`}${M extends [] ? '' : `--${M[number]}`}`;

Solution by kai-phan #32514

type Join<B extends string, E extends string[], J extends string> = 
  E extends [] ? B : `${B}${J}${E[number]}`
type BEM<B extends string, E extends string[], M extends string[]> = Join<Join<B, E, '__'>, M, '--'>

Solution by action-hong #32141

type isNever<T> = [T] extends [never] ? true : false;
type Converter<Prefix extends string, A extends string> = 
  isNever<A> extends true
    ? ''
    : `${Prefix}${A}`;
type BEM<B extends string, E extends string[], M extends string[]> = 
  `${B}${Converter<'__', E extends Array<infer U> ? U : never>}${Converter<'--', M extends Array<infer U> ? U : never>}`

Solution by ricky-fn #32106

// your answers

type TupleToUnion<T extends any[]> = T extends [infer F, ...infer R]
  ? F | TupleToUnion<R>
  : never;

type BE<
  B extends string,
  E extends string[],
  divider extends string
> = E extends [infer F extends string, ...infer R extends string[]]
  ? [`${B}${divider}${F}`, ...BE<B, R, divider>]
  : E;

type EM<E extends string[], M extends string[]> = E extends [
  infer EF extends string,
  ...infer ER extends string[]
]
  ? [...BE<EF, M, "--">, ...EM<ER, M>]
  : E;

type BEM<
  B extends string,
  E extends string[],
  M extends string[]
> = TupleToUnion<
  E["length"] extends 0
    ? BE<B, M, "--">
    : M["length"] extends 0
    ? BE<B, E, "__">
    : EM<BE<B, E, "__">, M>
>;


Solution by chenqy-yh #31215

type HyphenMapper<S extends string, M extends string[], R extends string[] = []> = 
  M extends [infer F, ...infer Rest] ?
  Rest extends string[] ?
  F extends string ?
  HyphenMapper<S, Rest, [...R, `${S}--${F}`]> :
  never :
  never :
  R[number]

type ExtendByHyphen<S extends string, M extends string[], R extends string[] = []> =
  M extends [infer F, ...infer Rest] ?
  HyphenMapper<S, M, R> : 
  S

type BEM<B extends string, E extends string[], M extends string[]> = 
  E extends [infer F, ...infer Rest] ?
  F extends string ?
  ExtendByHyphen<`${B}__${F}`, M> :
  ExtendByHyphen<B, M> :
  ExtendByHyphen<B, M>


/* _____________ 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' >>,
]

Solution by gearonixx #30887

// your answers
type PutPrefix<T extends string[], Prefix extends string> = T extends [] ? '' : `${Prefix}${T[number]}`

type BEM<B extends string, E extends string[], M extends string[]> = `${B}${PutPrefix<E, '__'>}${PutPrefix<M, '--'>}`

Solution by Kakeru-Miyazaki #30884

type Element<T extends string[]> = T extends [] ? '' : `__${T[number]}`
type Modifier<T extends string[]> = T extends [] ? '' : `--${T[number]}`

type BEM<B extends string, E extends string[], M extends string[]> = `${B}${Element<E>}${Modifier<M>}`

Solution by matallui #30807

type AdjustedBEMComponent<T extends string | never, S extends ("--" | "__")> = [T] extends [never] ? "" : `${S}${T}`
type BEM<B extends string, E extends string[], M extends string[]> =  
  `${B}${AdjustedBEMComponent<E[number], "__">}${AdjustedBEMComponent<M[number], "--">}`

Solution by wouldRewind #30792

// your answers
type BEM<B extends string, E extends string[], M extends string[]> = 
  E extends [] 
    ? M extends [] 
      ? `${B}`
      : `${B}--${M[number]}`
    : M extends [] 
      ? `${B}__${E[number]}`
      : `${B}__${E[number]}--${M[number]}`

Solution by GodAraden #30787

type BEM<B extends string, E extends string[], M extends string[]> = `${B}${E['length'] extends 0 ? '' : `__${E[number]}`}${M['length'] extends 0 ? '' : `--${M[number]}`}`

Solution by dreamluo-plus #30623

type GenerateLine<B extends string, E extends string[], Line extends string = '__'> = 
  E extends [infer Head, ...infer Other]
    ? Other extends string[]
      ? Head extends string
        ? `${B}${Line}${Head}` | GenerateLine<B, Other, Line>
        : never
      : never
    : never


type BEM<B extends string, E extends string[], M extends string[]> = 
  M['length'] extends 0
    ? E['length'] extends 0
      ? B
      : GenerateLine<B, E>
    : E['length'] extends 0
      ? GenerateLine<B, M, '--'>
      : GenerateLine<GenerateLine<B, E>, M, '--'>

Solution by yang49519845 #30150

type BEM<B extends string, E extends string[], M extends string[]> = 
  M extends [infer F extends string, ...infer O extends string[]] ?
    `${BEM<B, E, []>}--${F}` | `${BEM<B, E, O extends [] ? never : O>}` :
    E extends [infer F extends string, ...infer O extends string[]] ?
      `${BEM<B, [], M>}__${F}` | `${BEM<B, O extends [] ? never : O, M>}` :
      `${B}`

Solution by 8471919 #30014

Solution:

type BEM<B extends string, E extends string[], M extends string[]> = `${B}${E extends []
  ? ''
  : { [K in keyof E]: `__${E[K]}` }[number]}${M extends [] ? '' : { [K in keyof M]: `--${M[K]}` }[number]}`

Solution by DmitriiBr #29797

type BEM<B extends string, E extends string[], M extends string[]> =
  `${B}${E[number] extends never ? '' : `__${E[number]}`}${M[number] extends never ? '' : `--${M[number]}`}`

Solution by hesoso #29791

// your answers
type BEM<B extends string, E extends string[], M extends string[]> = E extends [] ? `${B}--${M[number]}` : M extends [] ? `${B}__${E[number]}` : `${B}__${E[number]}--${M[number]}`

Solution by eodnjs467 #29449

// your answers

type BEM<B extends string, E extends string[], M extends string[]> =
           `${B}${E["length"] extends  0 ? "" : `__${E[number]}`}${M["length"] extends 0 ? "" : `--${M[number]}`}`

Solution by kerolossamir165 #29280

// your answers
type BEM<B extends string, E extends string[], M extends string[]> =
   E extends [] ?  
    `${B}--${M[number]}` 
    : M extends [] ? 
      `${B}__${E[number]}` 
      : `${B}__${E[number]}--${M[number]}`;

Solution by eodnjs467 #29144