00108-medium-trim

Back

type Whitespace = ' ' | '\n' | '\t';

type Trim<T extends string> =
    T extends `${Whitespace}${infer Rest}`
        ? Trim<Rest>
        : T extends `${infer Rest}${Whitespace}`
            ? Trim<Rest>
            : T;```

Solution by HrOkiG2 #35249

type TrimLeft<S extends string> = S extends `${infer Left}${infer Rest}`
  ? Left extends (' ' | '\n' | '\t') 
    ? TrimLeft<Rest>
    : S
  : S;

type TrimRight<S extends string> = LastChar<S> extends (' ' | '\n' | '\t') 
    ? TrimRight<WithoutLast<S>>
    : S;

type Trim<S extends string> = TrimLeft<TrimRight<S>>

type LastChar<T extends string> = T extends `${infer First}${infer Rest}` ?
  Rest extends '' ? First : LastChar<Rest> : '';

type WithoutLast<T extends string, Prev extends string = ''> = T extends `${infer First}${infer Rest}` ?
  Rest extends '' ? Prev : WithoutLast<Rest, `${Prev}${First}`> : '';

Solution by scape76 #35228

type Space = " " | "\n" | "\t";
type Trim<S extends string> = S extends
  | `${Space}${infer R}`
  | `${infer R}${Space}`
  ? Trim<R>
  : S;

Solution by LennyLee1998 #35177

type Space = ' ' | '\n' | '\t'
type TrimLeft<S extends string> = S extends `${Space}${infer R}` ? TrimLeft<R> : S
type TrimRight<S extends string> = S extends `${infer R}${Space}` ? TrimRight<R> : S
type Trim<S extends string> = TrimLeft<TrimRight<S>>

Solution by eunsukimme #35136

type Trim<S extends string> = S extends `${' ' | '\n' | '\t'}${infer R}` ? Trim<R> : S extends `${infer R}${' ' | '\n' | '\t'}` ? Trim<R> : S;

Solution by raeyoung-kim #35030

Trim rightside first, then trim leftside.

type TrimLeft<S extends string> = S extends `${' ' | '\n' | '\t'}${infer R}` ? TrimLeft<R> : S;
type TrimRight<S extends string> = S extends `${infer R}${' ' | '\n' | '\t'}` ? TrimRight<R> : S;
type Trim<S extends string> = TrimLeft<TrimRight<S>>;

Solution by dev-jaemin #34975

// your answers

type WhiteSpace = ' ' | '\n' | '\t'
type TrimLeft<S extends string> = S extends `${WhiteSpace}${infer Rest}` ? TrimLeft<Rest> : S;
type TrimRight<S extends string> = S extends `${infer Rest}${WhiteSpace}` ? TrimRight<Rest> : S;

type Trim<S extends string> = TrimLeft<TrimRight<S>>

Solution by hhk9292 #34974

// your answers
type Space = " " | "\n" | "\t";
type Trim<S extends string> = S extends `${Space}${infer R}`
  ? Trim<R>
  : S extends `${infer L}${Space}`
  ? Trim<L>
  : S;

Solution by zeyuanHong0 #34844

type Space = ' ' | '\t' | '\n';
type Trim<S extends string> = S extends `${Space}${infer A}` | `${infer A}${Space}` ? Trim<A> : S 

Solution by binhdv155127 #34646

type Space = " " | "\n" | "\t"

type Trim<S extends string> = S extends `${Space}${infer Remain}` | `${infer Remain}${Space}`  ? Trim<Remain> : S

Solution by devshinthant #34572

// 你的答案

type Space = ' ' | '\n' | '\t' type Trim = S extends ${Space}${infer A}|${infer A}${Space}? Trim:S

Solution by W-fitTiger #34259

type Whitespace = " " | "\n" | "\t";
type Trim<S> = S extends `${Whitespace}${infer R}`
  ? Trim<R>
  : S extends `${infer L}${Whitespace}`
  ? Trim<L>
  : S;

Solution by G00syara #34038

type TrimLeft<S extends string> = S extends `${' ' | '\n' | '\t'}${infer rest}` ? TrimLeft<rest> : S
type TrimRight<S extends string> = S extends `${infer rest}${' ' | '\n' | '\t'}` ? TrimRight<rest> : S
type Trim<S extends string> = TrimLeft<TrimRight<S>>

Solution by ouzexi #33995

type Space = ' ' | '\n\t' | '\t' | '\n'
type Trim<T extends string> = T extends `${Space}${infer R}`
  ? Trim<R>
  : T extends `${infer L}${Space}`
    ? Trim<L>
    : T

Solution by Danny101201 #33845

108 - Trim

by Anthony Fu (@antfu) #medium #template-literal

Question

Implement Trim<T> which takes an exact string type and returns a new string with the whitespace from both ends removed.

For example

type trimmed = Trim<'  Hello World  '> // expected to be 'Hello World'

...

type WS = ' '|'\n'|'\t';

type Trim<S extends string> = S extends `${WS}${infer R}` | `${infer R}${WS}`
  ? Trim<R>
  : S;

Solution by veralex #33776

type Space = ' ' | '\n' | '\t'; type Trim = T extends ${Space}${infer S}${Space} ? Trim : T

Solution by rookiewxy #33681

type TrimLeft<T extends string> = T extends ` ${infer Rest}`
  ? TrimLeft<Rest>
  : T

type TrimRight<T extends string> = T extends `${infer Rest} `
  ? TrimRight<Rest>
  : T

type Trim<T extends string> = TrimRight<TrimLeft<T>>

Solution by Flavour86 #33458

type Space = ' ' | '\t' | '\n';

type Trim<S extends string> = S extends `${Space}${infer word}` | `${infer word}${Space}` ? Trim<word> : S

Solution by loevray #33309

// your answers


type TrimLeft<S extends string> = S extends `${' ' | '\n' | '\t'}${infer R}` ? TrimLeft<R> : S
type TrimRight<S extends string> = S extends `${infer F}${' ' | '\n' | '\t'}` ? TrimRight<F> : S

type Trim<S extends string> = TrimLeft<TrimRight<S>>

Solution by KeithChou #33192

// 解答をここに記入
type Space = ' ' | '\n' | '\t';

type Trim<S extends string> = S extends `${Space}${infer A}`
  ? Trim<A>
  : S extends `${infer B}${Space}`
    ? Trim<B>
    : S;
  • 基本の考え方はTrimLeftの時と同じ。
  • 型変数Sがスペースの後に文字列が来ているかどうかを判定し、スペースがあれば、Trimを再帰処理で動作させる。
    • スペースがない場合でも、型変数Sの文字列の後にスペースがあるかもしれないので、さらに判定&再起処理を実施。
    • 上記どちらにも該当しない場合はそのまま返却する。

Solution by Yasunori-aloha #32901

type Space = ' ' | '\n' | '\t'
type Trim<S extends string> = S extends `${Space}${infer Rest}` 
                              ? Trim<Rest> 
                              : S extends `${infer First}${Space}` ? Trim<First> : S;

Solution by ZhipengYang0605 #32817

type Space = ' ' | '\n' | '\t'
type Trim<T extends string> = T extends `${infer S}${Space}`
    ? Trim<S>
    : T extends `${Space}${infer S}`
    ? Trim<S>
    : T

Solution by ZhulinskiiDanil #32676

type EmptyStrings = ' ' | '\n' | '\t';
type Trim<S extends string> = S extends `${EmptyStrings}${infer Sub}${EmptyStrings}` | `${EmptyStrings}${infer Sub}` | `${infer Sub}${EmptyStrings}`
  ? Trim<Sub>
  : S

Solution by keyurparalkar #32467

처음 풀이

type Trim<S extends string> = 
  S extends `${' ' | '\n' | '\t'}${infer RR}` 
    ? Trim<RR> 
    : S extends `${infer LR}${' ' | '\n' | '\t'}` 
      ? Trim<LR> 
      : S

더 간결한 풀이

type Space = ' ' | '\n' | '\t'
type Trim<S extends string> = 
  S extends `${Space}${infer R}` | `${infer R}${Space}`
    ? Trim<R> 
    : S

Solution by dev-hobin #32365

// your answers
type Trim<S extends string> =  S extends `${' ' | '\n' | '\t'}${infer U}` | `${infer U}${' ' | '\n' | '\t'}` ? Trim<U> : S

Solution by pea-sys #31970

type Space = ' ' | '\n' |'\t';

type Trim<S extends string> = S extends `${Space}${infer R}` | `${Space}${infer R}${Space}` | `${infer R}${Space}`
  ? Trim<R> 
  : S;

Solution by kai-phan #31626

type Space = '\n' | '\t' | ' '
type Trim<S extends string> = S extends `${Space}${infer A}${Space}` ? Trim<A> : S extends `${Space}${infer B}` ? Trim<B> : S extends `${infer C}${Space}` ? Trim<C> : S 

Solution by jinyoung234 #31584

// your answers

type Trim<T extends string> = T extends ` ${infer Text}` | `${infer Text} ` ? Trim<Text> : T;

Solution by AhmedRagabKamal #31497

type TrimLeft<T extends string> = T extends ` ${infer P}` ? TrimLeft<P> : T;
type Trim<T extends string> = T extends ` ${infer P} ` ? Trim<P> : T;

Solution by stormt #31457

type TrimLeft<T extends string> = T extends ` ${infer P}` ? TrimLeft<P> : T;

Solution by stormt #31456