-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcompress_gm.ts
32 lines (24 loc) · 1.13 KB
/
compress_gm.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
namespace Puzzle {
type ToStr<T> = Extract<T, string>;
type ToStrArr<T> = Extract<T, string[]>;
type Length<T extends any[]> = T['length'];
type Result<TCurrent extends string[]> = TCurrent
type FirstChar<T extends string> = T extends `${infer A}${infer _}` ? A : never;
type Join<T extends string[]> = T extends [infer Head, ...infer Tail]
? `${ToStr<Head>}${Join<ToStrArr<Tail>>}`
: '';
type CompressString<TInput extends string, TResult extends Result<string[]> = Result<['']>> =
TInput extends `${infer Head}${infer Tail}`
? Head extends FirstChar<Tail>
? CompressString<Tail, Result<[...TResult, Head]>>
: [`${Length<TResult>}${Head}`, ...CompressString<Tail, Result<['']>>]
: [];
export type Compress<TInput extends string> = Join<CompressString<TInput>>;
}
type ToCompress = 'AAABBAAC'
type Result = Puzzle.Compress<ToCompress>;
// 'assert'
// will report an error to `tsc` if the value assigned to `_assert` doesn't match
// the type Result
// Example: error TS2322: Type ''3A2B2A1C'' is not assignable to type ''3A2B2A2C''.
const _assert: Result = '3A2B2A1C'