-
Notifications
You must be signed in to change notification settings - Fork 0
/
00847-hard-string-join.ts
37 lines (32 loc) · 1.22 KB
/
00847-hard-string-join.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
// Edge cases
const noCharsOutput = join('-')()
const oneCharOutput = join('-')('a')
const noDelimiterOutput = join('')('a', 'b', 'c')
// Regular cases
const hyphenOutput = join('-')('a', 'b', 'c')
const hashOutput = join('#')('a', 'b', 'c')
const twoCharOutput = join('-')('a', 'b')
const longOutput = join('-')('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
type cases = [
Expect<Equal<typeof noCharsOutput, ''>>,
Expect<Equal<typeof oneCharOutput, 'a'>>,
Expect<Equal<typeof noDelimiterOutput, 'abc'>>,
Expect<Equal<typeof twoCharOutput, 'a-b'>>,
Expect<Equal<typeof hyphenOutput, 'a-b-c'>>,
Expect<Equal<typeof hashOutput, 'a#b#c'>>,
Expect<Equal<typeof longOutput, 'a-b-c-d-e-f-g-h'>>
]
// ============= Your Code Here =============
type Join<
Delimiter extends string = '',
Parts extends readonly string[] = []
> = Parts extends [infer Head extends string, ...infer Tail extends string[]]
? Tail extends []
? Head
: `${Head}${Delimiter}${Join<Delimiter, Tail>}`
: ''
declare function join<Delimiter extends string>(
delimiter: Delimiter
): <Parts extends readonly string[]>(...parts: Parts) => Join<Delimiter, Parts>