type-challenges icon indicating copy to clipboard operation
type-challenges copied to clipboard

108 - Trim

Open bre30kra69cs opened this issue 5 years ago • 15 comments

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

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

bre30kra69cs avatar Dec 12 '20 21:12 bre30kra69cs

very good

surprisingwu avatar Jan 21 '21 12:01 surprisingwu

nice, thank you

2357619026 avatar May 10 '23 12:05 2357619026

666

Mario-Marion avatar May 15 '23 13:05 Mario-Marion

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

Why it didn't work?

zqqcee avatar Jun 02 '23 01:06 zqqcee

@zqqcee

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

Why it didn't work?

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

Mario-Marion avatar Jun 02 '23 02:06 Mario-Marion

Thanks!

zqqcee avatar Jun 10 '23 05:06 zqqcee

**zqqcee ** commented Jun 2, 2023

type TrimLeft<S extends string> = S extends `${Space}${infer R}` ? TrimLeft<R> : S
type TrimRight<S extends string> = S extends `${infer L}${Space}` ? TrimRight<L> : S
type Trim<S extends string> = TrimLeft<TrimRight<S>>

also works

123fdsgsd avatar Jul 24 '23 11:07 123fdsgsd

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

Why it didn't work?

My answer is:

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

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

xkyong avatar Oct 31 '23 14:10 xkyong

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

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

I'm a pig

YuFengDing avatar Mar 11 '24 09:03 YuFengDing

nice

Pozhan-js avatar May 26 '24 12:05 Pozhan-js

NiuBi!

pangxie231 avatar Aug 23 '24 04:08 pangxie231

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

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

lewton avatar Aug 24 '24 07:08 lewton

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

wksmile avatar Sep 09 '24 03:09 wksmile

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

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

It's always good to see a better solution. The trick was identifying the extra characters as a separate type and then write the recursive part.

my initial solution looked like this 😅

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

aamraei97 avatar Sep 19 '24 06:09 aamraei97

I solved it but in a very strange way. And probably inefficient

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

type TrimLeft<S extends string> = S extends `${infer F}${infer R}` ? 
F extends Space ? `${TrimLeft<R>}` : `${F}${R}`
: ''

type Reverse<S extends string> = S extends `${infer F}${infer R}` ?
`${Reverse<R>}${F}` : ''

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

LiamDochartaigh avatar Oct 27 '24 22:10 LiamDochartaigh