Skip to content

Commit 62e1f2e

Browse files
committed
add intermediate trim ts challenges
1 parent afef101 commit 62e1f2e

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
106 - Trim Left
3+
-------
4+
by Anthony Fu (@antfu) #medium #template-literal
5+
6+
### Question
7+
8+
Implement `TrimLeft<T>` which takes an exact string type and returns a new string with the whitespace beginning removed.
9+
10+
For example
11+
12+
```ts
13+
type trimed = TrimLeft<' Hello World '> // expected to be 'Hello World '
14+
```
15+
16+
> View on GitHub: https://tsch.js.org/106
17+
*/
18+
19+
/* _____________ Your Code Here _____________ */
20+
21+
type TrimLeft<S extends string> = S extends `${infer A}${infer B}`
22+
? A extends " " | "\n" | "\t"
23+
? TrimLeft<B>
24+
: S
25+
: S;
26+
type trimed = TrimLeft<" Hello World ">; // expected to be 'Hello World '
27+
28+
/* _____________ Test Cases _____________ */
29+
import type { Equal, Expect } from "@type-challenges/utils";
30+
31+
type cases = [
32+
Expect<Equal<TrimLeft<"str">, "str">>,
33+
Expect<Equal<TrimLeft<" str">, "str">>,
34+
Expect<Equal<TrimLeft<" str">, "str">>,
35+
Expect<Equal<TrimLeft<" str ">, "str ">>,
36+
Expect<Equal<TrimLeft<" \n\t foo bar ">, "foo bar ">>,
37+
Expect<Equal<TrimLeft<"">, "">>,
38+
Expect<Equal<TrimLeft<" \n\t">, "">>
39+
];
40+
41+
/* _____________ Further Steps _____________ */
42+
/*
43+
> Share your solutions: https://tsch.js.org/106/answer
44+
> View solutions: https://tsch.js.org/106/solutions
45+
> More Challenges: https://tsch.js.org
46+
*/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
108 - Trim
3+
-------
4+
by Anthony Fu (@antfu) #medium #template-literal
5+
6+
### Question
7+
8+
Implement `Trim<T>` which takes an exact string type and returns a new string with the whitespace from both ends removed.
9+
10+
For example
11+
12+
```ts
13+
type trimmed = Trim<' Hello World '> // expected to be 'Hello World'
14+
```
15+
16+
> View on GitHub: https://tsch.js.org/108
17+
*/
18+
19+
/* _____________ Your Code Here _____________ */
20+
type WhiteSpace = " " | "\n" | "\t" | "\r";
21+
22+
type TrimLeft<S extends string> = S extends `${infer A}${infer B}`
23+
? A extends WhiteSpace
24+
? TrimLeft<B>
25+
: S
26+
: S;
27+
28+
29+
type TrimRight<S extends string> = S extends `${infer B}${WhiteSpace}`
30+
? TrimRight<B>
31+
: S;
32+
33+
type Trim<S extends string> = TrimLeft<TrimRight<S>>;
34+
35+
/* _____________ Test Cases _____________ */
36+
import type { Equal, Expect } from "@type-challenges/utils";
37+
38+
type cases = [
39+
Expect<Equal<Trim<"str">, "str">>,
40+
Expect<Equal<Trim<" str">, "str">>,
41+
Expect<Equal<Trim<" str">, "str">>,
42+
Expect<Equal<Trim<"str ">, "str">>,
43+
Expect<Equal<Trim<" str ">, "str">>,
44+
Expect<Equal<Trim<" \n\t foo bar \t">, "foo bar">>,
45+
Expect<Equal<Trim<"">, "">>,
46+
Expect<Equal<Trim<" \n\t ">, "">>
47+
];
48+
49+
/* _____________ Further Steps _____________ */
50+
/*
51+
> Share your solutions: https://tsch.js.org/108/answer
52+
> View solutions: https://tsch.js.org/108/solutions
53+
> More Challenges: https://tsch.js.org
54+
*/

0 commit comments

Comments
 (0)